1.本文將向你介紹自定義進(jìn)度條的寫(xiě)法,比較簡(jiǎn)單,但還是有些知識(shí)點(diǎn)是需要注意的:
invalidate()方法
RectF方法的應(yīng)用
onMeasure方法的應(yīng)用
2.原理
畫(huà)3層圓角矩形,底層為黑色,第二層為灰色,最上一層為進(jìn)度條顏色,示例圖如下:

3.效果圖
實(shí)現(xiàn)圓角進(jìn)度條還有很多其他方法,比如在Progress控件里填充圓角圖片,通過(guò)拉伸圖片來(lái)達(dá)到預(yù)想的效果,雖然聽(tīng)起來(lái)很簡(jiǎn)單,但實(shí)現(xiàn)起來(lái)還是有些麻煩的。
4.解說(shuō)方法
(1)invalidate()方法
invalidate()是用來(lái)刷新View的,必須是在UI線(xiàn)程中進(jìn)行工作。比如在修改某個(gè)view的顯示時(shí), 調(diào)用invalidate()才能看到重新繪制的界面。invalidate()的調(diào)用是把之前的舊的view從主UI線(xiàn)程隊(duì)列中pop掉。一般在自定義控件中會(huì)用到這個(gè)方法。
(2)RectF方法的應(yīng)用
RectF是用來(lái)繪畫(huà)矩形的方法。
RectF(left,top,right,bottom),四個(gè)參數(shù)的含義分別是父控件距離矩形左上右下邊距的距離,以下用圖來(lái)說(shuō)明:
drawRoundRect方法是用來(lái)繪制圓角矩形的,它的參數(shù)如下:
參數(shù)說(shuō)明
rect:RectF對(duì)象。
rx:x方向上的圓角半徑。
ry:y方向上的圓角半徑。
paint:繪制時(shí)所使用的畫(huà)筆。
(3)onMeasure方法
指定自定義控件在屏幕上的大小,onMeasure方法的兩個(gè)參數(shù)是由上一層控件 傳入的大小,而且是模式和尺寸混合在一起的數(shù)值,需要MeasureSpec.getMode(widthMeasureSpec) 得到模式,MeasureSpec.getSize(widthMeasureSpec)得到尺寸。
onMeasure的幾種模式分別為EXACTLY,AT_MOST,UNSPECIFIED。
[1]MeasureSpec.EXACTLY
MeasureSpec.EXACTLY是精確尺寸,當(dāng)我們將控件的layout_width或layout_height指定為具體數(shù)值時(shí)如andorid:layout_width=”50dip”,或者為FILL_PARENT是,都是控件大小已經(jīng)確定的情況,都是精確尺寸。
[2]MeasureSpec.AT_MOST
MeasureSpec.AT_MOST是最大尺寸,當(dāng)控件的layout_width或layout_height指定為WRAP_CONTENT時(shí),控件大小一般隨著控件的子空間或內(nèi)容進(jìn)行變化,此時(shí)控件尺寸只要不超過(guò)父控件允許的最大尺寸即可。因此,此時(shí)的mode是AT_MOST,size給出了父控件允許的最大尺寸。
[3]MeasureSpec.UNSPECIFIED
MeasureSpec.UNSPECIFIED是未指定尺寸,這種情況不多,一般都是父控件是AdapterView,通過(guò)measure方法傳入的模式。
5.activity_main.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.progresstest.MainActivity"tools:ignore="MergeRootFrame" ><com.example.progresstest.ProgressViewTest android:id="@+id/progressbar"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
6.ProgressViewTest.java文件
package com.example.progresstest;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;public class ProgressViewTest extends View {/**進(jìn)度條最大值*/private float maxCount;/**進(jìn)度條當(dāng)前值*/private float currentCount;/**畫(huà)筆*/private Paint mPaint;private int mWidth,mHeight;public ProgressViewTest(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public ProgressViewTest(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public ProgressViewTest(Context context) {super(context);// TODO Auto-generated constructor stub}/**** 設(shè)置最大的進(jìn)度值* @param maxCount*/public void setMaxCount(float maxCount) {this.maxCount = maxCount;}/*** 得到最大進(jìn)度值*/public double getMaxCount(){return maxCount;}/**** 設(shè)置當(dāng)前的進(jìn)度值* @param currentCount*/public void setCurrentCount(float currentCount) {this.currentCount = currentCount > maxCount ? maxCount : currentCount;/*** invalidate()是用來(lái)刷新View的,必須是在UI線(xiàn)程中進(jìn)行工作。比如在修改某個(gè)view的顯示時(shí),* 調(diào)用invalidate()才能看到重新繪制的界面。invalidate()的調(diào)用是把之前的舊的view從主UI* 線(xiàn)程隊(duì)列中pop掉。 */invalidate();}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);mPaint = new Paint();//設(shè)置抗鋸齒效果mPaint.setAntiAlias(true);//設(shè)置畫(huà)筆顏色mPaint.setColor(Color.BLACK);int round = mHeight/2;/*** RectF:繪制矩形,四個(gè)參數(shù)分別是left,top,right,bottom* 類(lèi)型是單精度浮點(diǎn)數(shù)*/RectF rf = new RectF(0, 0, mWidth, mHeight);/*繪制圓角矩形,背景色為畫(huà)筆顏色*/canvas.drawRoundRect(rf, round, round, mPaint);/*設(shè)置progress內(nèi)部是灰色*/mPaint.setColor(Color.rgb(211, 211, 211));RectF rectBlackBg = new RectF(2, 2, mWidth-2, mHeight-2);canvas.drawRoundRect(rectBlackBg, round, round, mPaint);//設(shè)置進(jìn)度條進(jìn)度及顏色float section = currentCount/maxCount;RectF rectProgressBg = new RectF(3, 3, (mWidth-3)*section, mHeight-3);if(section!=0.0f){mPaint.setColor(Color.GREEN);}else{mPaint.setColor(Color.TRANSPARENT);}canvas.drawRoundRect(rectProgressBg, round, round, mPaint);}//dip * scale + 0.5f * (dip >= 0 ? 1 : -1)private int dipToPx(int dip){float scale = getContext().getResources().getDisplayMetrics().density;return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));//加0.5是為了四舍五入}/**指定自定義控件在屏幕上的大小,onMeasure方法的兩個(gè)參數(shù)是由上一層控件* 傳入的大小,而且是模式和尺寸混合在一起的數(shù)值,需要MeasureSpec.getMode(widthMeasureSpec)* 得到模式,MeasureSpec.getSize(widthMeasureSpec)得到尺寸* */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);//MeasureSpec.EXACTLY,精確尺寸if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {mWidth = widthSpecSize;} else {mWidth = 0;}//MeasureSpec.AT_MOST,最大尺寸,只要不超過(guò)父控件允許的最大尺寸即可,MeasureSpec.UNSPECIFIED未指定尺寸if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {mHeight = dipToPx(20);} else {mHeight = heightSpecSize;}//設(shè)置控件實(shí)際大小setMeasuredDimension(mWidth, mHeight);}}MainActivity.java文件
package com.example.progresstest;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.os.Handler;import android.os.Message;public class MainActivity extends ActionBarActivity {private ProgressViewTest progress;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);progress = (ProgressViewTest) findViewById(R.id.progressbar);progress.setMaxCount(100);new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i <=progress.getMaxCount(); i++) {Message msg = new Message();msg.arg1 = i;msg.what = 0x01;handler.sendMessage(msg);try {//每隔0.1秒進(jìn)度前進(jìn)1Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}).start();}Handler handler = new Handler(){public void handleMessage(Message msg) {if(msg.what==0x01){progress.setCurrentCount(msg.arg1);}};};}以上所述是小編給大家介紹的Android自定義進(jìn)度條的圓角橫向進(jìn)度條實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注