Android自定义控件2----自定义属性

 

 

果图

项目结构:

activity_main.xml中:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:zhh="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.selfattrs.MainActivity">

    <com.example.selfattrs.MyAttributeView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        zhh:my_age="100"
        zhh:my_bg="@drawable/jtx"
        zhh:my_name="android0220"
        />
</RelativeLayout>

attrs.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--定义名字叫MyAttributeView属性集合-->
    <declare-styleable name="MyAttributeView">
        <!--定义一个名字叫my_name并且类型是string的属性-->
        <attr name="my_name" format="string"/>
        <!--定义一个名字叫my_age并且类型是integer的属性-->
        <attr name="my_age" format="integer"/>
        <!--定义一个名字叫my_bg并且类型是reference|color的属性-->
        <attr name="my_bg" format="reference|color"/>
    </declare-styleable>
</resources>
MyAttributeView中
/**
 * 作用:自定义属性
 * http://www.gulixueyuan.com/course/124/learn#lesson/1908
 */
public class MyAttributeView extends View {
    private int myAge;
    private String myName;
    private Bitmap myBg;
    /**
     * 3个构造方法的用途
     * 一个参数的构造方法,在代码中new对象的时候调用;
     * 两个参数的构造方法,在布局文件中使用的时候被调用;
     * 三个参数的构造方法,在自定义属性的时候被调用;
     * 这里为了代码写的更加严密,
     * 一个参数的构造方法,调用两个参数的构造方法,两个参数的构造方法,调用三个参数的构造方法
     *
     */

    /**
     * 在代码中new MyAttributeView(context)的时候
     * 调用这个构造方法
     * @param context
     */
    public MyAttributeView(Context context) {
//      调MyAttributeView(Context context, AttributeSet attrs)
        this(context, null);
    }

    /**
     * 在布局文件中使用时,调用这个构造方法
     * @param context
     * @param attrs
     */
    public MyAttributeView(Context context, AttributeSet attrs) {
//      调MyAttributeView(Context context, AttributeSet attrs, int defStyleAttr)
        this(context, attrs, 0);
    }

    /**
     * 自定义属性的时候,调用这个构造方法
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public MyAttributeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取属性4种方式
        //1.用命名空间取获取
        String age = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_age");
        String name = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_name");
        String bg = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_bg");
        System.out.println(">>>age==" + age + ",name==" + name + ",bg===" + bg);

        //2.遍历属性集合
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            System.out.println(attrs.getAttributeName(i) + "=====" + attrs.getAttributeValue(i));
        }

        //3.使用系统工具,获取属性(常用方法)
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyAttributeView);
//        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyAttributeView, defStyleAttr,0);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int index = typedArray.getIndex(i);

            switch (index) {
//                MyAttributeView_my_age 的意思是 :MyAttributeView 是属性集合的名称,
//                                                  my_age 是自定义属性的名称
//                                                  把这两个用下划线连起来
                case R.styleable.MyAttributeView_my_age:
                    myAge = typedArray.getInt(index, 0);
                    break;
                case R.styleable.MyAttributeView_my_name:
                    myName = typedArray.getString(index);
                    break;
                case R.styleable.MyAttributeView_my_bg:
                    Drawable drawable = typedArray.getDrawable(index);
                    BitmapDrawable drawable1 = (BitmapDrawable) drawable;
                    myBg = drawable1.getBitmap();
                    break;
            }
        }
        // 记得回收
        typedArray.recycle();
    }

    /**
     * 绘制视图
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
//      根据属性绘制图像
        canvas.drawText(myAge + "---" + myName, 50, 50, paint);
        canvas.drawBitmap(myBg, 50, 50, paint);
    }
}

对上面的代码进行分析:

代码分析:自定义属性步骤
1在activity_main.xml中
 <com.example.selfattrs.MyAttributeView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        zhh:my_age="100"
        zhh:my_bg="@drawable/jtx"
        zhh:my_name="android0220"
        />
  对应的命名空间
  xmlns:zhh="http://schemas.android.com/apk/res-auto"

2在values中创建attrs.xml文件
 在创建对应的属性
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
    <!--定义名字叫MyAttributeView属性集合-->
    <declare-styleable name="MyAttributeView">
        <!--定义一个名字叫my_name并且类型是string的属性-->
        <attr name="my_name" format="string"/>
        <!--定义一个名字叫my_age并且类型是integer的属性-->
        <attr name="my_age" format="integer"/>
        <!--定义一个名字叫my_bg并且类型是reference|color的属性-->
        <attr name="my_bg" format="reference|color"/>
    </declare-styleable>
  </resources>

3在自定义的控件类中MyAttributeView在MyAttributeView(Context context, AttributeSet attrs, int defStyleAttr)
 构造方法中拿到自定义的属性值(三种方法,第三种方法最常用)
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyAttributeView);
        for(int i=0;i<typedArray.getIndexCount();i++){
          int index =  typedArray.getIndex(i);
           switch (index){
               case R.styleable.MyAttributeView_my_age:
                   myAge = typedArray.getInt(index,0);
                   break;
               case R.styleable.MyAttributeView_my_name:
                   myName = typedArray.getString(index);
                   break;
               case R.styleable.MyAttributeView_my_bg:
                   Drawable drawable = typedArray.getDrawable(index);
                   BitmapDrawable drawable1 = (BitmapDrawable) drawable;
                   myBg = drawable1.getBitmap();
                   break;
           }
       }
        // 记得回收
        typedArray.recycle();

4根据设置的属性绘制视图
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
//      根据属性绘制图像
        canvas.drawText(myAge+"---"+myName,50,50,paint);
        canvas.drawBitmap(myBg,50,50,paint);
    }
总结:所有的视图控件都是通过onDraw方法绘制出来的,设置不同的
      属性就是根据属性进行绘制

源码下载:

Myself ---- selfattrs

http://download.csdn.net/download/zhaihaohao1/10111351

参考视频:

http://www.gulixueyuan.com/course/124/learn#lesson/1908

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值