公司的一個項目需要實現血壓計的效果,我翻了網上的許多文章大多是通過圖片實現的,后來我看了看,自己研究了一下,用筆刷,畫矩形這樣也是可以做到的。這樣做的好處是明顯減少本身app的大小,降低各模塊之間的耦合度。
具體思路:
1.進度條,其實就是一個最大值和最小值的比例值;
2.drawRoundRect 畫圓角矩形
3.LinearGradient對象渲染,具體渲染的比例要自己計算,目前我的demo提供3中顏色渲染,就是超過三分之一時是另外一種顏色。
關鍵代碼:
public class ProgressView extends View {
private static final int[] SECTION_COLORS = {Color.RED, Color.YELLOW, Color.GREEN};
private float maxCount;
private float currentCount;
private Paint mPaint;
private int mWidth, mHeight;
public ProgressView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ProgressView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint = new Paint();
mPaint.setAntiAlias(true);
int round = mHeight / 2;
mPaint.setColor(Color.rgb(71, 76, 80));
RectF rectBg = new RectF(0, 0, mWidth, mHeight);
canvas.drawRoundRect(rectBg, round, round, mPaint);
mPaint.setColor(Color.GRAY);
RectF rectBlackBg = new RectF(2, 2, mWidth - 2, mHeight - 2);
canvas.drawRoundRect(rectBlackBg, round, round, mPaint);
float section = currentCount / maxCount;
RectF rectProgressBg = new RectF(3, (mHeight - 3) * (1.0f - section), mWidth - 3, mHeight - 3);
if (section <= 1.0f / 3.0f) {
if (section != 0.0f) {
mPaint.setColor(SECTION_COLORS[2]);
} else {
mPaint.setColor(Color.TRANSPARENT);
}
} else {
int count = (section <= 1.0f / 3.0f * 2) ? 2 : 3;
int[] colors = new int[count];
float[] positions = new float[count];
if (count == 2) {
positions[0] = 0.0f;
positions[1] = 1.0f - positions[0];
//因為是豎著的進度條,所以需要從原數組的第二個位置開始
System.arraycopy(SECTION_COLORS, 1, colors, 0, count);
} else {
positions[0] = 0.0f;
positions[1] = (maxCount / 3) / currentCount;
positions[2] = 1.0f - positions[0] * 2;
System.arraycopy(SECTION_COLORS, 0, colors, 0, count);
}
positions[positions.length - 1] = 1.0f;
LinearGradient shader = new LinearGradient(3, (mHeight - 3) * (1.0f - section), mWidth - 3, mHeight - 3, colors, null, Shader.TileMode.MIRROR);
mPaint.setShader(shader);
}
canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
}
private int dipToPx(int dip) {
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
public void setMaxCount(float maxCount) {
this.maxCount = maxCount;
}
public void setCurrentCount(float currentCount) {
this.currentCount = currentCount > maxCount ? maxCount : currentCount;
invalidate();
}
public float getMaxCount() {
return maxCount;
}
public float getCurrentCount() {
return currentCount;
}
@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下載
具體效果如圖:
QQ圖片20170505161909.jpg