这是我对循环进度视图的实现。 (是否支持将其值绑定到 TextView)
适用于 Android 16+(或更低版本未测试)
设置总计和进度。
重要的是 THIS VIEW 不会自行测量,因此您需要提供一些正方形属性,例如 width=100dp height=100dp
package app.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;
/**
* Created by MarcosVasconcelos on 28/07/2017.
*/
public class CircularProgressView extends View {
private final float strokeWidth;
private Paint totalPaint, progressPaint;
private float progress = 360;
private float total = 360;
public TextView textView;
public CircularProgressView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, context.getResources().getDisplayMetrics());
totalPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
totalPaint.setStyle(Paint.Style.STROKE);
totalPaint.setColor(Color.parseColor("#CCCCCC"));
totalPaint.setStrokeWidth(strokeWidth);
progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
progressPaint.setStyle(Paint.Style.STROKE);
progressPaint.setColor(Color.GREEN);
progressPaint.setStrokeWidth(strokeWidth);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
drawProgress(canvas, (int) 360f, totalPaint);
if(total != 0 && progress != 0)
drawProgress(canvas, total == progress ? 360 : (int) ((360f / total) * progress), progressPaint);
canvas.restore();
}
private void drawProgress(Canvas canvas, int total, Paint paint) {
canvas.drawArc(new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth), -90, total, false, paint);
}
public void setProgress(int progress) {
this.progress = progress;
invalidate();
}
public void setProgressColor(int rgb) {
progressPaint.setColor(rgb);
invalidate();
}
public void setTotal(int total) {
this.total = total;
if(textView != null)
textView.setText(String.valueOf(total));
invalidate();
}
public void setTotalTextView(TextView view) {
this.textView = view;
textView.setText(String.valueOf((int)total));
}
}