首先自定义一个圆,相信以前的学习大家都会画圆,在values下写一些自定义的属性

package com.exaple.day01rikao;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class Myview extends View {
int radius;
int color;
private final static String Nam = "chen";

public Myview(Context context, AttributeSet attrs) {
super(context, attrs);
/*
* TypedArray ta = context.obtainStyledAttributes(attrs,
* R.styleable.Myview); radius = ta.getInt(R.styleable.Myview_radius,
* 0); color = ta.getInt(R.styleable.Myview_mycolor, 0);

较难的一种方式
*/
radius = attrs.getAttributeIntValue(Nam, "radius", 0);
color = attrs.getAttributeIntValue(Nam, "mycolor", 0);

简单的一中方式
}

@Override
protected void onDraw(Canvas canvas) {
Paint pa = new Paint();
Paint pa1 = new Paint();
float wi = canvas.getWidth();
float he = canvas.getHeight();
pa.setColor(color);
pa1.setColor(Color.WHITE);

canvas.drawColor(Color.WHITE);
canvas.drawCircle(wi / 2, he / 2, radius, pa);
/* canvas.drawCircle(wi / 2, he / 2, 55, pa1); */
/* canvas.drawText("hhhhhhhhhhhhhh", wi / 4, he / 4, pa); */
super.onDraw(canvas);
}

/*
* @Override public boolean onTouchEvent(MotionEvent event) { wi =
* event.getX(); he = event.getY(); this.invalidate();
*
* return true; }
*/

}

在values 下面新建一个attrs文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="Myview">-----类名
<attr name="radius" format="integer"></attr>
<attr name="mycolor" format="color|reference"></attr>
</declare-styleable>

</resources>

Mainactivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fan="chen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.exaple.day01rikao.Myview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fan:mycolor="#f00"
fan:radius="100" />

</RelativeLayout>

自定义属性 view的更多相关文章

  1. 微信小程序开发常用方法

    1.函数中访问data中的数据 _this.setData({ // 日历数据 signList: dataList, // 当前日期 todayDay: str }) 2.if判断 wx:if=&q ...

  2. Android自定义View自定义属性

    1.引言 对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleabl ...

  3. View (二) 自定义属性

    主要有三种方法可以实现自定义属性. 方法一:不使用命名空间,不使用attrs.xml文件.通过attrs.getAttributeResourceValue方法拿到属性值 方法二: 使用命名空间, 不 ...

  4. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...

  5. Android 自定义View修炼-自定义View-带百分比进度的圆形进度条(采用自定义属性)

    很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如o ...

  6. Android自定义View(二、深入解析自定义属性)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51468648 本文出自:[openXu的博客] 目录: 为什么要自定义属性 怎样自定义属性 ...

  7. Android初级教程初谈自定义view自定义属性

    有些时候,自己要在布局文件中重复书写大量的代码来定义一个布局.这是最基本的使用,当然要掌握:但是有些场景都去对应的布局里面写对应的属性,就显得很无力.会发现,系统自带的控件无法满足我们的要求,这个时候 ...

  8. Android 自定义View二(深入了解自定义属性attrs.xml)

    1.为什么要自定义属性 要使用属性,首先这个属性应该存在,所以如果我们要使用自己的属性,必须要先把他定义出来才能使用.但我们平时在写布局文件的时候好像没有自己定义属性,但我们照样可以用很多属性,这是为 ...

  9. 手机安全卫士——在设置中心 自定义view和自定义属性

    自定义组合控件 1. 自定义一个View, 继承ViewGroup,比如RelativeLayout,此文中是SettingItemView 2. 编写组合控件的布局文件,在自定义的View中加载   ...

随机推荐

  1. Hibernate映射多对多双向关联关系(小案例)

    多对多双向关联关系(Project(工程)/Emp(员工)为案例): 步骤如下: 1.创建Project类,并需要定义集合类型的Emp属性 public class Project { //编号 pr ...

  2. ado.net 实体类_数据访问类

    实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  3. java二叉树的实现和遍历

    /* * Java实现二叉树 */ public class BinaryTree { int treeNode; BinaryTree leftTree; BinaryTree rightTree; ...

  4. Eclipse设置注释模板

    设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...

  5. 学习Jquery

    早就听说了Jquery的大名,一直没有细心的学习一下,通过阅读收集的一些资料,感觉Jquery真的很强大.决定开始自己的学习Jquery之旅.在这里不是为大家讲解Jquery(深知水平有限),只是将自 ...

  6. Python脚本模拟登录网页之ZiMuZu篇

    ZiMuZu.tv这个网站喜欢看电影看美剧的人一定都熟悉. 这个网站原先的升级策略是每天登陆网站, 然后去一个"每日签到"的页面点击一个签到按钮, 以实现帐号等级的升级. 之前网上 ...

  7. HTML中禁用表单控件的两种方法readonly与disabled

    时候我们会希望表单上的控件是不可修改的,比如在修改密码的网页中,显示用户名的文本框就应该是不可修改状态的,下面与大家分享下禁用表中控件的两种方法 在网页的制作过程中,我们会经常使用到表单.但是有时候我 ...

  8. 开发回归测试之Selenium开发环境搭建

    前言 在解bug的过程中,经常遇到解完某个bug带来新的bug,原因一般是因为回归测试不足或不全面,下面我们来看看怎么利用selenium工具来完成开发后的回归测试, 虽然这可能是QA的事,不过有利于 ...

  9. Java中Double类型计算问题

    public class Test{    public static void main(String args[]){        System.out.println(0.05+0.01);  ...

  10. easyUI loyout tabs自适应宽度

    index.html 页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...