欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義圓環(huán)式進(jìn)度條

 更新時(shí)間:2021年04月22日 15:30:12   作者:StrongDarkness  
這篇文章主要為大家詳細(xì)介紹了Android自定義圓環(huán)式進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

安卓自定義圓環(huán)式進(jìn)度條,供大家參考,具體內(nèi)容如下

需求是實(shí)現(xiàn)一個(gè)圓環(huán)式中間帶有進(jìn)度的進(jìn)度條,自己動(dòng)手實(shí)現(xiàn)一個(gè)

package com.djt.aienglish.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.djt.aienglish.R;


/**
 * @author qiu
 * @date 2020/3/12 13:51
 */
public class CirclePgBar extends View {
    private int mHeight = 0;
    private int mWidth = 0;

    // 畫圓環(huán)的畫筆
    private Paint mRingPaint;
    // 畫圓環(huán)的畫筆背景色
    private Paint mRingPaintBg;
    // 畫字體的畫筆
    private Paint mTextPaint;
    // 圓環(huán)顏色
    private int mRingColor;
    // 圓環(huán)背景顏色
    private int mRingBgColor;
    // 半徑
    private float mRadius;
    // 圓環(huán)半徑
    private float mRingRadius;
    // 圓環(huán)寬度
    private float mStrokeWidth;
    // 圓心x坐標(biāo)
    private int mXCenter;
    // 圓心y坐標(biāo)
    private int mYCenter;
    // 字的長(zhǎng)度
    private float mTxtWidth;
    // 字的高度
    private float mTxtHeight;
    // 總進(jìn)度
    private int max = 100;
    // 當(dāng)前進(jìn)度
    private int progress;
    private String text;

    public CirclePgBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 獲取自定義的屬性
        initAttrs(context, attrs);
        initVariable();
    }

    /**
     * 屬性
     */
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.TasksCompletedView, 0, 0);
        mStrokeWidth = typeArray.getDimension(R.styleable.TasksCompletedView_circleWidth, 0);
        mRingColor = typeArray.getColor(R.styleable.TasksCompletedView_ringColor, 0xFFFFFFFF);
        mRingBgColor = typeArray.getColor(R.styleable.TasksCompletedView_ringBgColor, 0xFFFFFFFF);
        text = typeArray.getString(R.styleable.TasksCompletedView_text);
        max = typeArray.getInteger(R.styleable.TasksCompletedView_max, 0);
        progress = typeArray.getInteger(R.styleable.TasksCompletedView_progress, 0);
    }

    /**
     * 初始化畫筆
     */
    private void initVariable() {
        //外圓弧背景
        mRingPaintBg = new Paint();
        mRingPaintBg.setAntiAlias(true);
        mRingPaintBg.setColor(mRingBgColor);
        mRingPaintBg.setStyle(Paint.Style.STROKE);
        mRingPaintBg.setStrokeWidth(mStrokeWidth);
        //外圓弧
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);
        //mRingPaint.setStrokeCap(Paint.Cap.ROUND);//設(shè)置線冒樣式,有圓 有方
        //中間字
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mRingColor);
        invalidate();
    }

    //測(cè)量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //實(shí)際測(cè)量寬高
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        if (mWidth > mHeight) {
            mRadius = mHeight / 2;
        } else {
            mRadius = mWidth / 2;
        }
        //半徑
        mRingRadius = mRadius - mStrokeWidth / 2;
        //文字寬高測(cè)量
        mTextPaint.setTextSize(mRadius / 2);
        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);
    }

    /**
     * 畫圖
     */
    @Override
    protected void onDraw(Canvas canvas) {
        mXCenter = mWidth / 2;
        mYCenter = mHeight / 2;
        //外圓弧背景
        RectF rectBg = new RectF(mXCenter - mRingRadius, mYCenter - mRingRadius, mXCenter + mRingRadius, mYCenter + mRingRadius);
        canvas.drawArc(rectBg, 0, 360, false, mRingPaintBg);
        //外圓弧//進(jìn)度
        if (progress > 0) {
            RectF oval = new RectF(mXCenter - mRingRadius, mYCenter - mRingRadius, mXCenter + mRingRadius, mYCenter + mRingRadius);
            canvas.drawArc(oval, -90, ((float) progress / max) * 360, false, mRingPaint);
        }
        //字體
        if(!TextUtils.isEmpty(text)) {
            mTxtWidth = mTextPaint.measureText(text, 0, text.length());
            canvas.drawText(text, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint);
        }
    }

    /**
     * 設(shè)置進(jìn)度
     *
     * @param progress
     */
    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();//重繪
    }

    /**
     * 設(shè)置最大值
     *
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
        postInvalidate();
    }

    /**
     * 設(shè)置文字內(nèi)容
     *
     * @param text
     */
    public void setText(String text) {
        this.text = text;
        postInvalidate();
    }
}

別忘記在value下的attr.xml中加入默認(rèn)配置屬性

<!--圓弧進(jìn)度條-->
    <declare-styleable name="TasksCompletedView">
        <attr name="circleWidth" format="dimension" />
        <attr name="ringColor" format="color" />
        <attr name="ringBgColor" format="color" />
        <attr name="text" format="string" />
        <attr name="progress" format="integer" />
     <attr name="max" format="integer" />
</declare-styleable>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論