注册

做了这么多年开发,TypedArray你该知道的东西




大家好,我是程序员双木L,后续会发专题类的文章,这是自定义控件的第一篇,之后也会陆续更新相关的文章,欢迎关注。


自定义属性在自定义控件过程中属于比较常见的操作,我们可以回想一下这样的场景:自定义view的过程中,我们需要在不同的情况下设置不同的文字大小,那么我们是不是就需要提供对外的方法来设置,这样就比较灵活操作。而我们自定义对外的方法,就是我们自定义的属性啦,那我们来分析一下其原理及作用。


下面我们根据例子来进行分析:


1、首先我们需要在res->values目录下新建attrs.xml文件,该文件就是用来声明属性名及其接受的数据格式的,如下:


<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="view_int" format="integer" />
<attr name="view_str" format="string" />
<attr name="view_bool" format="boolean" />
<attr name="view_color" format="color" />
<attr name="view_ref" format="reference" />
<attr name="view_float" format="float" />
<attr name="view_dim" format="dimension" />
<attr name="view_frac" format="fraction" />

<attr name="view_enum">
<enum name="num_one" value="1" />
<enum name="num_two" value="2" />
<enum name="num_three" value="3" />
<enum name="num_four" value="4" />
</attr>

<attr name="view_flag">
<flag name="top" value="0x1" />
<flag name="left" value="0x2" />
<flag name="right" value="0x3" />
<flag name="bottom" value="0x4" />
</attr>

</resources>

attr名词解析:


name表示属性名,上面的属性名是我自己定义的。


format表示接受的输入格式,format格式集合如下:


color:颜色值;
boolean:布尔值;
dimension:尺寸值,注意,这里如果是dp那就会做像素转换;
float:浮点值;
integer:整型值;
string:字符串;
fraction:百分数;
enum:枚举值;
flag:是自己定义的,就是里面对应了自己的属性值;
reference:指向其它资源;
reference|color:颜色的资源文件;
reference|boolean:布尔值的资源文件.

2、自定义属性的使用,这里我们使用两种方式进行对比解析


最最最原始的使用方式


(1)、自定义文件如下:


public class TestAttrsView extends View {
private final String TAG = "TestAttrsView:";

public TestAttrsView(Context context) {
this(context, null);
}

public TestAttrsView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

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

//最原始使用方式
for (int i = 0; i < attrs.getAttributeCount(); i++) {
Log.i(TAG, "name:" + attrs.getAttributeName(i) + " value:" + attrs.getAttributeValue(i));
}
}
}

我们可以在TestAttrsView方法的参数AttributeSet是个xml解析工具类,帮助我们从布局的xml里提取属性名和属性值。


(2)、在布局文件xml中的使用


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.example.viewdemo.customView.TestAttrsView
android:layout_width="200dp"
android:layout_height="200dp"
app:view_bool="true"
app:view_color="#e5e5e5"
app:view_dim="10px"
app:view_float="5.0"
app:view_frac="100%"
app:view_int="10"
app:view_ref="@dimen/dp_15"
app:view_str="test attrs view" />

</FrameLayout>

这里使用自定义属性需要声明xml的命名空间,其中app是命名空间,用来加在自定义属性前面。


xmlns:app=“http://schemas.android.com/apk/res-auto”
声明xml命名空间,xmlns意思为“xml namespace”.冒号后面是给这个引用起的别名。
schemas是xml文档的两种约束文件其中的一种,规定了xml中有哪些元素(标签)、
元素有哪些属性及各元素的关系,当然从面向对象的角度理解schemas文件可以
认为它是被约束的xml文档的“类”或称为“模板”。


(3)、将属性名与属性值打印结果如下:


在这里插入图片描述


从打印结果我们可以看出,AttributeSet将布局文件xml下的属性全部打印出来了,细心的童鞋可能已经看出来:


xml文件:
app:view_ref="@dimen/dp_15"

打印结果:
name:view_ref value:@2131034213

这个属性我们设置的是一个整数尺寸,可最后打印出来的是资源编号。


那如果我们想要输出我们设置的整数尺寸,需要怎么操作呢?


这个时候就该我们这篇的主角出场了,使用TypedArray方式。



  • 使用TypedArray方式

(1)、这里我们需要将attrs.xml使用“declare-styleable”标签进行改造,如下:


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

<declare-styleable name="TestStyleable">
<attr name="view_int" format="integer" />
<attr name="view_str" format="string" />
<attr name="view_bool" format="boolean" />
<attr name="view_color" format="color" />
<attr name="view_ref" format="reference" />
<attr name="view_float" format="float" />
<attr name="view_dim" format="dimension" />
<attr name="view_frac" format="fraction" />

