今天就跟大家聊聊有關(guān)android開發(fā)中使用view實現(xiàn)自定義一個進度條功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
作為一家“創(chuàng)意+整合+營銷”的成都網(wǎng)站建設(shè)機構(gòu),我們在業(yè)內(nèi)良好的客戶口碑。創(chuàng)新互聯(lián)建站提供從前期的網(wǎng)站品牌分析策劃、網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、創(chuàng)意表現(xiàn)、網(wǎng)頁制作、系統(tǒng)開發(fā)以及后續(xù)網(wǎng)站營銷運營等一系列服務(wù),幫助企業(yè)打造創(chuàng)新的互聯(lián)網(wǎng)品牌經(jīng)營模式與有效的網(wǎng)絡(luò)營銷方法,創(chuàng)造更大的價值。
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)前進度 */ private int mProgress; /** * 第一圈的顏色 */ private int mFirstColor; /** * 第二圈的顏色 */ private int mSecondColor; /** * 圈的寬度 */ private int mCircleWidth; /** * 速度 */ private int mSpeed; /** * 中間進度百分比的字符串的字體 */ 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);// 默認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) { /** * 畫進度百分比 */ 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坐標 int radius = centre - mCircleWidth / 2;// 半徑 float textWidth = mPaint.measureText(percent + "%"); //測量字體寬度,我們需要根據(jù)字體的寬度設(shè)置在圓環(huán)中間 canvas.drawText(percent + "%",centre-textWidth/ 2,centre+textSize/2, mPaint); //畫出進度百分比 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ù)進度畫圓弧 }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ù)進度畫圓弧 } }
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ù)覽
看完上述內(nèi)容,你們對android開發(fā)中使用view實現(xiàn)自定義一個進度條功能有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前題目:android開發(fā)中使用view實現(xiàn)自定義一個進度條功能
當(dāng)前URL:http://sd-ha.com/article10/jgsjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、響應(yīng)式網(wǎng)站、網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)、、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)