android自定義進(jìn)度條漸變圓形
在安全衛(wèi)生上,經(jīng)??吹接袌A形的進(jìn)度條在轉(zhuǎn)動,效果非常好看,于是就嘗試去實(shí)現(xiàn)一下,具體實(shí)現(xiàn)過程不多說了,直接上效果圖,先炫耀下。
效果圖:

分析:比較常見于掃描結(jié)果、進(jìn)度條等場景
利用canvas.drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)繪制圓弧
Paint的一些屬性定義粗細(xì)、顏色、樣式等
LinearGradient實(shí)現(xiàn)顏色的線型漸變
同樣的道理,可以畫出長條進(jìn)度條,扇圖餅圖等,感興趣可以試下..
package com.liujing.progressviewdemo;
/***
* 自定義圓弧進(jìn)度條
*
* @author liujing
*/
public class ProgressView extends View {
//分段顏色
private static final int[] SECTION_COLORS = { Color.GREEN, Color.YELLOW,
Color.RED };
private static final String[] ALARM_LEVEL = { "安全", "低危", "中危", "高危" };
private float maxCount;
private float currentCount;
private int score;
private String crrentLevel;
private Paint mPaint;
private Paint mTextPaint;
private int mWidth, mHeight;
public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public ProgressView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ProgressView(Context context) {
this(context, null);
}
private void init(Context context) {
mPaint = new Paint();
mTextPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initPaint();
RectF rectBlackBg = new RectF(20, 20, mWidth - 20, mHeight - 20);
canvas.drawArc(rectBlackBg, 0, 360, false, mPaint);
mPaint.setColor(Color.BLACK);
canvas.drawText(score + "分", mWidth / 2, mHeight / 2, mTextPaint);
mTextPaint.setTextSize(40);
if (crrentLevel != null) {
canvas.drawText(crrentLevel, mWidth / 2, mHeight / 2 + 50,
mTextPaint);
}
float section = currentCount / maxCount;
if (section <= 1.0f / 3.0f) {
if (section != 0.0f) {
mPaint.setColor(SECTION_COLORS[0]);
} else {
mPaint.setColor(Color.TRANSPARENT);
}
} else {
int count = (section <= 1.0f / 3.0f * 2) ? 2 : 3;
int[] colors = new int[count];
System.arraycopy(SECTION_COLORS, 0, colors, 0, count);
float[] positions = new float[count];
if (count == 2) {
positions[0] = 0.0f;
positions[1] = 1.0f - positions[0];
} else {
positions[0] = 0.0f;
positions[1] = (maxCount / 3) / currentCount;
positions[2] = 1.0f - positions[0] * 2;
}
positions[positions.length - 1] = 1.0f;
LinearGradient shader = new LinearGradient(3, 3, (mWidth - 3)
* section, mHeight - 3, colors, null,
Shader.TileMode.MIRROR);
mPaint.setShader(shader);
}
canvas.drawArc(rectBlackBg, 180, section * 360, false, mPaint);
}
private void initPaint() {
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth((float) 40.0);
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeCap(Cap.ROUND);
mPaint.setColor(Color.TRANSPARENT);
mTextPaint.setAntiAlias(true);
mTextPaint.setStrokeWidth((float) 3.0);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTextSize(50);
mTextPaint.setColor(Color.BLACK);
}
private int dipToPx(int dip) {
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
public int getScore() {
return score;
}
public String getCrrentLevel() {
return crrentLevel;
}
public void setCrrentLevel(String crrentLevel) {
this.crrentLevel = crrentLevel;
}
public float getMaxCount() {
return maxCount;
}
public float getCurrentCount() {
return currentCount;
}
public void setScore(int score) {
this.score = score;
if (score == 100) {
this.crrentLevel = ALARM_LEVEL[0];
} else if (score >= 70 && score < 100) {
this.crrentLevel = ALARM_LEVEL[1];
} else if (score >= 30 && score < 70) {
this.crrentLevel = ALARM_LEVEL[2];
} else {
this.crrentLevel = ALARM_LEVEL[3];
}
invalidate();
}
/***
* 設(shè)置最大的進(jìn)度值
*
* @param maxCount
*/
public void setMaxCount(float maxCount) {
this.maxCount = maxCount;
}
/***
* 設(shè)置當(dāng)前的進(jìn)度值
*
* @param currentCount
*/
public void setCurrentCount(float currentCount) {
this.currentCount = currentCount > maxCount ? maxCount : currentCount;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.EXACTLY
|| widthSpecMode == MeasureSpec.AT_MOST) {
mWidth = widthSpecSize;
} else {
mWidth = 0;
}
if (heightSpecMode == MeasureSpec.AT_MOST
|| heightSpecMode == MeasureSpec.UNSPECIFIED) {
mHeight = dipToPx(15);
} else {
mHeight = heightSpecSize;
}
setMeasuredDimension(mWidth, mHeight);
}
}
Demo:http://xiazai.jb51.net/201507/yuanma/ProgressViewDemo(jb51.net).rar
以上內(nèi)容就是實(shí)現(xiàn)android自定義進(jìn)度條漸變圓形的代碼,希望對大家有所幫助。
- Android編程之ProgressBar圓形進(jìn)度條顏色設(shè)置方法
- Android三種方式實(shí)現(xiàn)ProgressBar自定義圓形進(jìn)度條
- Android帶進(jìn)度的圓形進(jìn)度條
- Android自定義圓形倒計(jì)時(shí)進(jìn)度條
- Android 自定義圓形帶刻度漸變色的進(jìn)度條樣式實(shí)例代碼
- Android studio圓形進(jìn)度條 百分?jǐn)?shù)跟隨變化
- 自定義Android圓形進(jìn)度條(附源碼)
- Android使用Canvas繪制圓形進(jìn)度條效果
- Android實(shí)現(xiàn)漸變圓環(huán)、圓形進(jìn)度條效果
- android自定義等級評分圓形進(jìn)度條
相關(guān)文章
Android LinearLayout實(shí)現(xiàn)自動換行效果
這篇文章主要為大家詳細(xì)介紹了Android LinearLayout實(shí)現(xiàn)自動換行效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android UI控件之Gallery實(shí)現(xiàn)拖動式圖片瀏覽效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件之Gallery實(shí)現(xiàn)拖動式圖片瀏覽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android 3D滑動菜單完全解析 Android實(shí)現(xiàn)推拉門式的立體特效
這篇文章主要為大家詳細(xì)介紹了Android 3D滑動菜單,Android實(shí)現(xiàn)推拉門式的立體特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android編程簡單實(shí)現(xiàn)雷達(dá)掃描效果
這篇文章主要介紹了Android編程簡單實(shí)現(xiàn)雷達(dá)掃描效果,涉及Android圖形繪制及顯示的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能
這篇文章主要介紹了基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能,介紹了如何錄音,如何播放本地和遠(yuǎn)程音頻文件,以及如何實(shí)現(xiàn)動畫,在錄制完音頻文件后如何上傳,這些都是我們平常使用這個功能會遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-05-05
詳解 android 光線傳感器 light sensor的使用
這篇文章主要介紹了詳解 android 光線傳感器 light sensor的使用的相關(guān)資料,需要的朋友可以參考下2017-06-06

