十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要讲解了Android如何实现自定义带动画效果的圆形ProgressBar,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
公司主营业务:网站建设、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出石河子免费做网站回馈大家。
最近有个需求显示进度,尾部还要有一标示,像下边这样
使用自定义View的方式实现,代码如下,很简单注释的很清楚
文章最后我们拓展一下功能,实现一个带动画效果的进度条
package com.example.fwc.allexample.progressbar; import android.animation.ValueAnimator; import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Typeface; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.animation.DecelerateInterpolator; import com.example.fwc.allexample.R; /** * Created by fwc on 2016/7/6. */ public class CircleProgressBar extends View { private Context mContext; private Paint mPaint; private int mProgress = 0; private static int MAX_PROGRESS = 100; /** * 弧度 */ private int mAngle; /** * 中间的文字 */ private String mText; /** * 外圆颜色 */ private int outRoundColor; /** * 内圆的颜色 */ private int inRoundColor; /** * 线的宽度 */ private int roundWidth; private int style; /** * 字体颜色 */ private int textColor; /** * 字体大小 */ private float textSize; /** * 字体是否加粗 */ private boolean isBold; /** * 进度条颜色 */ private int progressBarColor; public CircleProgressBar(Context context) { this(context, null); } public CircleProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mContext = context; init(attrs); } @TargetApi(21) public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); mContext = context; init(attrs); } /** * 解析自定义属性 * * @param attrs */ public void init(AttributeSet attrs) { mPaint = new Paint(); TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar); outRoundColor = typedArray.getColor(R.styleable.CircleProgressBar_outCircleColor, getResources().getColor(R.color.colorPrimary)); inRoundColor = typedArray.getColor(R.styleable.CircleProgressBar_inCircleColor, getResources().getColor(R.color.colorPrimaryDark)); progressBarColor = typedArray.getColor(R.styleable.CircleProgressBar_progressColor, getResources().getColor(R.color.colorAccent)); isBold = typedArray.getBoolean(R.styleable.CircleProgressBar_textBold, false); textColor = typedArray.getColor(R.styleable.CircleProgressBar_textColor, Color.BLACK); roundWidth = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBar_lineWidth, 20); typedArray.recycle(); } @Override protected void onDraw(Canvas canvas) { /** * 画外圆 */ super.onDraw(canvas); int center = getWidth() / 2; //圆心 int radius = (center - roundWidth / 2); //半径 mPaint.setColor(outRoundColor); //外圆颜色 mPaint.setStrokeWidth(roundWidth); //线的宽度 mPaint.setStyle(Paint.Style.STROKE); //空心圆 mPaint.setAntiAlias(true); //消除锯齿 canvas.drawCircle(center, center, radius, mPaint); //内圆 mPaint.setColor(inRoundColor); radius = radius - roundWidth; canvas.drawCircle(center, center, radius, mPaint); //画进度是一个弧线 mPaint.setColor(progressBarColor); RectF rectF = new RectF(center - radius, center - radius, center + radius, center + radius);//圆弧范围的外接矩形 canvas.drawArc(rectF, -90, mAngle, false, mPaint); canvas.save(); //平移画布之前保存之前画的 //画进度终点的小球,旋转画布的方式实现 mPaint.setStyle(Paint.Style.FILL); //将画布坐标原点移动至圆心 canvas.translate(center, center); //旋转和进度相同的角度,因为进度是从-90度开始的所以-90度 canvas.rotate(mAngle - 90); //同理从圆心出发直接将原点平移至要画小球的位置 canvas.translate(radius, 0); canvas.drawCircle(0, 0, roundWidth, mPaint); //画完之后恢复画布坐标 canvas.restore(); //画文字将坐标平移至圆心 canvas.translate(center, center); mPaint.setStrokeWidth(0); mPaint.setColor(textColor); if (isBold) { //字体加粗 mPaint.setTypeface(Typeface.DEFAULT_BOLD); } if (TextUtils.isEmpty(mText)) { mText = mProgress + "%"; } //动态设置文字长为圆半径,计算字体大小 float textLength = mText.length(); textSize = radius / textLength; mPaint.setTextSize(textSize); //将文字画到中间 float textWidth = mPaint.measureText(mText); canvas.drawText(mText, -textWidth / 2, textSize / 2, mPaint); } public int getmProgress() { return mProgress; } /** * 设置进度 * * @return */ public void setmProgress(int p) { if (p > MAX_PROGRESS) { mProgress = MAX_PROGRESS; mAngle = 360; } else { mProgress = p; mAngle = 360 * p / MAX_PROGRESS; } } public String getmText() { return mText; } /** * 设置文本 * * @param mText */ public void setmText(String mText) { this.mText = mText; } /** * 设置带动画的进度 * @param p */ public void setAnimProgress(int p) { if (p > MAX_PROGRESS) { mProgress = MAX_PROGRESS; } else { mProgress = p; } //设置属性动画 ValueAnimator valueAnimator = new ValueAnimator().ofInt(0, p); //动画从快到慢 valueAnimator.setInterpolator(new DecelerateInterpolator()); valueAnimator.setDuration(3000); //监听值的变化 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int currentV = (Integer) animation.getAnimatedValue(); Log.e("fwc", "current" + currentV); mAngle = 360 * currentV / MAX_PROGRESS; mText = currentV + "%"; invalidate(); } }); valueAnimator.start(); } }
自定义属性
布局文件中使用
<?xml version="1.0" encoding="utf-8"?>
activity中设置进度,显示文字
package com.example.fwc.allexample.progressbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.example.fwc.allexample.R; public class ProgressActivtiy extends AppCompatActivity { CircleProgressBar circleProgressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress_activtiy); circleProgressBar = (CircleProgressBar)findViewById(R.id.progress_bar); circleProgressBar.setProgress(65); circleProgressBar.setmText(circleProgressBar.getProgress()+"%"); } }
效果图
拓展
拓展也很简单,加一个setAnimProgress(int p)设置动画效果:
/** * 设置带动画的进度 * @param p */ public void setAnimProgress(int p) { if (p > MAX_PROGRESS) { mProgress = MAX_PROGRESS; } else { mProgress = p; } //设置属性动画 ValueAnimator valueAnimator = new ValueAnimator().ofInt(0, p); //动画从快到慢 valueAnimator.setInterpolator(new DecelerateInterpolator()); valueAnimator.setDuration(3000); //监听值的变化 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int currentV = (Integer) animation.getAnimatedValue(); Log.e("fwc", "current" + currentV); mAngle = 360 * currentV / MAX_PROGRESS; mText = currentV + "%"; invalidate(); } }); valueAnimator.start(); }
在activity中调用这个方法
circleProgressBar.setAnimProgress(65);
效果如下
看完上述内容,是不是对Android如何实现自定义带动画效果的圆形ProgressBar有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。