(4.1.37.5)自定义控件的xml属性:declare-styleable

做Android布局是件很享受的事,这得益于他良好的xml方式。使用xml可以快速有效的为软件定义界面。可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了。那么如何才能做到像官方提供的那些组件一样用xml来定义他的属性呢?现在我们就来讨论一下他的用法。

一、示例

1.1 在res/values文件下定义一个attrs.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ToolBar"> 
        <attr name="buttonNum" format="integer"/> 
        <attr name="itemBackground" format="reference|color"/> 
    </declare-styleable> 
</resources>

1.2 在布局xml中如下使用该属性

  • 现在都支持 app:前缀自动匹配
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <cn.zzm.toolbar.ToolBar android:id="@+id/gridview_toolbar" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@drawable/control_bar" 
        android:gravity="center" 
        toolbar:buttonNum="5" 
        toolbar:itemBackground="@drawable/control_bar_item_bg"/> 
</RelativeLayout>

1.3 在自定义组件中,可以如下获得xml中定义的值

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar); 
buttonNum = a.getInt(R.styleable.ToolBar_buttonNum, 5); 
itemBg = a.getResourceId(R.styleable.ToolBar_itemBackground, -1);
a.recycle();

在自定义组件的构造函数中,用

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar);

来获得对属性集的引用,然后就可以用“a”的各种方法来获取相应的属性值了。

这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。

当然,在取完值的时候别忘了回收资源哦!

二、declare-styleable的参数解析

2.1 需要用< declare-styleable name=”ToolBar”>包围所有属性

其中name为该属性集的名字,主要用途是标识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用到”R.styleable.ToolBar_buttonNum”,很显然,他在每个属性前面都加了”ToolBar_”。

2.2 支持属性类型

类型示意定义使用备注
reference参考指定Theme中资源ID< attr name=”label” format=”reference” >< Button app:label=”@string/app_name” >
dimension尺寸值< attr name=”myWidth” format=”dimension” />< Button zkx:myWidth=”100dip”/>
float浮点型< attr name=”fromAlpha” format=”float” />< alpha app:fromAlpha=”0.3”/>
boolean布尔值< attr name=”isVisible” format=”boolean” />< Button app:isVisible=”false”/>
integer整型< alpha zkx:fromAlpha=”0.3”/>< animated-rotate app:framesCount=”22”/>
string字符串< attr name=”Name” format=”string” />< rotate app:Name=”My name is zhang kun xiang”/>
fraction百分数< attr name=”pivotX” format=”fraction” />< rotate app:pivotX=”200%”/>
flag位或运算< attr name=”windowSoftInputMode”>< flag name=”stateUnspecified” value=”1” />< flag name = “adjustNothing” value = “0x30” />< /attr>< activity android:windowSoftInputMode=”stateUnspecifiedadjustNothing”>
color颜色< attr name=”textColor” format=”color” />< Button zkx:textColor=”#ff0000”/>
enum枚举< attr name=”language”>< enum name=”English” value=”1”/>< /attr>< Button app:language=”English”/>

属性定义时可以指定多种类型值:

<declare-styleable name = "名称">    
    <attr name="background" format="reference|color" />
    </declare-styleable>

使用:

  <ImageView android:background = "@drawable/图片ID|#00FF00"/>

三、Java中获取

具体属性的获取要拼合定义的name,例如DiyWidget_diy_topDividerShow:

    protected void assignXml(AttributeSet attrs){
        if(attrs != null){
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DiyWidget);
            if(typedArray != null){
                topDividerShow = typedArray.getBoolean(R.styleable.DiyWidget_diy_topDividerShow, topDividerShow);
                topDividerIndent = typedArray.getBoolean(R.styleable.DiyWidget_diy_topDividerIndent, topDividerIndent);
                topDividerIndentValue = typedArray.getDimensionPixelSize(R.styleable.DiyWidget_diy_topDividerIndentValue, topDividerIndentValue);

                bottomDividerShow = typedArray.getBoolean(R.styleable.DiyWidget_diy_bottomDividerShow, bottomDividerShow);
                bottomDividerIndent = typedArray.getBoolean(R.styleable.DiyWidget_diy_bottomDividerIndent, bottomDividerIndent);
                bottomDividerIndentValue = typedArray.getDimensionPixelSize(R.styleable.DiyWidget_diy_bottomDividerIndentValue, bottomDividerIndentValue);

                contentPaddingLeft = typedArray.getDimensionPixelSize(R.styleable.DiyWidget_diy_contentPaddingleft, contentPaddingLeft);
                contentPaddingRight = typedArray.getDimensionPixelSize(R.styleable.DiyWidget_diy_contentPaddingRight, contentPaddingRight);
                contentPaddingTop = typedArray.getDimensionPixelSize(R.styleable.DiyWidget_diy_contentPaddingTop, contentPaddingTop);
                contentPaddingBottom = typedArray.getDimensionPixelSize(R.styleable.DiyWidget_diy_contentPaddingBottom, contentPaddingBottom);

                initialFocusedWhenTouched = typedArray.getBoolean(R.styleable.DiyWidget_diy_focusedWhenTouched, initialFocusedWhenTouched);
                typedArray.recycle();
            }

        }
    }

