自定义CheckBox

2020-12-04  本文已影响0人  张岱熹

还在为了系统控件不符合设计图样式而烦恼吗?

也许是iOS中CheckBox比安卓设计好看,设计师在设计效果图时候,总是依据iOS样式来设计,这就导致安卓原生控件很尴尬不能使用,那么我就需要自定义一下比较实用的控件来使用.废话不多说现在开始自定义CheckBox;

1.res-values-新建attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCheckBox" >
        <!--默认图片-->
        <attr name="checkNormal" format="reference"/>
        <!--选中图片-->
        <attr name="checkFocus" format="reference"/>
        <!--选中切换-->
        <attr name="isCheck" format="boolean"/>
        <!--是否可以选中切换-->
        <attr name="isClick" format="boolean"/>

    </declare-styleable>
</resources>

2.创建MyCheckBox:

public class MyCheckBox extends View {
    private Bitmap mStartNomal;
    private Bitmap mStartFocus;

    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
        invalidate();
    }

    private boolean isSelect=true;
    private boolean isClick=true;
    public MyCheckBox(Context context) {
        this(context,null);
    }

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

    public MyCheckBox(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.MyCheckBox);
        int image1=typedArray.getResourceId(R.styleable.MyCheckBox_checkNormal,0);
        int image2=typedArray.getResourceId(R.styleable.MyCheckBox_checkFocus,0);
        isSelect=  typedArray.getBoolean(R.styleable.MyCheckBox_isCheck,isSelect);
        isClick=  typedArray.getBoolean(R.styleable.MyCheckBox_isClick,isClick);
        mStartNomal= BitmapFactory.decodeResource(getResources(),image1);
        mStartFocus= BitmapFactory.decodeResource(getResources(),image2);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //图片的高度
        int height=mStartNomal.getHeight();
        int wight=mStartNomal.getWidth();
        setMeasuredDimension(wight,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mStartFocus!=null){
            if (isSelect){
                canvas.drawBitmap(mStartFocus,0,0,null);
            }else{
                canvas.drawBitmap(mStartNomal,0,0,null);
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN://按下
                if (isClick){
                    isSelect=!isSelect;
                    invalidate();
                }
            case MotionEvent.ACTION_MOVE://移动

            case MotionEvent.ACTION_UP://抬起

        }
        return super.onTouchEvent(event);
    }
}

3.使用

<com.lnkj.yiguo.widget.MyCheckBox
    app:isCheck="选中状态"
    app:isClick="是否可以切换选中"
    app:checkFocus="@mipmap/未选中资源文件"
    app:checkNormal="@mipmap/选中资源文件"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

4.更多动画效果添加,那就根据个人需求添加.这边只写出一个基础选中效果;

如果想偷个懒,那么你出钱,我们出力.定制专属于你的APP请咨询我们:山东六牛网络科技有限公司

上一篇下一篇

猜你喜欢

热点阅读