Android自定义控件(十二)——自定义属性及应用

如何自定义属性

我们经常使用XML引入各种控件,比如TextView,Button等,使用过程中,我们通过设置各种属性值,让控件变的多姿多彩,比如我们使用android:layout_width定义控件的宽度,使用android:layout_height定义控件的高度。那么现在我有一个需求,我想自己定义个圆的属性,画一个闭合的圆形应该怎么做?我们先来看一段代码:

<com.liyuanjinglyj.customproperty.CircleView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:radius="250"
    app:CircleX="250"
    app:CircleY="250"/>

这里引入了一个博主自己定义的View,看看最后三个属性,是不是没见过?这就是自定义的属性值,我们在这里设置了自定义属性的半径,以及圆心位置。那么这些属性在哪里?

在小编新建的attrs.xml文件中,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CircleView">
        <attr name="radius" format="integer"/>
        <attr name="CircleX" format="integer"/>
        <attr name="CircleY" format="integer"/>
    </declare-styleable>
</resources>

所有自定义的属性都是通过declare-styleable标签实现的,而它的name,就是自定义控件的类名,这一点不能变,也不能改,这是死规则。也就是说哪个类用的自定义属性值,name就是哪个类名。

在来看看其中定义的三个属性,他们的名字分别是radius,CircleX,CircleY,也就是上面控件直接引入的值,后面的format,就是引入的值的类型。比如这样属性必须传入int类型。

format取值类型

我们上面自定义的属性format是一个integer值,这个也算是非常常用的,但我们除了这些常会用的,比如,boolean,string,float等,还有三个类型值,分别是:

(1)reference:指的是从string.xml,drawable.xml,以及color.xml等文件中引入过来的值,比如常常在ImagView中使用的android:src="@drawable/background",这种引入值就使用这个类型。

(2)flag:是自己定义的值,比如android:gravity=“top”,以及如下自定义属性代码:

<attr name="age">
	<flag name="child" value="10"/>
	<flag name="young" value="18"/>
	<flag name="old" value="60"/>
</attr>

<!--XML中使用属性app:age="child"-->

这种方式,常用在在几个选项中,选择设置的需求中,比如排版问题上的,只能选居中,左对齐,右对齐等,其他的不需要。

(3)dimension:指的是从dimension.xml文件中引入过来的值,特别需要注意的是,如果这个是dp,就会进行像素转换。

引入自定义属性

虽然说自定了上面的属性类型,但并不能直接在代码中引用,还要先导入自定义属性集才能使用,这里我们有两种导入方式:

xmlns:app="http://schemas.android.com/apk/res/com.liyuanjinglyj.customproperty"
xmlns:app="http://schemas.android.com/apk/res-auto"

推荐使用第二章方式导入,当然,他们都是引入到XML根控件之中的,但这里还需要解释两点。

(1)app这个是自定义的,这个可以定义成任何你想要的内容,你定义liyuanjing,那么就通过liyuanjing:radius引入自定义属性,其他字符类同。

(2)如果使用第一种引入方式,那么apk/res/后面的就是你程序的包名,包名在AndroidManifest.xml根节点的package属性中。

代码中获取自定义属性

既然我们在XML中引入了自定义属性以及自定义的View,肯定是要用到这些属性的,所以我们还要掌握在程序中如何使用这些属性,代码如下:

public CircleView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CircleView);//获取自定义属性
    this.radius=typedArray.getInt(R.styleable.CircleView_radius,50);
    this.circleX=typedArray.getInt(R.styleable.CircleView_CircleX,25);
    this.circleY=typedArray.getInt(R.styleable.CircleView_CircleY,25);
    typedArray.recycle();
}

首先,我们在自定义View的构造函数中,有一个attrs参数,这个参数就是获取设置属性用的,我们可以通过context.obtainStyledAttributes获取到自定义属性,然后根据类型获取到设置的值,比如这里获取的integer类型,第二个参数是默认值。当没有在XML中设置这个属性的时候,使用默认值。

备注:还有这里需要注意以下,使用完成之后,记得释放TypedArray资源。

自定义属性画圆

上面我们已经都介绍完,如何自定义属性,以及获取属性值的方式,这里我们来自定义一个CircleView来根据XML设置的值,画一个圆,自定义View代码如下:

public class CircleView extends View {
    private int radius;
    private int circleX,circleY;
    private Paint paint;
    public CircleView(Context context) {
        super(context);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CircleView);//获取自定义属性
        this.radius=typedArray.getInt(R.styleable.CircleView_radius,50);
        this.circleX=typedArray.getInt(R.styleable.CircleView_CircleX,25);
        this.circleY=typedArray.getInt(R.styleable.CircleView_CircleY,25);
        typedArray.recycle();
        this.paint=new Paint();
        this.paint.setColor(Color.RED);
        this.paint.setStyle(Paint.Style.FILL);
        this.paint.setStrokeWidth(6);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(this.circleX,this.circleY,this.radius,this.paint);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

其实很简单就是刚上面讲解的知识,获取XML中设置的值,根据这里值画一个圆。自定义属性在attrs.xml中,也就是开头定义的三个integer类型。XML完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <com.liyuanjinglyj.customproperty.CircleView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:radius="250"
        app:CircleX="250"
        app:CircleY="250"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background"/>

</LinearLayout>

实现的效果如下图所示:

实现效果
本文Github源代码下载地址:点击下载

发布了94 篇原创文章 · 获赞 115 · 访问量 75万+

猜你喜欢

转载自blog.csdn.net/liyuanjinglyj/article/details/103327186
今日推荐