Android自定义环形LoadingView结果-
发布时间:09/01 来源:未知 浏览:
关键词:
比来项目有要用到环形的进度条,Github上有一个相似的DashedCircularProgress控件,但是他画的进度是通过设定画笔的虚线结果来实现隔断的:progressPaint.setPathEffect(new DashPathEffect(new float[]{dashWith, dashSpace}, dashSpace));要是内层还有一层圆环,在动态设定时,内层和外层有细微的偏向.于是我在原有根基上改了一个,实现了我要的结果(设定进度时可以选中加动画或者不加动画):
public interface OnValueChangeListener { void onValueChange(float value); }
中心绘制类:
InternalCirclePainterImp2,绘制内层的玄色的环形:
/** * @author Chuck */ public class InternalCirclePainterImp2 implements InternalCirclePainter { private RectF internalCircle;//画出圆弧时,圆弧的外切矩形 private Paint internalCirclePaint; private int color; private float startAngle = 270f; int arcQuantity=100;//等分(圆弧加隔断),比方arcQuantity=100时,表示将有100个圆弧,和100个空白隔断 float ratio=0.5f;//每段圆弧与圆弧加隔断之和的比例,ratio=0.5表示每个圆弧与相邻的隔断弧度比是1:1 private int width; private int height; private int internalStrokeWidth = 48;//圆环宽度 public InternalCirclePainterImp2(int color, int progressStrokeWidth, int arcQuantity,float ratio) { this.color = color; this.internalStrokeWidth = progressStrokeWidth; this.arcQuantity = arcQuantity; if(ratio>0&&ratio<1){ this.ratio = ratio; } init(); } private void init() { initExternalCirclePainter(); } private void initExternalCirclePainter() { internalCirclePaint = new Paint(); internalCirclePaint.setAntiAlias(true); internalCirclePaint.setStrokeWidth(internalStrokeWidth); internalCirclePaint.setColor(color); internalCirclePaint.setStyle(Paint.Style.STROKE); } //圆弧外切矩形 private void initExternalCircle() { internalCircle = new RectF(); float padding = internalStrokeWidth * 0.5f; internalCircle.set(padding, padding , width - padding, height - padding); initExternalCirclePainter(); } @Override public void draw(Canvas canvas) { float eachAngle=360f/arcQuantity; float eachArcAngle=eachAngle*ratio; for(int i=0;iProgressPainterImp2,绘制内层的玄色的环形:
/** * @author Chuck */ public class ProgressPainterImp2 implements ProgressPainter { private RectF progressCircle; private Paint progressPaint; private int color = Color.RED; private float startAngle = 270f; private int internalStrokeWidth = 48; private float min; private float max; private int width; private int height; private int currentPecent;//目前的百分比 int arcQuantity=100;//等分(圆弧加隔断),比方arcQuantity=100时,表示将有100个圆弧,和100个空白隔断 float ratio=0.5f;//每段圆弧与圆弧加隔断之和的比例,ratio=0.5表示每个圆弧与相邻的隔断弧度比是1:1 public ProgressPainterImp2(int color, float min, float max, int progressStrokeWidth, int arcQuantity,float ratio) { this.color = color; this.min = min; this.max = max; this.internalStrokeWidth = progressStrokeWidth; this.arcQuantity = arcQuantity; this.ratio = ratio; init(); Log.e("ProgressPainterImp","结构函数施行"); } private void init() { initInternalCirclePainter(); } private void initInternalCirclePainter() { progressPaint = new Paint(); progressPaint.setAntiAlias(true); progressPaint.setStrokeWidth(internalStrokeWidth); progressPaint.setColor(color); progressPaint.setStyle(Paint.Style.STROKE); } //初始化外切的阿谁矩形 private void initInternalCircle() { progressCircle = new RectF(); float padding = internalStrokeWidth * 0.5f; progressCircle.set(padding, padding , width - padding, height - padding); initInternalCirclePainter(); } @Override public void draw(Canvas canvas) { float eachAngle=360f/arcQuantity; float eachArcAngle=eachAngle*ratio; int quantity=2*arcQuantity*currentPecent/100; for(int i=0;i可以自定义的属性:
调取:
main_activity.xml:
MainActivity:
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { mDashedCircularProgress.setValue(66);//没有动画的,直接设定 } catch (Exception e) { e.printStackTrace(); } } }); findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { mDashedCircularProgress.setValue(0);//无动画,归零 mDashedCircularProgress.setValueWithAnimation(100,2000);//带动画 } catch (Exception e) { e.printStackTrace(); } } });Github地址:https://github.com/506954774/AndroidCircularLoadingView
以上就是本文的全部内容,但愿对大家的学习有所帮忙,更多相干内容请关注百分百源码网!