<declare-styleable name="CiclePercentView"> <attr name="radius" format="integer"/> <attr name="ring_color" format="color"/> </declare-styleable>
private void init() {
paint = new Paint();
paint.setColor(ringColor);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setStrokeWidth(14);
bgPaint = new Paint();
bgPaint.setAntiAlias(true);
bgPaint.setColor(getResources().getColor(R.color.halfwhite));
centerPaint = new Paint();
centerPaint.setAntiAlias(true);
centerPaint.setColor(Color.WHITE);
//起始角度
startAngle = -90;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画圆弧
RectF rectf = new RectF(6,6,dp2px(radius-2),dp2px(radius-2));
canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius)/2,bgPaint);
canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius/3)/2,centerPaint);
canvas.drawArc(rectf,startAngle,curAngle,false,paint);
}
public void countDown(final int totalTime){
countDownTimer = new CountDownTimer(totalTime, (long)(totalTime/100f)) {
@Override
public void onTick(long millisUntilFinished) {
curPercentate = (int) ((totalTime-millisUntilFinished)/(float)totalTime*100);
percentToAngle(curPercentate);
}
@Override
public void onFinish() {
curPercentate = 0;
percentToAngle(curPercentate);
}
}.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
countDown(countdownTime);
break;
case MotionEvent.ACTION_UP:
countDownTimer.cancel();
curPercentate = 0;
percentToAngle(curPercentate);
break;
}
return true;
}
public class CiclePercentView extends View{
private Paint paint;
private int curAngle;
private int curPercentate;
private Paint bgPaint,centerPaint;
private int radius;
private int ringColor;
private int startAngle;
private int countdownTime;
private CountDownTimer countDownTimer;
public CiclePercentView(Context context) {
super(context);
init();
}
public CiclePercentView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CiclePercentView);
radius = array.getInt(R.styleable.CiclePercentView_radius,85);
ringColor = array.getColor(R.styleable.CiclePercentView_ring_color,Color.GREEN);
array.recycle();
init();
}
private void init() {
paint = new Paint();
paint.setColor(ringColor);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setStrokeWidth(14);
bgPaint = new Paint();
bgPaint.setAntiAlias(true);
bgPaint.setColor(getResources().getColor(R.color.halfwhite));
centerPaint = new Paint();
centerPaint.setAntiAlias(true);
centerPaint.setColor(Color.WHITE);
//起始角度
startAngle = -90;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画圆弧
RectF rectf = new RectF(6,6,dp2px(radius-2),dp2px(radius-2));
canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius)/2,bgPaint);
canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius/3)/2,centerPaint);
canvas.drawArc(rectf,startAngle,curAngle,false,paint);
}
private void percentToAngle(int percentage){
curAngle = (int) (percentage/100f*360);
invalidate();
}
public void setCountdownTime(int countdownTime){
this.countdownTime = countdownTime;
}
public void countDown(final int totalTime){
countDownTimer = new CountDownTimer(totalTime, (long)(totalTime/100f)) {
@Override
public void onTick(long millisUntilFinished) {
curPercentate = (int) ((totalTime-millisUntilFinished)/(float)totalTime*100);
percentToAngle(curPercentate);
}
@Override
public void onFinish() {
curPercentate = 0;
percentToAngle(curPercentate);
}
}.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
countDown(countdownTime);
break;
case MotionEvent.ACTION_UP:
countDownTimer.cancel();
curPercentate = 0;
percentToAngle(curPercentate);
break;
}
return true;
}
private int dp2px(int dp){
return (int) (getContext().getResources().getDisplayMetrics().density*dp + 0.5);
}
}
mPaints[0] = new Paint(); mPaints[0].setAntiAlias(true); mPaints[0].setStyle(Paint.Style.FILL); mPaints[0].setColor(0x88FF0000); mUseCenters[0] = false;
mPaints[1] = new Paint(mPaints[0]); mPaints[1].setColor(0x8800FF00); mUseCenters[1] = true;
mPaints[2] = new Paint(mPaints[0]); mPaints[2].setStyle(Paint.Style.STROKE); mPaints[2].setStrokeWidth(4); mPaints[2].setColor(0x880000FF); mUseCenters[2] = false;
mPaints[3] = new Paint(mPaints[2]); mPaints[3].setColor(0x88888888); mUseCenters[3] = true;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有