android自定义视图属性学习

下载了一个Android自定义控件(各种各样的圆形进度条)的DEMO,上面的进度条是模仿iphone的圆形进度条。很有个性。

           学习过程中发现核心关键的一个类:TypedArray

去官方网站看一下吧:TypedArray继承自Object类,然后再看下它的类概述:

Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.
是一个用于存放恢复 obtainStyledAttributes( AttributeSet, int[], int, int )或   obtainAttributes(AttributeSet, int[])   值的一个数组容器,当操作完成以后,一定要调用recycle()方法。 用于检索的索引值在这个结构对应的位置给obtainStyledAttributes属性。

   使用这个类的时候,先要在valuse文件夹下创建:attrs.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="RoundProgressBar">  
        <attr name="roundColor" format="color"/>
        <attr name="roundProgressColor" format="color"/>
        <attr name="roundWidth" format="dimension"></attr>
        <attr name="textColor" format="color" />  
        <attr name="textSize" format="dimension" /> 
        <attr name="max" format="integer"></attr> 
        <attr name="textIsDisplayable" format="boolean"></attr>
        <attr name="style">
            <enum name="STROKE" value="0"></enum>
            <enum name="FILL" value="1"></enum>
        </attr>
    </declare-styleable> 
</resources>

   首先,声明自定义<declare-styleable name="RoundProgressBar">,name RoundProgressBar,属性设置为比较简单的格式,前面参数name,后面是参数格式。

自定义属性的format,可以有以下多种:

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag

然后这样使用:

TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
				R.styleable.RoundProgressBar);
		
		//获取自定义属性和默认值
		roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
		roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
		textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
		textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
		roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
		max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
		textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
		style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
		
		mTypedArray.recycle();

最后一定不要忘记mTypeArray.recycle():

Give back a previously retrieved StyledAttributes, for later re-use.

定义好了自定义属性,就可以在自定控件中的属性设置了:

 <com.example.roundprogressbar.RoundProgressBar
        android:id="@+id/roundProgressBar2"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:layout_alignLeft="@+id/roundProgressBar1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="78dp"
        
        
        android_custom:roundColor="#D1D1D1"
        android_custom:roundProgressColor="@android:color/black"
        android_custom:textColor="#9A32CD"
        android_custom:roundWidth="10dip"
        android_custom:textSize="18sp" />

首先,要有声明:

 xmlns:android_custom="http://schemas.android.com/apk/res/com.example.circlepregress",com.example.circlepregress这个是你项目的包名。

然后我们就可以使用 android_custom:这样设置自定义的属性了。

具体的逻辑绘图部分,下载源码研究下吧!



           

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值