Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
效果展示
動(dòng)畫效果
View實(shí)現(xiàn)
1.底層圓環(huán)是灰色背景2.上層圓環(huán)是紅色背景3.使用動(dòng)畫畫一條弧線
View
/** * 圓環(huán)進(jìn)度條 */public class RoundProgressBar extends View { //繪制矩形區(qū)域 private RectF rectF; //起始角度 private float startAngle; //掃過角度 private float sweepAngle; //畫筆 private Paint paint; //默認(rèn)控件大小 private int defoutSize; //默認(rèn)線條寬度 private int defoutLine; private int strokeWidth; private PointF pointF = new PointF(); public RoundProgressBar(Context context) { super(context); initData(); } public RoundProgressBar(Context context, AttributeSet attrs) { super(context, attrs); initData(); } /** * 參數(shù)初始化 */ private void initData() { startAngle = 0; sweepAngle = 0; defoutSize = 400; defoutLine = 20; strokeWidth = 20; rectF = new RectF(); //抗鋸齒畫筆 paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.GRAY); paint.setStrokeWidth(defoutLine); //筆帽樣式 paint.setStrokeCap(Paint.Cap.ROUND); paint.setStyle(Paint.Style.STROKE); } /** * xml -----> 提供可繪制位置 * * @param widthMeasureSpec 寬 * @param heightMeasureSpec 高 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(defoutSize, defoutSize); } /** * 當(dāng)大小時(shí)改變回調(diào) * * @param w * @param h * @param oldw * @param oldh */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); pointF.x = w >> 1; pointF.y = h >> 1; rectF.top = strokeWidth >> 1; rectF.bottom = h - (strokeWidth >> 1); rectF.left = strokeWidth >> 1; rectF.right = w - (strokeWidth >> 1); } /** * 繪制 * * @param canvas */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //畫布旋轉(zhuǎn) paint.setColor(Color.GRAY); canvas.rotate(135, pointF.x, pointF.y); //繪制圓環(huán) canvas.drawArc(rectF, startAngle, 270, false, paint); paint.setColor(Color.RED); canvas.drawArc(rectF, startAngle, sweepAngle, false, paint); } public void setProgress(float index) { //防止數(shù)值越界 if (index > 1 || index < 0) { return; } ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, index); valueAnimator.setDuration(3000); valueAnimator.setInterpolator(new DecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { sweepAngle = (float) animation.getAnimatedValue() * 270; //重寫繪制 invalidate(); } }); valueAnimator.start(); }}
最后在Activity中使用setProgress方法賦值進(jìn)度條的進(jìn)度來實(shí)現(xiàn)效果
progressView.setProgress(0.8f);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫(kù)用法分享3. python操作數(shù)據(jù)庫(kù)獲取結(jié)果之fetchone和fetchall的區(qū)別說明4. php5.6不能擴(kuò)展redis.so的解決方法5. Ajax實(shí)現(xiàn)頁面無刷新留言效果6. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效7. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值8. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總9. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】10. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)
