Android自定義帶加載動(dòng)畫效果的環(huán)狀進(jìn)度條
最近閑來無事,自定義了一個(gè)環(huán)狀進(jìn)度條,話不多說直接上代碼 :
public class CircleProgressView extends View{ private Paint mCirPaint; private Paint mArcPaint; private Paint mTextPaint; private float radius=200; private int textsize=60; private int progress=68; private int stokeWidth=10; private int circleColor=Color.GRAY; private int arcColor=Color.GREEN; private int textColor=Color.BLACK; private int speed=0; public CircleProgressView(Context context) { super(context); } public CircleProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CircleProgressView(Context context, AttributeSet attrs) { super(context, attrs); } public void setRadius(float radius){ this.radius=radius; invalidate(); } public void setTextSize(int textsize){ this.textsize=textsize; invalidate(); } public void setProgress(int progress){ this.progress=progress; } public void setStokewidth(int stokeWidth){ this.stokeWidth=stokeWidth; invalidate(); } public void setColor(int circleColor,int arcColor,int textColor){ this.circleColor=circleColor; this.arcColor=arcColor; this.textColor=textColor; invalidate(); } public void setSpeed(int speed){ this.speed=speed; } private void init() { mCirPaint=new Paint(); mCirPaint.setColor(circleColor); mCirPaint.setAntiAlias(true); mCirPaint.setStyle(Paint.Style.STROKE); mCirPaint.setStrokeWidth(stokeWidth); mArcPaint=new Paint(); mArcPaint.setColor(arcColor); mArcPaint.setAntiAlias(true); mArcPaint.setStyle(Paint.Style.STROKE); mArcPaint.setStrokeWidth(stokeWidth); mTextPaint=new Paint(); mTextPaint.setColor(textColor); mTextPaint.setTextSize(textsize); mTextPaint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); init(); float centerX=getWidth()/2; float centerY=getHeight()/2; canvas.drawCircle(centerX,centerY,radius,mCirPaint); canvas.drawArc(centerX-radius,centerY-radius,centerX+radius,centerY+radius,-90,progress*360/100,false,mArcPaint); canvas.drawText(progress+"%",centerX-(mTextPaint.measureText(progress+"%"))/2,centerY+textsize/2,mTextPaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (speed!=0){ startProgress(); } } public void startProgress(){ final int preProgress=progress; new CountDownTimer(preProgress * speed, speed) { @Override public void onTick(long l) { setProgress(preProgress-(int) (l/speed)); invalidate(); } @Override public void onFinish() { setProgress(preProgress); invalidate(); this.cancel(); } }.start(); } }
相關(guān)用法:
setProgress(progress);//設(shè)置進(jìn)度
setRadius(300);//設(shè)置半徑
setStokewidth(60);//設(shè)置環(huán)寬
setTextSize(80);//設(shè)置文字進(jìn)度大小
setColor(Color.GRAY,Color.RED,Color.BLUE);//設(shè)置顏色(環(huán)的顏色,進(jìn)度條的顏色,文字進(jìn)度的字體顏色)
setSpeed(20);//設(shè)置動(dòng)畫速度,這里的數(shù)值是每次進(jìn)度加一所用時(shí)間,所以數(shù)值越小動(dòng)畫速度越快
測(cè)試代碼:
mCircleProgressView= (CircleProgressView) findViewById(R.id.circle_progress); mCircleProgressView.setProgress(progress); mCircleProgressView.setRadius(300); mCircleProgressView.setStokewidth(60); mCircleProgressView.setTextSize(80); mCircleProgressView.setColor(Color.GRAY,Color.RED,Color.BLUE); mCircleProgressView.setSpeed(20);
測(cè)試效果
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
- Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
- Android 進(jìn)度條 ProgressBar的實(shí)現(xiàn)代碼(隱藏、出現(xiàn)、加載進(jìn)度)
- Android自定義View仿華為圓形加載進(jìn)度條
- Android自定義View實(shí)現(xiàn)加載進(jìn)度條效果
- Android開發(fā)之ProgressBar字體隨著進(jìn)度條的加載而滾動(dòng)
- Android自定義View基礎(chǔ)開發(fā)之圖片加載進(jìn)度條
- Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
- Android自定義帶進(jìn)度條WebView仿微信加載過程
- Android自定義View實(shí)現(xiàn)圓形加載進(jìn)度條
相關(guān)文章
Android中使用PagerSlidingTabStrip實(shí)現(xiàn)導(dǎo)航標(biāo)題的示例
本篇文章主要介紹了Android中使用PagerSlidingTabStrip實(shí)現(xiàn)導(dǎo)航標(biāo)題的示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01android實(shí)現(xiàn)輪播圖引導(dǎo)頁
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)輪播圖引導(dǎo)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android中給按鈕同時(shí)設(shè)置背景和圓角示例代碼
相信每位Android開發(fā)者們都遇到過給按鈕設(shè)置背景或者設(shè)置圓角的需求,但是如果要同時(shí)設(shè)置背景和圓角該怎么操作才是方便快捷的呢?這篇文章通過示例代碼給大家演示了Android中給按鈕同時(shí)設(shè)置背景和圓角的方法,有需要的朋友們可以參考借鑒。2016-10-10Android Studio 報(bào)Integer types not allowed錯(cuò)誤
本文給大家分享的是在使用Android Studio的過程中遇到的報(bào)Integer types not allowed錯(cuò)誤的分析及解決方法,非常實(shí)用,有需要的小伙伴可以參考下2017-10-10flutter實(shí)現(xiàn)底部導(dǎo)航欄切換
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)底部導(dǎo)航欄切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Android穩(wěn)定性:可遠(yuǎn)程配置化的Looper兜底框架
這篇文章主要為大家介紹了Android穩(wěn)定性可遠(yuǎn)程配置化的Looper兜底框架實(shí)例實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02ListView-添加item的事件監(jiān)聽實(shí)例
下面小編就為大家?guī)硪黄狶istView-添加item的事件監(jiān)聽實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07