溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在Android中自定義一個圓環(huán)式進(jìn)度條

發(fā)布時間:2021-04-30 15:31:40 來源:億速云 閱讀:175 作者:Leah 欄目:開發(fā)技術(shù)

如何在Android中自定義一個圓環(huán)式進(jìn)度條?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

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;



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;
    // 字的長度
    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();
    }

    //測量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //實(shí)際測量寬高
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        if (mWidth > mHeight) {
            mRadius = mHeight / 2;
        } else {
            mRadius = mWidth / 2;
        }
        //半徑
        mRingRadius = mRadius - mStrokeWidth / 2;
        //文字寬高測量
        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>

看完上述內(nèi)容,你們掌握如何在Android中自定義一個圓環(huán)式進(jìn)度條的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI