android自定義view制作圓形進(jìn)度條效果
還是我們自定View的那幾個(gè)步驟:
1、自定義View的屬性
2、在View的構(gòu)造方法中獲得我們自定義的屬性
[ 3、重寫onMesure ]
4、重寫onDraw
1、自定義屬性:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomTitleView"> <attr name="mSpeed" format="integer" /> <attr name="mFirstColor" format="color" /> <attr name="mSecondColor" format="color" /> <attr name="mCircleWidth" format="dimension"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>
2、在View的構(gòu)造方法中獲得我們自定義的屬性
/** * 當(dāng)前進(jìn)度 */ private int mProgress; /** * 第一圈的顏色 */ private int mFirstColor; /** * 第二圈的顏色 */ private int mSecondColor; /** * 圈的寬度 */ private int mCircleWidth; /** * 速度 */ private int mSpeed; /** * 中間進(jìn)度百分比的字符串的字體 */ private float textSize; private boolean isNext = false; private Paint mPaint; public CustomTitleView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typearray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView); int count= typearray.getIndexCount(); for(int i=0;i<count;i++){ int attr =typearray.getIndex(i); switch(attr){ case R.styleable.CustomTitleView_mFirstColor: mFirstColor=typearray.getColor(attr,Color.BLACK); break; case R.styleable.CustomTitleView_mSecondColor: mSecondColor=typearray.getColor(attr,Color.RED); break; case R.styleable.CustomTitleView_mCircleWidth: mCircleWidth = typearray.getDimensionPixelSize(attr,(int)TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_PX,20,getResources().getDisplayMetrics())); break; case R.styleable.CustomTitleView_textSize: textSize = typearray.getDimensionPixelSize(attr,(int)TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_PX,30,getResources().getDisplayMetrics())); break; case R.styleable.CustomTitleView_mSpeed: mSpeed = typearray.getInt(attr,100);// 默認(rèn)100 break; } } Log.v("----",mSpeed+""); typearray.recycle(); mPaint = new Paint(); new Thread() { public void run() { while (true) { mProgress++; if (mProgress == 360) { mProgress = 0; if (!isNext) isNext = true; else isNext = false; } postInvalidate(); try { Thread.sleep(mSpeed); } catch (InterruptedException e) { e.printStackTrace(); } } }; }.start(); }
3、直接重寫onDraw,這不需要重寫onMeasure
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); } protected void onDraw(Canvas canvas) { /** * 畫進(jìn)度百分比 */ mPaint.setStrokeWidth(0); mPaint.setColor(Color.BLACK); mPaint.setTextSize(textSize); mPaint.setTypeface(Typeface.DEFAULT_BOLD); //設(shè)置字體 int percent = (int)(((float)mProgress / (float)360) * 100); int centre = getWidth() / 2; // 獲取圓心的x坐標(biāo) int radius = centre - mCircleWidth / 2;// 半徑 float textWidth = mPaint.measureText(percent + "%"); //測(cè)量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間 canvas.drawText(percent + "%",centre-textWidth/ 2,centre+textSize/2, mPaint); //畫出進(jìn)度百分比 mPaint.setStrokeWidth(mCircleWidth); // 設(shè)置圓環(huán)的寬度 mPaint.setAntiAlias(true); // 消除鋸齒 mPaint.setStyle(Paint.Style.STROKE); // 設(shè)置空心 RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定義的圓弧的形狀和大小的界限 if(isNext){ mPaint.setColor(mSecondColor); // 設(shè)置圓環(huán)的顏色 canvas.drawCircle(centre, centre, radius, mPaint); // 畫出圓環(huán) mPaint.setColor(mFirstColor); // 設(shè)置圓環(huán)的顏色 canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根據(jù)進(jìn)度畫圓弧 }else{ mPaint.setColor(mFirstColor); // 設(shè)置圓環(huán)的顏色 canvas.drawCircle(centre, centre, radius, mPaint); // 畫出圓環(huán) mPaint.setColor(mSecondColor); // 設(shè)置圓環(huán)的顏色 canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根據(jù)進(jìn)度畫圓弧 } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" > <view.CustomTitleView android:layout_width="200dp" android:layout_height="400dp" custom:mSpeed="50" custom:mFirstColor="#7300e6" custom:mSecondColor="#39ac39" custom:mCircleWidth="10px" custom:textSize="20px" android:id="@+id/one" /> <view.CustomTitleView android:layout_toEndOf="@id/one" android:layout_width="200dp" android:layout_height="400dp" custom:mSpeed="100" custom:mFirstColor="#0040ff" custom:mSecondColor="#40ff00" custom:mCircleWidth="20px" custom:textSize="30px" /> </RelativeLayout>
效果預(yù)覽
源碼下載:http://xiazai.jb51.net/201701/yuanma/AndroidProgressbar(jb51.net).rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義View實(shí)現(xiàn)帶數(shù)字的進(jìn)度條實(shí)例代碼
- Android 自定義view和屬性動(dòng)畫實(shí)現(xiàn)充電進(jìn)度條效果
- Android自定義View基礎(chǔ)開發(fā)之圖片加載進(jìn)度條
- Android自定義view實(shí)現(xiàn)水波紋進(jìn)度球效果
- Android自定義View仿華為圓形加載進(jìn)度條
- Android自定義View之圓形進(jìn)度條式按鈕
- Android自定義View弧線進(jìn)度控件
- Android自定義View實(shí)現(xiàn)漸變色進(jìn)度條
- Android自定義View實(shí)現(xiàn)環(huán)形進(jìn)度條的思路與實(shí)例
- Android自定義View實(shí)現(xiàn)簡(jiǎn)單炫酷的球體進(jìn)度球?qū)嵗a
相關(guān)文章
Android中使用imageviewswitcher 實(shí)現(xiàn)圖片切換輪播導(dǎo)航的方法
ImageSwitcher是Android中控制圖片展示效果的一個(gè)控件。本文給大家介紹Android中使用imageviewswitcher 實(shí)現(xiàn)圖片切換輪播導(dǎo)航的方法,需要的朋友參考下吧2016-12-12Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android實(shí)現(xiàn)對(duì)圖片放大、平移和旋轉(zhuǎn)的功能
現(xiàn)在很多App在查看一張圖片的原圖時(shí),都會(huì)支持圖片的手勢(shì)縮放,手勢(shì)平移以及圖片旋轉(zhuǎn)的操作。那么今天小編就來教大家去簡(jiǎn)單的實(shí)現(xiàn)圖片的放大、平移、旋轉(zhuǎn)的操作,有需要的可以參考借鑒。2016-08-08Android仿新浪微博發(fā)布微博界面設(shè)計(jì)(5)
這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博發(fā)布微博界面設(shè)計(jì)方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android實(shí)現(xiàn)基于ZXing快速集成二維碼掃描功能
這篇文章主要為大家詳細(xì)介紹了Android二維碼掃描ZXing快速項(xiàng)目集成的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android編程之非調(diào)用系統(tǒng)界面實(shí)現(xiàn)發(fā)送彩信的方法(MMS)
這篇文章主要介紹了Android編程之非調(diào)用系統(tǒng)界面實(shí)現(xiàn)發(fā)送彩信的方法,涉及Android源碼中的mms的使用技巧,需要的朋友可以參考下2016-01-01Flutter?繪制風(fēng)車實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Flutter?繪制風(fēng)車實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11