Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
效果展示
動(dòng)畫(huà)效果
View實(shí)現(xiàn)
1.底層圓環(huán)是灰色背景
2.上層圓環(huán)是紅色背景
3.使用動(dòng)畫(huà)畫(huà)一條弧線(xiàn)
View
/** * 圓環(huán)進(jìn)度條 */ public class RoundProgressBar extends View { //繪制矩形區(qū)域 private RectF rectF; //起始角度 private float startAngle; //掃過(guò)角度 private float sweepAngle; //畫(huà)筆 private Paint paint; //默認(rèn)控件大小 private int defoutSize; //默認(rèn)線(xià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(); //抗鋸齒畫(huà)筆 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); //畫(huà)布旋轉(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; //重寫(xiě)繪制 invalidate(); } }); valueAnimator.start(); } }
最后在A(yíng)ctivity中使用setProgress方法賦值進(jìn)度條的進(jìn)度來(lái)實(shí)現(xiàn)效果
progressView.setProgress(0.8f);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義進(jìn)度條漸變色View的實(shí)例代碼
- Android中實(shí)現(xiàn)Webview頂部帶進(jìn)度條的方法
- android ListView和ProgressBar(進(jìn)度條控件)的使用方法
- Android自定義View實(shí)現(xiàn)漸變色進(jìn)度條
- Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條
- Android自定義View實(shí)現(xiàn)帶數(shù)字的進(jìn)度條實(shí)例代碼
- Android Webview添加網(wǎng)頁(yè)加載進(jìn)度條實(shí)例詳解
- Android自定義View實(shí)現(xiàn)水平帶數(shù)字百分比進(jìn)度條
- Android自定義圓環(huán)式進(jìn)度條
- Android自定義view實(shí)現(xiàn)圓環(huán)進(jìn)度條效果
相關(guān)文章
Android開(kāi)發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊(cè)界面功能示例
這篇文章主要介紹了Android開(kāi)發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊(cè)界面功能,涉及Android使用intent傳值及界面布局等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Android開(kāi)發(fā)筆記 最好使用eclipse
值得注意一點(diǎn)的是,雖然Myeclipse比eclipse功能更強(qiáng)大,但是在具體的安卓開(kāi)發(fā)過(guò)程當(dāng)中,最好還是選用eclipse,sdk跟eclipse的兼容性更好2012-11-11Android編程心得分享——JSON學(xué)習(xí)過(guò)程
在我們初步學(xué)習(xí)JSON時(shí)我們都知道JSON作為現(xiàn)在比較流行的數(shù)據(jù)交換格式,有著它的許多優(yōu)點(diǎn),這里將我學(xué)習(xí)JSON的過(guò)程記錄如下2013-06-06Android筆記之:App模塊化及工程擴(kuò)展的應(yīng)用
這篇文章是android開(kāi)發(fā)人員的必備知識(shí),是我特別為大家整理和總結(jié)的,不求完美,但是有用2013-04-04flutter 自定義websocket路由的實(shí)現(xiàn)
這篇文章主要介紹了flutter 自定義websocket路由的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Android Application存取公共數(shù)據(jù)的實(shí)例詳解
這篇文章主要介紹了Android Application存取公共數(shù)據(jù)的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07關(guān)于A(yíng)ndroid Studio安裝完后activity_main.xml前幾行報(bào)錯(cuò)的解決建議
這篇文章主要介紹了關(guān)于A(yíng)ndroid Studio安裝完后activity_main.xml前幾行報(bào)錯(cuò)的解決建議,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
這篇文章主要介紹了Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法,實(shí)例分析了Android中ListView控件的使用技巧,需要的朋友可以參考下2016-01-01