其他

解析:TypedArray 为什么需要调用recycle()

在 Android 自定义 View 的时候,需要使用 TypedArray 来获取 XML layout 中的属性值,使用完之后,需要调用 recyle() 方法将 TypedArray 回收。

那么问题来了,这个TypedArray是个什么东西?为什么需要回收呢?TypedArray并没有占用IO,线程,它仅仅是一个变量而已,为什么需要 recycle?
为了解开这个谜,首先去找官网的 Documentation,到找 TypedArray 方法,得到下面一个简短的回答:
这里写图片描述
告诉我们在确定使用完之后调用 recycle() 方法。于是进一步查看该方法的解释,如下:
这里写图片描述

简单翻译下来,就是说:回收 TypedArray,用于后续调用时可复用之。当调用该方法后,不能再操作该变量。同样是一个简洁的答复,但没有解开我们心中的疑惑,这个TypedArray背后,到底隐藏着怎样的秘密……

我们知道TypedArray 的常规使用方法:

TypedArray array = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.PieChart,0,0);
try {
    mShowText = array.getBoolean(R.styleable.PieChart_showText,false);
    mTextPos = array.getInteger(R.styleable.PieChart_labelPosition,0);
}finally {
    array.recycle();
}

可见,TypedArray不是我们new出来的,而是调用了 obtainStyledAttributes 方法得到的对象,该方法实现如下:

public TypedArray obtainStyledAttributes(AttributeSet set,
                int[] attrs, int defStyleAttr, int defStyleRes) {
    final int len = attrs.length;
    final TypedArray array = TypedArray.obtain(Resources.this, len);
    // other code .....
    return array;
}

从上面的代码片段得知,TypedArray也不是它实例化的,而是调用了TypedArray的一个静态方法,得到一个实例,再做一些处理,最后返回这个实例。看到这里,我们似乎知道了什么,,,带着猜测,我们进一步查看该静态方法的内部实现:

public class TypedArray {

    static TypedArray obtain(Resources res, int len) {
        final TypedArray attrs = res.mTypedArrayPool.acquire();
        if (attrs != null) {
            attrs.mLength = len;
            attrs.mRecycled = false;

            final int fullLen = len * AssetManager.STYLE_NUM_ENTRIES;
            if (attrs.mData.length >= fullLen) {
                return attrs;
            }

            attrs.mData = new int[fullLen];
            attrs.mIndices = new int[1 + len];
            return attrs;
        }

        return new TypedArray(res,
                new int[len*AssetManager.STYLE_NUM_ENTRIES],
                new int[1+len], len);
    }
    // Other members ......
}

该类没有公共的构造函数,只提供静态方法获取实例,显然是一个典型的单例模式。在代码片段的第 13 行,很清晰的表达了这个 array 是从一个 array pool的池中获取的

结论:

程序在运行时维护了一个 TypedArray的池,程序调用时,会向该池中请求一个实例,用完之后,调用 recycle() 方法来释放该实例,从而使其可被其他模块复用。

那为什么要使用这种模式呢?答案也很简单,TypedArray的使用场景之一,就是上述的自定义View,会随着 Activity的每一次Create而Create,因此,需要系统频繁的创建array,对内存和性能是一个不小的开销,如果不使用池模式,每次都让GC来回收,很可能就会造成OutOfMemory。

这就是使用池+单例模式的原因,这也就是为什么官方文档一再的强调:使用完之后一定 recycle,recycle,recycle

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值