<attr name="view_enum">
<enum name="num_one" value="1" />
<enum name="num_two" value="2" />
<enum name="num_three" value="3" />
<enum name="num_four" value="4" />
</attr>

<attr name="view_flag">
<flag name="top" value="0x1" />
<flag name="left" value="0x2" />
<flag name="right" value="0x3" />
<flag name="bottom" value="0x4" />
</attr>
</declare-styleable>

</resources>

从改造后的attrs文件可以看出,我们将属性声明归结到TestStyleable里面,也就意味着这些属性是属于TestStyleable下的。


(2)、属性的解析:


public class TestAttrsView extends View {
private final String TAG = "TestAttrsView:";

public TestAttrsView(Context context) {
this(context, null);
}

public TestAttrsView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

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

//最原始使用方式
/* for (int i = 0; i < attrs.getAttributeCount(); i++) {
Log.i(TAG, "name:" + attrs.getAttributeName(i) + " value:" + attrs.getAttributeValue(i));
}*/


//使用TypeArray方式
//R.styleable.TestStyleable 指的是想要解析的属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TestStyleable);

int integerView = typedArray.getInt(R.styleable.TestStyleable_view_int, 0);
Log.i(TAG, "name:view_int" + " value:" + integerView);

boolean aBooleanView = typedArray.getBoolean(R.styleable.TestStyleable_view_bool, false);
Log.i(TAG, "name:view_bool" + " value:" + aBooleanView);

int colorView = typedArray.getColor(R.styleable.TestStyleable_view_color, Color.WHITE);
Log.i(TAG, "name:view_color" + " value:" + colorView);

String stringView = typedArray.getString(R.styleable.TestStyleable_view_str);
Log.i(TAG, "name:view_str" + " value:" + stringView);

float refView = typedArray.getDimension(R.styleable.TestStyleable_view_ref, 0);
Log.i(TAG, "name:view_ref" + " value:" + refView);

float aFloatView = typedArray.getFloat(R.styleable.TestStyleable_view_float, 0);
Log.i(TAG, "name:view_float" + " value:" + aFloatView);

float dimensionView = typedArray.getDimension(R.styleable.TestStyleable_view_dim, 0);
Log.i(TAG, "name:view_dim" + " value:" + dimensionView);

float fractionView = typedArray.getFraction(R.styleable.TestStyleable_view_frac, 1, 1, 0);
Log.i(TAG, "name:view_frac" + " value:" + fractionView);

//typedArray存放在缓存池,使用完需要释放缓存池
typedArray.recycle();
}
}

这里我直接打印出解析结果,这里可以获取我们想要的自定义属性,而系统有的属性可以忽略。


(3)、运行结果如下


在这里插入图片描述


从解析的结果可以看出,尺寸的结果已经转换为实际值了:


xml文件:
app:view_ref="@dimen/dp_15"

打印结果:
name:view_ref value:41.25

这个时候有童鞋又问了,我设置的是15dp,为啥最后打印是41.25了呢?其实解析出来的值单位是px,所以这里输出的是转换后的值。


解析的过程中用到了这个方法:


context.obtainStyledAttributes(attrs, R.styleable.TestStyleable);

我们来看一下这个方法的源码:


   public final TypedArray obtainStyledAttributes(
@Nullable AttributeSet set, @NonNull @StyleableRes int[] attrs) {
return getTheme().obtainStyledAttributes(set, attrs, 0, 0);
}

源码中我们可以看到这个方法有两个参数:


AttributeSet set:表示当前xml声明的属性集合

int[] attrs:表示你想挑选的属性,你想得到哪些属性,你就可以将其写到这个int数组中


obtainStyledAttributes方法返回值类型为TypedArray。该类型记录了获取到的属性值集合,而通过数组下标索引即可找到对应的属性值。索引下标通过R.styleable.TestStyleable_xx获取,“xx"表示属性名,一般命名为"styleable名” + “_” + “属性名”。


而TypedArray提供了各种Api,如getInteger,getString,getDimension等方法来获取属性值,这些方法都需要传入对应属性名在obtainStyledAttributes中的int数组的位置索引,通过下标获取数组里属性值。


这个TypedArray的作用就是资源的映射作用,把自定义属性在xml设置值映射到class,这样怎么获取都很简单啦。


到这里就分析完啦!

1 个评论

原来TypedArray是这样的 太强了

要回复文章请先登录注册