Android自定義View圓形百分比控件(一)
做一個(gè)自定義View的小練習(xí),效果如下

只需要畫(huà)一個(gè)圓、一個(gè)圓弧、一個(gè)百分比文本,添加一個(gè)點(diǎn)擊事件,傳入百分比重繪
1、在res/values文件夾下新建attrs.xml文件,編寫(xiě)自定義屬性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CirclePercentView" >
<attr name="circleBg" format="color"/>
<attr name="arcColor" format="color"/>
<attr name="arcWidth" format="dimension"/>
<attr name="percentTextColor" format="color"/>
<attr name="percentTextSize" format="dimension"/>
<attr name="radius" format="dimension"/>
</declare-styleable>
</resources>
2、新建CirclePercentView繼承View,重寫(xiě)構(gòu)造方法:
public CirclePercentView(Context context) {
this(context, null);
}
public CirclePercentView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CirclePercentView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
3、在第三個(gè)構(gòu)造方法中獲取自定義屬性的值:
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CirclePercentView, defStyleAttr, 0);
mCircleColor = ta.getColor(R.styleable.CirclePercentView_circleBg, 0xff8e29fa);
mArcColor = ta.getColor(R.styleable.CirclePercentView_arcColor, 0xffffee00);
mArcWidth = ta.getDimensionPixelSize(R.styleable.CirclePercentView_arcWidth, DensityUtils.dp2px(context, 16));
mPercentTextColor = ta.getColor(R.styleable.CirclePercentView_arcColor, 0xffffee00);
mPercentTextSize = ta.getDimensionPixelSize(R.styleable.CirclePercentView_percentTextSize, DensityUtils.sp2px(context, 16));
mRadius = ta.getDimensionPixelSize(R.styleable.CirclePercentView_radius, DensityUtils.dp2px(context, 100));
ta.recycle();
4、創(chuàng)建畫(huà)圖所使用的對(duì)象,如Paint、Rect、RectF:
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setStyle(Paint.Style.FILL);
mCirclePaint.setColor(mCircleColor);
mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mArcPaint.setStyle(Paint.Style.STROKE);
mArcPaint.setStrokeWidth(mArcWidth);
mArcPaint.setColor(mArcColor);
mArcPaint.setStrokeCap(Paint.Cap.ROUND);//使圓弧兩頭圓滑
mPercentTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPercentTextPaint.setStyle(Paint.Style.STROKE);
mPercentTextPaint.setColor(mPercentTextColor);
mPercentTextPaint.setTextSize(mPercentTextSize);
mArcRectF = new RectF();//圓弧的外接矩形
mTextBound = new Rect();//文本的范圍矩形
5、重寫(xiě)onMeasure()方法,計(jì)算自定義View的寬高:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureDimension(widthMeasureSpec), measureDimension(heightMeasureSpec));
}
private int measureDimension(int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {//精確地,代表寬高為定值或者match_parent時(shí)
result = specSize;
} else {
result = 2 * mRadius;
if (specMode == MeasureSpec.AT_MOST) {//最大地,代表寬高為wrap_content時(shí)
result = Math.min(result, specSize);
}
}
return result;
}
6、重寫(xiě)onDraw()方法,繪制圓、圓弧和百分比文本,注意坐標(biāo)的計(jì)算:

