Android 自定義View - ProgressViewDemo(帶文本)

效果:

image

ProgressView

public class ProgressView extends View {

    private Paint mBackGroundPaint;//背景色畫筆
    private Paint mArcPaint;//進(jìn)度畫筆
    private Paint mTextPaint;//中間文本的畫筆

    private int mProgress = 0;

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

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

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    /**
     * 初始化
     */
    public void initPaint() {
        mBackGroundPaint = new Paint();
        mBackGroundPaint.setColor(Color.BLACK);
        mBackGroundPaint.setAntiAlias(true);
        mBackGroundPaint.setStrokeWidth(5);
        mBackGroundPaint.setStyle(Paint.Style.STROKE);

        mArcPaint = new Paint();
        mArcPaint.setColor(Color.RED);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStrokeWidth(10);
        mArcPaint.setStyle(Paint.Style.STROKE);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.RED);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(30);
        mTextPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //繪制底部圓圈
        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, 150, mBackGroundPaint);
        //繪制進(jìn)度的圓弧
        canvas.drawArc(new RectF(getMeasuredWidth() / 2 - 150, getMeasuredHeight() / 2 - 150,
                        getMeasuredWidth() / 2 + 150, getMeasuredHeight() / 2 + 150), 0,
                360 * mProgress / 100, false, mArcPaint);
        //繪制中間進(jìn)度文本
        String text = mProgress + " %";
        //使文本居中擺放
        Rect txtRect = new Rect();
        mTextPaint.getTextBounds(text, 0, text.length(), txtRect);
        canvas.drawText(text, getMeasuredWidth() / 2 - txtRect.width() / 2, getMeasuredHeight() / 2 + txtRect.height() / 2, mTextPaint);
    }

    /**
     * 開始進(jìn)度
     */
    public void startProgress() {
        mProgress = 0;
        mHandler.sendEmptyMessageDelayed(0, 10);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (mProgress < 100) {
                    mProgress++;
                    invalidate();
                    sendEmptyMessageDelayed(0, 10);
                }
            }
        }
    };
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.progressviewdemo.MainActivity">

    <com.example.progressviewdemo.ProgressView
        android:id="@+id/progressView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        progressView.setOnClickListener({ progressView.startProgress() })
    }
}

傳送門:
https://github.com/Urwateryi/ProgressViewDemo.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容