<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomProgressBar"> <attr name="roundProgressColor" format="color"></attr> <attr name="roundColor" format="color"></attr> <attr name="roundWidth" format="dimension"></attr> <attr name="textSize" format="dimension"></attr> <attr name="textColor" format="color"></attr> <attr name="max" format="integer"></attr> <attr name="textShow" format="boolean"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable> </resources>
public class CustomProgressBar extends View {
private int max = 100;//总进度
private int roundColor = Color.RED;//进度圆弧的颜色
private float roundWidth = 10;//圆边框宽度
private int roundProgressColor = Color.BLUE;//默认的大圆环边框颜色
private float textSize = 55;//文本大小
private int textColor = Color.GREEN;//文本默认颜色
private boolean textShow = true;//是否展示文本
public static final int STROKE = 0;//描边
public static final int FILL = 1;//填充
private int style = STROKE;//默认描边
private int progress;//进度
private Paint mPaint;
private int mWidth = 200;//默认控件宽度,wrap_content时候使用
private int mHeight = 200;//默认控件高度,wrap_content时候使用
public CustomProgressBar(Context context) {
this(context, null);
}
public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
mPaint = new Paint();
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
max = typedArray.getInteger(R.styleable.CustomProgressBar_max, 100);
roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.BLUE);
roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor, Color.BLUE);
textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor, Color.GREEN);
textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize, 55);
roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth, 10);
textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow, true);
style = typedArray.getInt(R.styleable.CustomProgressBar_style, 0);
typedArray.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
setMeasuredDimension(mWidth,mHeight);
}else if (widthSpecMode == MeasureSpec.AT_MOST){
setMeasuredDimension(mWidth,heightSpecSize);
}else if (heightSpecMode == MeasureSpec.AT_MOST){
setMeasuredDimension(widthSpecSize,mHeight);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final int paddingLeft = getPaddingLeft();
final int paddingRight = getPaddingRight();
final int paddingTop = getPaddingTop();
final int paddingBottom = getPaddingBottom();
int width = getWidth() - paddingLeft - paddingRight;
int height = getHeight() - paddingBottom - paddingTop;
//画默认的大圆环
float radius = (float)Math.min(width,height)/2.0f;//中心坐标点
mPaint.setColor(roundColor);
mPaint.setStyle(Paint.Style.STROKE);//描边
mPaint.setStrokeWidth(roundWidth);//圆环边的宽度
// if (style == STROKE){
// mPaint.setStrokeWidth(roundWidth);//圆环边的宽度
// }
mPaint.setAntiAlias(true);
//(float cx, float cy, float radius, @NonNull Paint paint)
canvas.drawCircle(paddingLeft+width/2,paddingTop+height/2,radius,mPaint);
//画进度百分比
mPaint.setColor(textColor);
mPaint.setStrokeWidth(0);//圆环的宽度
mPaint.setTextSize(textSize);
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
int percent = (int)(progress/(float)max * 100);
if(textShow && percent!=0 && style == STROKE){
//(@NonNull String text, float x, float y, @NonNull Paint paint)
canvas.drawText(percent+"%", (getWidth()-mPaint.measureText(percent+"%"))/2f,
//y公式: float baselineY = centerY + (fontMetrics.bottom-fontMetrics.top)/2 - fontMetrics.bottom
getWidth()/2f-(mPaint.descent()+mPaint.ascent())/2f,
mPaint);
}
//画圆弧
//矩形区域,定义圆弧的形状大小
//(float left, float top, float right, float bottom)
RectF oval = new RectF(paddingLeft, paddingTop, width+paddingLeft, height+paddingTop);
mPaint.setColor(roundProgressColor);
mPaint.setStrokeWidth(roundWidth);//圆环边的宽度
switch (style){
case STROKE:
mPaint.setStyle(Paint.Style.STROKE);
//(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,@NonNull Paint paint)
//useCenter:设置圆弧在绘画的时候,是否经过圆形
canvas.drawArc(oval , 0, 360*progress/max, false, mPaint);
break;
case FILL:
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
if(progress!=0)
canvas.drawArc(oval , 0, 360*progress/max, true, mPaint);
break;
default:
break;
}
}
public void setProgressWidth(int width) {
mWidth = width;
}
public void setProgressHeight(int height) {
mHeight = height;
}
public synchronized void setMax(int max) {
if (max < 0) {
throw new IllegalArgumentException("max不能小于0");
}
this.max = max;
}
public void setRoundColor(int roundColor) {
this.roundColor = roundColor;
}
public void setRoundWidth(float roundWidth) {
this.roundWidth = roundWidth;
}
public void setRoundProgressColor(int roundProgressColor) {
this.roundProgressColor = roundProgressColor;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public void setTextShow(boolean textShow) {
this.textShow = textShow;
}
public synchronized void setProgress(int progress) {
if (progress < 0) {
throw new IllegalArgumentException("progress不能小于0");
}
if (progress > max) {
progress = max;
}
if (progress <= max) {
this.progress = progress;
postInvalidate();
}
}
public synchronized int getMax() {
return max;
}
public int getRoundColor() {
return roundColor;
}
public float getRoundWidth() {
return roundWidth;
}
public int getRoundProgressColor() {
return roundProgressColor;
}
public int getTextColor() {
return textColor;
}
public boolean isTextShow() {
return textShow;
}
public synchronized int getProgress() {
return progress;
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.main.paint.PaintActivity"> <com.main.paint.CustomProgressBar android:id="@+id/progressbar" android:layout_width="200dp" android:layout_height="200dp" app:roundProgressColor="#FF0000" app:roundWidth="2dp" app:textColor="#FF0000" app:style="STROKE" android:padding="30dp" app:textSize="20dp"/> <com.main.paint.CustomProgressBar android:id="@+id/progressbar01" android:layout_width="200dp" android:layout_height="200dp" app:roundProgressColor="#FF0000" app:roundWidth="2dp" app:textColor="#FF0000" app:style="FILL" android:padding="30dp" app:textSize="20dp"/> </LinearLayout>
public class PaintActivity extends AppCompatActivity {
private CustomProgressBar mCustomProgressBar;
private CustomProgressBar mCustomProgressBar01;
private int progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paint);
mCustomProgressBar = (CustomProgressBar)this.findViewById(R.id.progressbar);
mCustomProgressBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
progress = 0;
while (progress <= 100) {
progress += 2;
mCustomProgressBar.setProgress(progress);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
mCustomProgressBar01 = (CustomProgressBar)this.findViewById(R.id.progressbar01);
mCustomProgressBar01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
progress = 0;
while (progress <= 100) {
progress += 2;
mCustomProgressBar01.setProgress(progress);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有