Android自定义view渐变圆形动画

这篇文章主要为大家详细介绍了Android自定义view渐变圆形动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android自定义view渐变圆形动画的具体代码,供大家参考,具体内容如下

直接上效果图

自定义属性

attrs.xml文件

   

创建一个类 ProgressRing继承自 view

 public class ProgressRing extends View { private int progressStartColor; private int progressEndColor; private int bgStartColor; private int bgMidColor; private int bgEndColor; private int progress; private float progressWidth; private int startAngle; private int sweepAngle; private boolean showAnim; private int mMeasureHeight; private int mMeasureWidth; private Paint bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); private Paint progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); private RectF pRectF; private float unitAngle; private int curProgress = 0; public ProgressRing(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ProgressRing); progressStartColor = ta.getColor(R.styleable.ProgressRing_pr_progress_start_color, Color.YELLOW); progressEndColor = ta.getColor(R.styleable.ProgressRing_pr_progress_end_color, progressStartColor); bgStartColor = ta.getColor(R.styleable.ProgressRing_pr_bg_start_color, Color.LTGRAY); bgMidColor = ta.getColor(R.styleable.ProgressRing_pr_bg_mid_color, bgStartColor); bgEndColor = ta.getColor(R.styleable.ProgressRing_pr_bg_end_color, bgStartColor); progress = ta.getInt(R.styleable.ProgressRing_pr_progress, 0); progressWidth = ta.getDimension(R.styleable.ProgressRing_pr_progress_width, 8f); startAngle = ta.getInt(R.styleable.ProgressRing_pr_start_angle, 150); sweepAngle = ta.getInt(R.styleable.ProgressRing_pr_sweep_angle, 240); showAnim = ta.getBoolean(R.styleable.ProgressRing_pr_show_anim, true); ta.recycle(); unitAngle = (float) (sweepAngle / 100.0); bgPaint.setStyle(Paint.Style.STROKE); bgPaint.setStrokeCap(Paint.Cap.ROUND); bgPaint.setStrokeWidth(progressWidth); progressPaint.setStyle(Paint.Style.STROKE); progressPaint.setStrokeCap(Paint.Cap.ROUND); progressPaint.setStrokeWidth(progressWidth); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /*for (int i = 0, end = (int) (progress * unitAngle); i <= end; i++) { progressPaint.setColor(getGradient(i / (float) end, progressStartColor, progressEndColor)); canvas.drawArc(pRectF, startAngle + i, 1, false, progressPaint); }*/ if (!showAnim) { curProgress = progress; } drawBg(canvas); drawProgress(canvas); if (curProgress  st; --i) { if (i - halfSweep > 0) { bgPaint.setColor(getGradient((i - halfSweep) / halfSweep, bgMidColor, bgEndColor)); } else { bgPaint.setColor(getGradient((halfSweep - i) / halfSweep, bgMidColor, bgStartColor)); } canvas.drawArc(pRectF, startAngle + i, 1, false, bgPaint); } } private void drawProgress(Canvas canvas) { for (int i = 0, end = (int) (curProgress * unitAngle); i <= end; i++) { progressPaint.setColor(getGradient(i / (float) end, progressStartColor, progressEndColor)); canvas.drawArc(pRectF, startAngle + i, 1, false, progressPaint); } } public void setProgress(@IntRange(from = 0, to = 100) int progress) { this.progress = progress; invalidate(); } public int getProgress() { return progress; } public int getGradient(float fraction, int startColor, int endColor) { if (fraction > 1) fraction = 1; int alphaStart = Color.alpha(startColor); int redStart = Color.red(startColor); int blueStart = Color.blue(startColor); int greenStart = Color.green(startColor); int alphaEnd = Color.alpha(endColor); int redEnd = Color.red(endColor); int blueEnd = Color.blue(endColor); int greenEnd = Color.green(endColor); int alphaDifference = alphaEnd - alphaStart; int redDifference = redEnd - redStart; int blueDifference = blueEnd - blueStart; int greenDifference = greenEnd - greenStart; int alphaCurrent = (int) (alphaStart + fraction * alphaDifference); int redCurrent = (int) (redStart + fraction * redDifference); int blueCurrent = (int) (blueStart + fraction * blueDifference); int greenCurrent = (int) (greenStart + fraction * greenDifference); return Color.argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mMeasureWidth = getMeasuredWidth(); mMeasureHeight = getMeasuredHeight(); if (pRectF == null) { float halfProgressWidth = progressWidth / 2; pRectF = new RectF(halfProgressWidth + getPaddingLeft(), halfProgressWidth + getPaddingTop(), mMeasureWidth - halfProgressWidth - getPaddingRight(), mMeasureHeight - halfProgressWidth - getPaddingBottom()); } } } 

xml布局

 

以上就是Android自定义view渐变圆形动画的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动