![](https://i-blog.csdnimg.cn/blog_migrate/b7c6ac347d0ce592fda43cf4f38d2c9c.png)
package com.inks.jisaiclothesford.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import com.inks.inkslibrary.utils.L;
import com.inks.jisaiclothesford.R;
import com.inks.jisaiclothesford.data.Datas;
/**
* Created by inks on 2018/9/21 0021.
*/
public class MyProgress extends View {
private Paint mPaint;//文字的画笔
private Paint bgPaint;//
private Paint circlePaint;//
public static Typeface typeFace;
private int specWidthSize,specHeightSize ;
private String textString = " ";
private float textSize;
private int bgColor,textColor,circleColor;
private int maxPro = 100;
private int pro = 50;
private float onePro;//每一份的宽度
public void setPro(int pro){
this.pro = pro;
invalidate();
}
public MyProgress(Context context) {
this(context, null);
}
public MyProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyProgress(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public MyProgress(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/" + Datas.logoFont);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyProgress);
textSize =typedArray.getDimension(R.styleable.MyProgress_progressTextSize,22);
bgColor = typedArray.getColor(R.styleable.MyProgress_bgColor,Color.argb(255, 255, 255, 255));
textColor = typedArray.getColor(R.styleable.MyProgress_textColor,Color.argb(255, 255, 255, 255));
circleColor = typedArray.getColor(R.styleable.MyProgress_circleColor,Color.argb(255, 255, 255, 255));
textString= typedArray.getString(R.styleable.MyProgress_progresstext);
typedArray.recycle();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
bgPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
bgPaint.setColor(bgColor);
circlePaint= new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(circleColor);
mPaint.setAntiAlias(true);//抗锯齿
bgPaint.setAntiAlias(true);//抗锯齿
circlePaint.setAntiAlias(true);//抗锯齿
circlePaint.setStrokeWidth(6);
//circlePaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
specWidthSize = MeasureSpec.getSize(widthMeasureSpec);//宽
specHeightSize = MeasureSpec.getSize(heightMeasureSpec);//高
L.e("specWidthSize:"+specWidthSize);
L.e("specHeightSize:"+specHeightSize);
setMeasuredDimension(specWidthSize, specHeightSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
L.e("specWidthSize2:"+specWidthSize);
onePro = (specWidthSize-specHeightSize)/(float)maxPro;
canvas.save();
Path probg = new Path();
RectF rect = new RectF(specHeightSize*0.5f, specHeightSize*0.35f, specWidthSize-specHeightSize*0.5f, specHeightSize*0.65f);
probg.addRoundRect(rect,specHeightSize*0.25f,specHeightSize*0.25f ,Path.Direction.CCW);
canvas.clipPath(probg);
canvas.drawColor(bgColor);
canvas.restore();
canvas.save();
Path probar = new Path();
probar.addCircle(specHeightSize*0.5f+(pro*onePro), specHeightSize*0.5f, specHeightSize*0.5f-3 ,Path.Direction.CCW);
canvas.clipPath(probar);
canvas.drawColor(bgColor);
canvas.restore();
if(pro==50){
textString ="0";
}else{
if(pro>50){
textString ="+"+(pro-50);
}else if(pro<50){
textString ="-"+(50-pro);
}
}
Rect textRect = new Rect();
mPaint.getTextBounds(textString,0,textString.length(), textRect);
float textWidth = textRect.width();
L.e("textWidth:"+textWidth);
float textHeight = textRect.height();
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
float top = fontMetrics.top;//为基线到字体上边框的距离
float bottom = fontMetrics.bottom;//为基线到字体下边框的距离
int baseLineY = (int) (specHeightSize / 2 - top / 2 - bottom / 2);//基线中间点的y轴计算公式
// canvas.drawText(textString, (int) (width*0.5), baseLineY, mPaint);
canvas.drawText(textString, specHeightSize*0.5f+(pro*onePro)-textWidth/2, baseLineY, mPaint);
// canvas.save();
// RectF rect = new RectF(specHeightSize*0.5f, specHeightSize*0.35f, pro*onePro, specHeightSize*0.65f);
// Path path1 = new Path();
// path1.addRoundRect(rect,specHeightSize*0.25f,specHeightSize*0.25f ,Path.Direction.CCW);
// Path path2 = new Path();
// path2.addCircle(pro*onePro, specHeightSize*0.5f, specHeightSize*0.5f , Path.Direction.CCW);
// canvas.clipPath(path1);
// //canvas.clipPath(path2, Region.Op.DIFFERENCE );
// canvas.drawColor(bgColor);
// canvas.restore();
// canvas.save();
// RectF rect2 = new RectF(pro*onePro, specHeightSize*0.35f, specWidthSize-specHeightSize*0.5f, specHeightSize*0.65f);
// Path path3 = new Path();
// path3.addRoundRect(rect2,specHeightSize*0.25f,specHeightSize*0.25f ,Path.Direction.CCW);
// Path path4 = new Path();
// path4.addCircle(pro*onePro, specHeightSize*0.5f, specHeightSize*0.5f , Path.Direction.CCW);
// canvas.clipPath(path3);
// //canvas.clipPath(path4, Region.Op.DIFFERENCE );
// canvas.drawColor(bgColor);
// canvas.restore();
//
// canvas.drawCircle(specHeightSize*0.5f+(pro*onePro), specHeightSize*0.5f, specHeightSize*0.5f-3 ,circlePaint);
// Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
// float top = fontMetrics.top;//为基线到字体上边框的距离
// float bottom = fontMetrics.bottom;//为基线到字体下边框的距离
// int baseLineY = (int) (specHeightSize / 2 - top / 2 - bottom / 2);//基线中间点的y轴计算公式
// // canvas.drawText(textString, (int) (width*0.5), baseLineY, mPaint);
// canvas.drawText(textString, 0, baseLineY, mPaint);
}
}
<com.inks.jisaiclothesford.view.MyProgress
android:id="@+id/MyProgress"
android:layout_width="@dimen/dp200"
android:layout_height="@dimen/dp30"
android:layout_marginTop="@dimen/dp30"
android:layout_marginLeft="@dimen/dp10"
app:bgColor="@color/color8"
app:circleColor="@color/color8"
app:progressTextSize="@dimen/dp12"
app:textColor="@color/color3"
/>