国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義圓形進度條

2019-12-12 02:48:05
字體:
來源:轉載
供稿:網友

今天小編來手寫一個自定義圓形進度條:先看效果:

首先我們在attrs屬性文件中增加幾個自定義屬性

<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CustomProgressBar">  <!-- 圓形進度條進度顯示的顏色 -->  <attr name="roundProgressColor" format="color"></attr>  <!-- 外圈圓的顏色 -->  <attr name="roundColor" format="color"></attr>  <!-- 圓的總寬度 -->  <attr name="roundWidth" format="dimension"></attr>  <!-- 字體顯示的大小 -->  <attr name="textSize" format="dimension"></attr>  <!-- 字體顯示的顏色 -->  <attr name="textColor" format="color"></attr>  <!-- 進度的最大值 -->  <attr name="max" format="integer"></attr>  <!-- 是否顯示文字 -->  <attr name="textShow" format="boolean"></attr> </declare-styleable></resources>

上我們自定義類的實現代碼:

package xxx.xxx.xxx;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Typeface;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import test.dn.com.dn_test.R;/** * Created by Administrator on 2017/5/16 0016. */public class CircleProgressBar extends View { private int max; //最大值 private int roundColor; //圓形進度條的顏色 private int roundProgressColor;//圓形進度條進度的顏色 private int textColor;  //字體的顏色 private float textSize;  //字體的大小 private float roundWidth; //圓的寬度 private boolean textShow; //是否顯示圓 private int progress; //當前進度 private Paint mPaint; //畫筆 public static final int STROKE = 0; public static final int FILL = 1; public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {  super(context, attrs);  //初始化一只筆  mPaint = new Paint();  //獲取xml當中設置的屬性,如果沒有設置,則設置一個默認值  TypedArray typedArray = context.obtainStyledAttributes(attrs , R.styleable.CustomProgressBar);  max = typedArray.getInteger(R.styleable.CustomProgressBar_max , 100);  roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.RED);  roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor , Color.BLUE);  textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor , Color.GREEN);  textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize , 55);  roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth , 10);  textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow , true); } @Override protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  //畫背景圓環  int center = getWidth() / 2;  //設置半徑  float radius = center - roundWidth / 2;  //設置圓圈的顏色  mPaint.setColor(roundColor);  mPaint.setStyle(Paint.Style.STROKE);  mPaint.setStrokeWidth(roundWidth);//圓環的寬度  mPaint.setAntiAlias(true);//設置抗鋸齒  //畫外圈  canvas.drawCircle(center , center ,radius , mPaint);  //畫進度百分比  mPaint.setColor(textColor);  mPaint.setStrokeWidth(0);  //設置字體大小  mPaint.setTextSize(textSize);  mPaint.setTypeface(Typeface.DEFAULT);  //設置筆帽  mPaint.setStrokeCap(Paint.Cap.ROUND);  //設置文字的擺放方式為居中  mPaint.setTextAlign(Paint.Align.CENTER);  //獲取當前進度的值  int percent = (int) (progress / (float)max * 100);  String strPercent = percent + "%";  //獲取畫筆的文字屬性,總共有bottom , top , leading , ascent , descent 這個以后會詳細講解  Paint.FontMetricsInt fm = mPaint.getFontMetricsInt();  if(percent != 0){   canvas.drawText(strPercent , getWidth() / 2 ,     getWidth() / 2 + (fm.bottom - fm.top) / 2 - fm.bottom, mPaint);  }  //畫圓弧  RectF oval = new RectF(center - radius , center - radius ,center + radius , center + radius);  mPaint.setColor(roundProgressColor);  mPaint.setStrokeWidth(roundWidth);  mPaint.setStyle(Paint.Style.STROKE);  //設置筆帽  mPaint.setStrokeCap(Paint.Cap.ROUND);  //話進度  canvas.drawArc(oval , 0 , 360 * progress / max , false , mPaint); } public void setProgress(int progress){  if(progress < 0){   throw new IllegalArgumentException("進度progress不能小于0");  }  if(progress > max){   progress = max;  }  if(progress <= max){   this.progress = progress;   postInvalidate();  } }}

在我們的xml中設置控件:

 <xxx.xxx.CircleProgressBar  android:id="@+id/progressbar"  android:layout_width="100dp"  android:layout_height="100dp"  app:roundProgressColor="#ff00ff"  app:textColor="#666666"  app:textSize="20dp"  app:roundWidth="15dp"  />

Activity功能實現代碼:

mProgressBar = (CircleProgressBar) findViewById(R.id.progressbar);  mProgressBar.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {    //模擬http請求    new Thread(new Runnable() {     @Override     public void run() {      while (progress <= 100){       progress += 2;       mProgressBar.setProgress(progress);       //模擬網絡請求,每隔100毫秒增加一個進度       SystemClock.sleep(100);      }     }    }).start();   }  });

完結!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柘城县| 怀柔区| 阜城县| 阿克| 万山特区| 永宁县| 石门县| 巴青县| 巴马| 象山县| 合山市| 井陉县| 桃源县| 徐水县| 晋江市| 临澧县| 诸城市| 屏山县| 连江县| 宿松县| 保德县| 囊谦县| 海阳市| 论坛| 苍溪县| 沈阳市| 曲沃县| 旌德县| 铜鼓县| 福鼎市| 文水县| 竹北市| 哈尔滨市| 肃南| 林州市| 阜城县| 玛多县| 安福县| 安塞县| 东平县| 景谷|