简单的自定义带有动画的checkbox

android自带的checkbox交互体验不好点击一下干巴巴的难看,很早就写了一个demo现在贴上来。
没必要解释很简单了

public class RightView extends View implements View.OnClickListener,ValueAnimator.AnimatorUpdateListener{
    private Paint borderPaint;
    private float paintWidth=5;
    private int animDuration=200;
    private int selectColor=Color.RED;
    private int unselectColor=Color.GRAY;
    private boolean pressed=false;
    Path pathLine,dst1;
    PathMeasure pathMeasure;
    ValueAnimator animator;

    private RightViewSelectListener listener;
    float v1;
    public RightView(Context context) {
        this(context,null);
    }

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

    public RightView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        setOnClickListener(this);
    }

    private void init() {
        setBackgroundColor(Color.WHITE);
        borderPaint=new Paint();
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setAntiAlias(true);


        pathLine=new Path();
        dst1=new Path();
        pathMeasure=new PathMeasure();
        animator=ValueAnimator.ofFloat(0,1);
        animator.setDuration(animDuration);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureDimension(widthMeasureSpec),measureDimension(heightMeasureSpec));
    }
    private int measureDimension(int ms){
        int result;
        int measureMode=MeasureSpec.getMode(ms);
        int measureSize=MeasureSpec.getSize(ms);
        if(measureMode==MeasureSpec.EXACTLY){
            result=measureSize;
        }else {
            result=20;
            if(measureMode==MeasureSpec.AT_MOST){
                result=Math.max(result,measureSize);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect=new Rect(0,0,getWidth(),getHeight());
        if(!pressed){
            borderPaint.setColor(unselectColor);
        }else{
            //点击之后
            borderPaint.setStrokeWidth(paintWidth/2);
            borderPaint.setColor(selectColor);
            pathLine.moveTo(20, (float) (getHeight()*2.0/3));
            pathLine.lineTo((float) (getWidth()*1.0/3),getHeight()-20);
            pathLine.lineTo(getWidth()-20, (float) (getHeight()*1.0/3));
            pathMeasure.setPath(pathLine,false);
            pathMeasure.getSegment(0,v1*pathMeasure.getLength(),dst1,true);
            canvas.drawPath(dst1,borderPaint);
            dst1.reset();
        }
        borderPaint.setStrokeWidth(paintWidth);
        canvas.drawRect(rect,borderPaint);//边框
    }

    @Override
    public void onClick(View v) {
        if(pressed){
            pressed=false;
            invalidate();
        }else{
            pressed=true;
            animator.start();
        }
        if(listener!=null){
            listener.onSelect(pressed);
        }
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        if(animation.equals(animation)){
            v1= ((float) animation.getAnimatedValue());
            invalidate();
        }
    }

    public boolean getState(){
        return pressed;
    }

    public void setRightViewSelectListener(RightViewSelectListener l){
        listener=l;
    }

    interface RightViewSelectListener{
        void onSelect(boolean select);
    }
}

这里是xml

  <com.sofang.lxm.viewevent.RightView
        android:layout_width="50dp"
        android:layout_height="50dp" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值