Android 自定義六邊形進度條
阿新 • • 發佈:2019-01-26
最近公司專案中冒出個六邊形的進度條,當時看到有點蒙,和大多數人一樣不知道從哪開始寫,於是和大多數人一樣抱著學習的態度百度了一番,但並沒有具體的例項。但是,也不是沒收穫。在網上找到一篇圓形進度條的案例,我想既然有圓形的,六邊形的我也可以做啊!不就是模仿麼!於是開始了閱讀程式碼,連結是:http://blog.csdn.net/xiaanming/article/details/10298163。第一次寫部落格所以我就直接貼程式碼了,程式碼中的註釋很詳細,我就不過多解釋了。
效果圖如下:
主要程式碼:
package com.sxc.hexagonprogress;
import java.util.Random;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* 六邊形帶進度的進度條,執行緒安全的View,可直接線上程中更新進度
*
* @author sunxunchao
*
*/
public class HexagonProgress extends View {
/**
* 畫筆物件的引用
*/
private Paint paint, mPaint;
/**
* 畫筆路徑
*/
private Path path, mPath;
/**
* 環的顏色
*/
private int roundColor;
/**
* 環進度的顏色
*/
private int roundProgressColor;
/**
* 中間進度百分比的字串的顏色
*/
private int textColor;
/**
* 中間進度百分比的字串的字型
*/
private float textSize;
/**
* 環的寬度
*/
private float roundWidth;
/**
* 最大進度
*/
private double max;
/**
* 當前進度
*/
private double progress;
/**
* 是否顯示中間的進度
*/
private boolean textIsDisplayable;
/**
* 進度的風格,實心或者空心
*/
private int style;
public static final int STROKE = 0;
public static final int FILL = 1;
public HexagonProgress(Context context) {
this(context, null);
}
public HexagonProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HexagonProgress(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
mPaint = new Paint();
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.HexagonProgressBar);
// 獲取自定義屬性和預設值
roundColor = mTypedArray.getColor(
R.styleable.HexagonProgressBar_hexagonColor, Color.RED);
roundProgressColor = mTypedArray.getColor(
R.styleable.HexagonProgressBar_hexagonProgressColor, Color.GREEN);
textColor = mTypedArray.getColor(
R.styleable.HexagonProgressBar_textColor, Color.GREEN);
textSize = mTypedArray.getDimension(
R.styleable.HexagonProgressBar_textSize, 15);
roundWidth = mTypedArray.getDimension(
R.styleable.HexagonProgressBar_hexagonWidth, 5);
max = mTypedArray.getInteger(R.styleable.HexagonProgressBar_max, 100);
textIsDisplayable = mTypedArray.getBoolean(
R.styleable.HexagonProgressBar_textIsDisplayable, true);
mTypedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 畫外六邊形
*/
int centre = getWidth() / 2;// 中心座標
int radius = (int) (centre - roundWidth / 2);// 六邊形邊長
mPaint.setColor(roundColor); // 設定圓環的顏色
mPaint.setStyle(Paint.Style.STROKE); // 設定空心
mPaint.setStrokeWidth(roundWidth); // 設定圓環的寬度
mPaint.setAntiAlias(true); // 消除鋸齒
mPath = new Path();//設定路徑
// 第一個點座標(centre-radius, getHeight()/2)
// 第二個點座標(centre-radius/2,getHeight()/2-Math.sqrt(3)*radius/2)
// 第三個點座標(centre+radius/2,getHeight()/2-Math.sqrt(3)*radius/2)
// 第四個點座標(centre+radius,getHeight()/2)
// 第五個點座標 (centre+radius/2,Math.sqrt(3)*radius/2+getHeight()/2)
// 第六個點座標 (centre-radius/2,Math.sqrt(3)*radius/2+getHeight()/2)
mPath.moveTo(centre - radius, centre); // A
mPath.lineTo(centre - radius / 2, (float) (centre - Math.sqrt(3)* radius / 2));// B
mPath.lineTo(centre + radius / 2, (float) (centre - Math.sqrt(3)* radius / 2));// C
mPath.lineTo(centre + radius, centre);// D
mPath.lineTo(centre + radius / 2,(float) ((Math.sqrt(3) * radius / 2) + centre));// E
mPath.lineTo(centre - radius / 2,(float) ((Math.sqrt(3) * radius / 2) + centre));// F
mPath.close();
canvas.drawPath(mPath, mPaint);
/**
* 畫進度百分比
*/
mPaint.setStrokeWidth(0);
mPaint.setColor(textColor);
mPaint.setTextSize(textSize);
mPaint.setTypeface(Typeface.DEFAULT_BOLD); // 設定字型
int percent = (int) (((float) progress / (float) max) * 100); // 中間的進度百分比,先轉換成float在進行除法運算,不然都為0
float textWidth = mPaint.measureText(percent + "%"); // 測量字型寬度,我們需要根據字型的寬度設定在圓環中間
if (textIsDisplayable && style == STROKE) {
canvas.drawText(percent + "%", centre - textWidth / 2, centre
+ textSize / 2, mPaint); // 畫出進度百分比
}
/**
* 畫六邊形進度
*/
path = new Path();
paint.setStrokeWidth(roundWidth); // 設定圓環的寬度
paint.setColor(roundProgressColor); // 設定進度的顏色
paint.setAntiAlias(true);
double k= (progress*6) / max;
paint.setStyle(Paint.Style.STROKE);
if (k <= 1|| k==0) {
path.moveTo(centre + radius, centre);
path.lineTo((float)(centre+radius-k*radius/2), (float) (centre+k*radius*Math.sqrt(3)/2));
}
else if (k>1&&k<=2) {
path.moveTo(centre + radius, centre);
path.lineTo(centre + radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo((float) (centre+1.5*radius-k*radius), (float) (centre+0.5*Math.sqrt(3)*radius));
}else if (k>2&&k<=3) {
path.moveTo(centre + radius, centre);
path.lineTo(centre + radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre - radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo((float)(centre+0.5*radius-0.5*radius*k), (float) (centre+1.5*Math.sqrt(3)*radius-0.5*k*radius*Math.sqrt(3)));
}else if (k>3&&k<=4) {
path.moveTo(centre + radius, centre);
path.lineTo(centre + radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre - radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre-radius, centre);
path.lineTo((float)(centre-radius+0.5*k*radius-1.5*radius), (float) (centre-0.5*(k-3)*radius*Math.sqrt(3)));
}else if (k>4&&k<=5) {
path.moveTo(centre + radius, centre);
path.lineTo(centre + radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre - radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre - radius, centre);
path.lineTo(centre - radius / 2, (float) (centre - Math.sqrt(3)* radius / 2));
path.lineTo((float) ((k-4)*radius+centre-0.5*radius),(float) (centre - Math.sqrt(3)* radius / 2));
}else if (k>5&&k<6) {
path.moveTo(centre + radius, centre);
path.lineTo(centre + radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre - radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre - radius, centre);
path.lineTo(centre - radius / 2, (float) (centre - Math.sqrt(3)* radius / 2));
path.lineTo(centre + radius / 2,(float) (centre - Math.sqrt(3)* radius / 2));
path.lineTo((float)(centre+0.5*radius+0.5*(k-5)*radius),(float) (centre-0.5*Math.sqrt(3)*radius+0.5*Math.sqrt(3)*(k-5)*radius));
}else {
path.moveTo(centre + radius, centre);
path.lineTo(centre + radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre - radius / 2, (float) ((Math.sqrt(3) * radius / 2) + centre));
path.lineTo(centre - radius, centre);
path.lineTo(centre - radius / 2, (float) (centre - Math.sqrt(3)* radius / 2));
path.lineTo(centre + radius / 2,(float) (centre - Math.sqrt(3)* radius / 2));
path.lineTo(centre + radius , centre);
path.close();
}
canvas.drawPath(path, paint);
}
public synchronized double getMax() {
return max;
}
/**
* 設定進度的最大值
*
* @param max
*/
public synchronized void setMax(int max) {
if (max < 0) {
throw new IllegalArgumentException("max not less than 0");
}
this.max = max;
}
/**
* 獲取進度.需要同步
*
* @return
*/
public synchronized double getProgress() {
return progress;
}
/**
* 設定進度,此為執行緒安全控制元件,由於考慮多線的問題,需要同步 重新整理介面呼叫postInvalidate()能在非UI執行緒重新整理
*
* @param progress
*/
public synchronized void setProgress(double progress) {
if (progress < 0) {
throw new IllegalArgumentException("progress not less than 0");
}
if (progress > max) {
progress = max;
}
if (progress <= max) {
this.progress = progress;
postInvalidate();
}
}
public int getCricleColor() {
return roundColor;
}
public void setCricleColor(int cricleColor) {
this.roundColor = cricleColor;
}
public int getCricleProgressColor() {
return roundProgressColor;
}
public void setCricleProgressColor(int cricleProgressColor) {
this.roundProgressColor = cricleProgressColor;
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public float getTextSize() {
return textSize;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
}
public float getRoundWidth() {
return roundWidth;
}
public void setRoundWidth(float roundWidth) {
this.roundWidth = roundWidth;
}
}
在values中新建一個attrs.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="HexagonProgressBar">
<attr name="hexagonColor" format="color"/>
<attr name="hexagonProgressColor" format="color"/>
<attr name="hexagonWidth" format="dimension"></attr>
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="max" format="integer"></attr>
<attr name="textIsDisplayable" format="boolean"></attr>
<!-- <attr name="style">
<enum name="STROKE" value="0"></enum>
<enum name="FILL" value="1"></enum>
</attr> -->
</declare-styleable>
</resources>