Android自定義控件(入門級)--數字顯示器

需求
1.數字爲1位,顯示圓形
2.數字爲2位圖形拉伸,左右各半圓
3.數字大於999,顯示999+
4.自定義文字顏色,自定義背景色
效果(好吧,看起來挺low的)
自己並不複雜,不過做爲一道計算題仍是很不錯的

效果.png


1.自定義屬性
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--計數TextView-->
    <declare-styleable name="CountTextView">
        <attr name="z_ctv_font_size" format="reference|dimension"/>
        <attr name="z_ctv_num" format="integer"/>
        <attr name="z_bg_color" format="reference|color"/>
        <attr name="z_txt_color" format="reference|color"/>
    </declare-styleable>
</resources>
2.分析
使用圓角矩形來畫背景,Paint.getTextBounds來獲取文字邊界矩形
1).先繪製文字,將文字左頂點與屏幕左頂點重合
2).經過計算,畫出一個數時的圓角矩形兩個頂點(以下圖)
3).經過數字位數來控制圓角矩形兩頂點的X
4).經過畫布平移讓圓角矩形左頂點處於畫布頂點
5).計算圓角矩形的寬高,設置View大小

IMG20181203130904.jpg

public class CountTextView extends View {
    private int mCtvFontSize = sp(100);
    private int mCtvNum = 5;
    private int mCtvBgColor = 0xffBFF3F7;
    private int mCtvTxtColor = Color.WHITE;
    private Paint mPaint;//主畫筆
    private Paint mTxtPaint;
    private Rect mRect;
    private String mStr;
    private int mOffSet;

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

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

    public CountTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CountTextView);
        mCtvFontSize = (int) a.getDimension(R.styleable.CountTextView_z_ctv_font_size, mCtvFontSize);
        mCtvNum = a.getInteger(R.styleable.CountTextView_z_ctv_num, mCtvNum);
        mCtvTxtColor = a.getColor(R.styleable.CountTextView_z_txt_color, mCtvTxtColor);
        mCtvBgColor = a.getColor(R.styleable.CountTextView_z_bg_color, mCtvBgColor);
        a.recycle();
        init();
    }

    private void init() {
        //初始化主畫筆
        mTxtPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTxtPaint.setColor(mCtvTxtColor);
        mTxtPaint.setTextSize(mCtvFontSize);

        mRect = new Rect();
        mStr = mCtvNum + "";
        if (mCtvNum >= 1000) {
            mStr = "999+";
        }
        mTxtPaint.getTextBounds(mStr, 0, mStr.length(), mRect);
        int AChartLen = mRect.width() / mStr.length();
        mOffSet = (int) ((mStr.length() - 1) * AChartLen * 0.7f);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(mCtvBgColor);
        mPaint.setStrokeWidth(mRect.height());
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //文字左側距圓心的偏移
        int offsetX = mRect.height() - mRect.width() / 2 + mOffSet;
        setMeasuredDimension(2 * offsetX + mRect.width(), 2 * mRect.height());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        int offsetX = mRect.height() - mRect.width() / 2 + mOffSet;
        canvas.translate(offsetX, mRect.height() / 2);
        //圓角矩形左上點
        int topX = mRect.width() / 2 - mRect.height();
        int topY = -mRect.height() / 2;
        //圓角矩形右下點
        int bottomX = mRect.height() + mRect.width() / 2;
        int bottomY = mRect.height() / 2 + mRect.height();
        
        canvas.drawRoundRect(topX - mOffSet, topY, bottomX + mOffSet, bottomY,
                mRect.height(), mRect.height(), mPaint);

        canvas.drawText(mStr, 0, mRect.height(), mTxtPaint);
        canvas.restore();
    }

    private int sp(float sp) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }
}
3.使用
<com.toly1994.c.view.CountTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:z_ctv_font_size="40sp"
    app:z_ctv_num="30"/>
相關文章
相關標籤/搜索