自定义ViewPagerIndicator(下)

由于有些参数定义为成员变量,不好复制,具体请参考源码。
(1) 在values目录中创建attrs.xml( 技巧:拷贝ApiDemos\res\values\attrs.xml,保留labelview,删除其他进行编辑比较快)

<resources>

    <declare-styleable name="PagerIndicator">
        <attr name="selectedColor" format="color" />
        <attr name="strokeWidth" format="dimension" />
        <attr name="unselectedColor" format="color" />
        <attr name="radius" format="dimension" />
    </declare-styleable> 
</resources>

(2) 在自定义控件两个参数构造方法中解析属性,并设置到画笔和画布上

public PagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, 
            R.styleable.PagerIndicator);
        colorSelect = a.getColor(
            R.styleable.PagerIndicator_selectedColor, Color.GRAY);
        colorUnselect = a.getColor(
            R.styleable.PagerIndicator_unselectedColor, Color.RED);
        strokeWidth = a.getDimension(
            R.styleable.PagerIndicator_strokeWidth, 3);
        radius = a.getDimension(
            R.styleable.PagerIndicator_radius, 15);
        a.recycle();// 释放内存
        initPaint();
    }

(3)在需要此控件的布局上使用它

 <com.xinbo.demo.PagerIndicator
        xmlns:indicator="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pagerIndicator1"
        indicator:selectedColor="#0000FF"
        indicator:unselectedColor="#00FF00"
        indicator:strokeWidth="1dp"
        indicator:radius="10dp"
        android:layout_width="match_parent"
        android:background="#40000000"
        android:layout_gravity="bottom"
        android:layout_height="80dp" />

源码地址:http://download.csdn.net/download/sjc1010592550/9090189

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值