Android超级好用的CheckView

前言

多选和单选是日常开发经常用的控件,android系统原生的控件样式(RadioButton 、CheckBox)不太好看,很多时候我们需要根据美工的设计自己实现CheckView。

我们需要自己准备两张图片。一张为checked状态,一张为unchecked状态。如果产品哥哥说你这个颜色不好看,我们又需要重新修改。基于以上原因,我们实现了自定义的CheckView,可以任意修改 颜色、大小,形状(圆心和方形),效果如下。

PIC.png

源码展示

自定义CheckView:

package com.xcheng.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;

import com.xcheng.view.R;
import com.xcheng.view.util.LocalDisplay;

import static com.xcheng.view.widget.ProgressView.TYPE_CIRCLE;

public class CheckView extends View implements Checkable {
    //圆框的颜色
    private int mStrokeWidth; // dp
    //圆框的尺寸
    private int mSize; // dp
    //圆框的颜色
    private int mStrokeColor;
    //圆框内填充的颜色
    private int mbgColor;
    //选中的颜色
    private int mCheckedColor;
    private boolean mChecked;
    private Paint mStrokePaint;
    private Paint mBackgroundPaint;
    private Drawable mCheckDrawable;
    private Rect mCheckRect;
    private Rect mBgRect;
    private Rect mStrokeRect;

    private int mType;

    public CheckView(Context context) {
        this(context, null);
    }

    public CheckView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CheckView, defStyleAttr, 0);
        mSize = ta.getDimensionPixelSize(R.styleable.CheckView_ev_size, LocalDisplay.dp2px(18));
        mType = ta.getInt(R.styleable.CheckView_ev_type, TYPE_CIRCLE);

        mStrokeWidth = ta.getDimensionPixelSize(R.styleable.CheckView_ev_strokeWidth, LocalDisplay.dp2px(2));
        mStrokeColor = ta.getColor(R.styleable.CheckView_ev_strokeColor, Color.parseColor("#c2c9cc"));
        mbgColor = ta.getColor(R.styleable.CheckView_ev_bgColor, Color.TRANSPARENT);
        mCheckedColor = ta.getColor(R.styleable.CheckView_ev_checkedColor, Color.parseColor("#0bd38a"));
        mChecked = ta.getBoolean(R.styleable.CheckView_android_checked, false);
        ta.recycle();

        mStrokePaint = new Paint();
        mStrokePaint.setAntiAlias(true);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        mStrokePaint.setStrokeWidth(mStrokeWidth);
        mStrokePaint.setColor(mStrokeColor);
        mCheckDrawable = ResourcesCompat.getDrawable(context.getResources(),
                R.drawable.ic_check_white_18dp, context.getTheme());

        mBackgroundPaint = new Paint();
        mBackgroundPaint.setAntiAlias(true);
        mBackgroundPaint.setStyle(Paint.Style.FILL);

        mCheckRect = getSquareRect(0);
        mBgRect = getSquareRect(mStrokeWidth);
        mStrokeRect = getSquareRect(mStrokeWidth / 2);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mSize + mStrokeWidth * 2 + getPaddingLeft() + getPaddingRight(),
                mSize + mStrokeWidth * 2 + getPaddingTop() + getPaddingBottom());
    }

    @Override
    public void setChecked(boolean checked) {
        mChecked = checked;
        invalidate();
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mChecked) {
            mBackgroundPaint.setColor(mCheckedColor);
            if (mType == ProgressView.TYPE_CIRCLE) {
                canvas.drawCircle(getPaddingLeft() + mStrokeWidth + mSize / 2,
                        getPaddingTop() + mStrokeWidth + mSize / 2,
                        mStrokeWidth + mSize / 2, mBackgroundPaint);
            } else {
                canvas.drawRect(mBgRect, mBackgroundPaint);
            }
            mCheckDrawable.setBounds(mCheckRect);
            mCheckDrawable.draw(canvas);
        } else {
            mBackgroundPaint.setColor(mbgColor);
            if (mType == ProgressView.TYPE_CIRCLE) {
                //渲染这个圆形背景
                canvas.drawCircle(getPaddingLeft() + mStrokeWidth + mSize / 2,
                        getPaddingTop() + mStrokeWidth + mSize / 2,
                        mSize / 2 + mStrokeWidth, mBackgroundPaint);
                //渲染这个圆形边框
                canvas.drawCircle(getPaddingLeft() + mStrokeWidth + mSize / 2,
                        getPaddingTop() + mStrokeWidth + mSize / 2,
                        (mStrokeWidth + mSize) / 2/*Paint.Style.STROKE的情况下画笔在StrokeWidth中间*/, mStrokePaint);
            } else {
                //渲染这个方形背景
                canvas.drawRect(mBgRect, mBackgroundPaint);
                //渲染这个方形边框
                canvas.drawRect(mStrokeRect, mStrokePaint);
            }
        }
        // enable hint
        setAlpha(isEnabled() ? 1.0f : 0.5f);
    }


    private Rect getSquareRect(@IntRange(from = 0) int offset) {
        int centerX = getPaddingLeft() + mStrokeWidth + mSize / 2;
        int centerY = getPaddingTop() + mStrokeWidth + mSize / 2;
        return new Rect(centerX - mSize / 2 - offset, centerY - mSize / 2 - offset,
                centerX + mSize / 2 + offset, centerY + mSize / 2 + offset);
    }
}

xml属性:

<declare-styleable name="CheckView">
    <attr name="android:checked" />
    <attr name="ev_type" />
    <attr name="ev_strokeColor" format="color" />
    <attr name="ev_bgColor" format="color" />
    <attr name="ev_checkedColor" format="color" />
    <attr name="ev_size" format="dimension" />
    <attr name="ev_strokeWidth" format="dimension" />
</declare-styleable>

使用:

<com.xcheng.view.widget.CheckView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:checked="false"
    app:ev_type="rect"
    app:ev_bgColor="@color/ev_white"
    app:ev_strokeColor="@color/ev_blue_normal"
    app:ev_checkedColor="@color/colorAccent"
    app:ev_size="30dp"
    app:ev_strokeWidth="6dp" />

是不是很简单,一劳永逸。再也不用担心修改样式了。

源码地址:https://github.com/xchengDroid/EasyView/blob/master/library/src/main/java/com/xcheng/view/widget/CheckView.java

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,000评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,745评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,561评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,782评论 1 298
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,798评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,394评论 1 310
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,952评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,852评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,409评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,483评论 3 341
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,615评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,303评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,979评论 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,470评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,571评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,041评论 3 377
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,630评论 2 359

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,303评论 25 707
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,745评论 2 59
  • 这世界,不只有漫山鲜花的村落;不只有古朴纯净的悠悠古镇;不只有一望无际的蔚蓝大海。还有,死亡与美丽同行的生命禁区,...
    change郑慧阅读 1,660评论 5 18
  • 随着年纪的增长,人们自我所思便愈加的少了。其原因在于自我思想浮沉于俗世之间,多了平日琐屑事物的困扰,去思考和探寻未...
    斯人如斯1阅读 753评论 0 1
  • 利润:销售收入与成本的差额 销售收入:菜品销售收入+商品销售收入+外卖销售收入+其他收入
    爱上小炒阅读 412评论 0 0