@Override
protected void onDraw(Canvas canvas) {
//畫(huà)圓
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mRadius, mCirclePaint);
//畫(huà)圓弧
mArcRectF.set(getWidth() / 2 - mRadius + mArcWidth / 2, getHeight() / 2 - mRadius + mArcWidth / 2
, getWidth() / 2 + mRadius - mArcWidth / 2, getHeight() / 2 + mRadius - mArcWidth / 2);
canvas.drawArc(mArcRectF, 270, 360 * mCurPercent / 100, false, mArcPaint);
String text = mCurPercent + "%";
//計(jì)算文本寬高
mPercentTextPaint.getTextBounds(text, 0, String.valueOf(text).length(), mTextBound);
//畫(huà)百分比文本
canvas.drawText(text, getWidth() / 2 - mTextBound.width() / 2
, getHeight() / 2 + mTextBound.height() / 2, mPercentTextPaint);
}
7、給這個(gè)view設(shè)置點(diǎn)擊事件,暴露一個(gè)動(dòng)態(tài)設(shè)置百分比的方法:
public void setCurPercent(float curPercent) {
ValueAnimator anim = ValueAnimator.ofFloat(mCurPercent, curPercent);
//動(dòng)畫(huà)時(shí)長(zhǎng)由百分比大小決定
anim.setDuration((long) (Math.abs(mCurPercent - curPercent) * 20));
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (float) animation.getAnimatedValue();
mCurPercent = (float) (Math.round(value * 10)) / 10;//四舍五入保留到小數(shù)點(diǎn)后兩位
invalidate();//重繪,重走onDraw()方法,這也是不能再onDraw()中創(chuàng)建對(duì)象的原因
}
});
anim.start();
}
public void setOnCircleClickListener(OnClickListener onClickListener) {
this.mOnClickListener = onClickListener;
}
//在構(gòu)造方法中
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mOnClickListener != null) {
mOnClickListener.onClick(CirclePercentView.this);
}
}
});
8、在activity_main.xml布局文件中使用該View:
<!-- 使用自定義命名空間namespace,cpv是該控件名的首字母縮寫(xiě),可以隨意取 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cpv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- cpv后面的屬性名對(duì)應(yīng)attrs.xml文件中的自定義屬性名 -->
<com.monkey.customviewdemo.view.CirclePercentView
android:id="@+id/circlePercentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
cpv:arcColor="#ffee00"
cpv:arcWidth="@dimen/activity_horizontal_margin"
cpv:circleBg="#8e29fa"
cpv:percentTextColor="#ffee00"
cpv:percentTextSize="16sp"
cpv:radius="100dp" />
</RelativeLayout>
9、在MainActivity.java中設(shè)置監(jiān)聽(tīng),傳入百分比:
mCirclePercentView = (CirclePercentView) findViewById(R.id.circlePercentView);
mCirclePercentView.setOnCircleClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float percent = (float) (Math.random() * 99 + 1);
mCirclePercentView.setCurPercent(percent);
}
});
代碼下載地址:
https://github.com/MonkeyMushroom/CirclePercentView/tree/master
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter 底部彈窗如何實(shí)現(xiàn)多項(xiàng)選擇
在Flutter中提供了一個(gè)showModelBottomSheet方法用于彈出底部彈窗,本篇基于這個(gè)方法介紹實(shí)現(xiàn)底部彈窗多選的思路和方式。2021-06-06
Android開(kāi)發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解
這篇文章主要介紹了Android開(kāi)發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android使用 Coroutine + Retrofit打造簡(jiǎn)單的HTTP請(qǐng)求庫(kù)
這篇文章主要介紹了Android使用 Coroutine + Retrofit打造簡(jiǎn)單的HTTP請(qǐng)求庫(kù),幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android編程動(dòng)態(tài)按鈕實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程動(dòng)態(tài)按鈕實(shí)現(xiàn)方法,分享了onTouch方法及xml調(diào)用兩種實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
獲取Android手機(jī)中所有短信的實(shí)現(xiàn)代碼
這篇文章主要介紹了獲取Android手機(jī)中所有短信的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-08-08
Android usb設(shè)備權(quán)限查詢(xún)及自動(dòng)獲取詳解流程
本篇文章介紹了我想要獲取Android系統(tǒng)usb設(shè)備使用權(quán)限時(shí)遇到的問(wèn)題,以及解決該問(wèn)題的過(guò)程及思路,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-10-10
Mac OS下為Android Studio編譯FFmpeg解碼庫(kù)的詳細(xì)教程
這篇文章主要介紹了Mac OS下為Android Studio編譯FFmpeg解碼庫(kù)的詳細(xì)教程,包括NDK的配置和Android Studio的配置兩個(gè)部分的內(nèi)容,需要的朋友可以參考下2016-01-01

