久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條

瀏覽:136日期:2022-09-23 18:54:22

本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下

效果展示

動(dòng)畫效果

Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條

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)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 文昌市| 永州市| 石屏县| 屏山县| 禄劝| 寿光市| 合山市| 江油市| 阳朔县| 自贡市| 南召县| 金乡县| 镇江市| 邯郸市| 湄潭县| 孝昌县| 社会| 仙桃市| 邯郸县| 娄烦县| 麦盖提县| 兴国县| 湟源县| 宿松县| 梁平县| 博野县| 广南县| 延长县| 大洼县| 建宁县| 宁南县| 安岳县| 临夏市| 松原市| 武夷山市| 金山区| 磴口县| 高平市| 吉首市| 南靖县| 红河县|