android自定义View——等级滑动条

先看效果,用豌豆荚演示功能做的Gif,比较卡顿
这里写图片描述

思路:
首先绘制直线,然后等分直线绘制点;
绘制点的时候把X值存到集合中。
然后绘制背景图片,以及图片上的数字。
点击事件down的时候,换小图片为大图片。move的时候跟随手指移动。
up的时候根据此时的X计算最近的集合中的点,然后自动吸附回去。

1,自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="BeautySeekBarView">  
        <attr name="valueCountent" format="integer"/>  
         <attr name="padding" format="dimension"/>     
        <attr name="pointColor" format="color"/>  
        <attr name="lineColor" format="color"/>
        <attr name="smallPic" format="reference"/>  
        <attr name="bigPic" format="reference"/>    
    </declare-styleable>  
</resources>

然后获取属性:

  /** 
         * 获得我们所定义的自定义样式属性 
         */  
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BeautySeekBarView, defStyleAttr, 0);
        //等级数量即点的个数
        valueCountent=a.getInteger(R.styleable.BeautySeekBarView_valueCountent, 5);
        //点的颜色
        pointColor = a.getColor(R.styleable.BeautySeekBarView_pointColor, Color.WHITE);  
        //线的颜色
        lineColor = a.getColor(R.styleable.BeautySeekBarView_lineColor, Color.WHITE);
        //小图片
        smallPic=a.getResourceId(R.styleable.BeautySeekBarView_smallPic, R.drawable.ic_launcher);
        //滑动过程中的大图片
        bigPic=a.getResourceId(R.styleable.BeautySeekBarView_bigPic, R.drawable.ic_launcher);    
        //控件的内边距
        viewPadding=a.getDimensionPixelSize(R.styleable.BeautySeekBarView_padding, (int) TypedValue.applyDimension(  
                        TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));

        a.recycle();        

2.绘制

@Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值