Android自定義圓環(huán)式進(jìn)度條
安卓自定義圓環(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í)有所幫助,也希望大家多多支持腳本之家。
- android自定義進(jìn)度條漸變色View的實(shí)例代碼
- Android中實(shí)現(xiàn)Webview頂部帶進(jìn)度條的方法
- android ListView和ProgressBar(進(jìn)度條控件)的使用方法
- Android自定義View實(shí)現(xiàn)漸變色進(jìn)度條
- Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條
- Android自定義View實(shí)現(xiàn)帶數(shù)字的進(jìn)度條實(shí)例代碼
- Android Webview添加網(wǎng)頁(yè)加載進(jìn)度條實(shí)例詳解
- Android自定義View實(shí)現(xiàn)水平帶數(shù)字百分比進(jìn)度條
- Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條
- Android自定義view實(shí)現(xiàn)圓環(huán)進(jìn)度條效果
相關(guān)文章
Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android仿QQ個(gè)人標(biāo)簽添加與刪除功能
這篇文章主要為大家詳細(xì)介紹了Android仿QQ個(gè)人標(biāo)簽添加與刪除功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android編程使用LinearLayout和PullRefreshView實(shí)現(xiàn)上下翻頁(yè)功能的方法
這篇文章主要介紹了Android編程使用LinearLayout和PullRefreshView實(shí)現(xiàn)上下翻頁(yè)功能的方法,涉及Android界面布局與邏輯處理相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
android實(shí)現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫(kù)代碼
這篇文章主要介紹了android實(shí)現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫(kù)代碼,有需要的朋友可以參考一下2013-12-12
Android實(shí)現(xiàn)瘋狂連連看游戲之開發(fā)游戲界面(二)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)瘋狂連連看游戲之開發(fā)游戲界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

