最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義圓形進(jìn)度條

 更新時(shí)間:2021年04月19日 09:47:21   作者:銀伙計(jì)  
這篇文章主要為大家詳細(xì)介紹了Android自定義圓形進(jìn)度條的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

今天小編來手寫一個(gè)自定義圓形進(jìn)度條:先看效果:

首先我們?cè)赼ttrs屬性文件中增加幾個(gè)自定義屬性

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <declare-styleable name="CustomProgressBar">
  <!-- 圓形進(jìn)度條進(jìn)度顯示的顏色 -->
  <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>
  <!-- 進(jìn)度的最大值 -->
  <attr name="max" format="integer"></attr>
  <!-- 是否顯示文字 -->
  <attr name="textShow" format="boolean"></attr>
 </declare-styleable>

</resources>

上我們自定義類的實(shí)現(xiàn)代碼:

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; //圓形進(jìn)度條的顏色
 private int roundProgressColor;//圓形進(jìn)度條進(jìn)度的顏色
 private int textColor;  //字體的顏色
 private float textSize;  //字體的大小
 private float roundWidth; //圓的寬度
 private boolean textShow; //是否顯示圓
 private int progress; //當(dāng)前進(jìn)度
 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當(dāng)中設(shè)置的屬性,如果沒有設(shè)置,則設(shè)置一個(gè)默認(rèn)值
  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);
  //畫背景圓環(huán)
  int center = getWidth() / 2;
  //設(shè)置半徑
  float radius = center - roundWidth / 2;
  //設(shè)置圓圈的顏色
  mPaint.setColor(roundColor);
  mPaint.setStyle(Paint.Style.STROKE);
  mPaint.setStrokeWidth(roundWidth);//圓環(huán)的寬度
  mPaint.setAntiAlias(true);//設(shè)置抗鋸齒

  //畫外圈
  canvas.drawCircle(center , center ,radius , mPaint);

  //畫進(jìn)度百分比
  mPaint.setColor(textColor);
  mPaint.setStrokeWidth(0);
  //設(shè)置字體大小
  mPaint.setTextSize(textSize);
  mPaint.setTypeface(Typeface.DEFAULT);
  //設(shè)置筆帽
  mPaint.setStrokeCap(Paint.Cap.ROUND);
  //設(shè)置文字的擺放方式為居中
  mPaint.setTextAlign(Paint.Align.CENTER);
  //獲取當(dāng)前進(jìn)度的值
  int percent = (int) (progress / (float)max * 100);
  String strPercent = percent + "%";
  //獲取畫筆的文字屬性,總共有bottom , top , leading , ascent , descent 這個(gè)以后會(huì)詳細(xì)講解
  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);
  //設(shè)置筆帽
  mPaint.setStrokeCap(Paint.Cap.ROUND);
  //話進(jìn)度
  canvas.drawArc(oval , 0 , 360 * progress / max , false , mPaint);
 }

 public void setProgress(int progress){
  if(progress < 0){
   throw new IllegalArgumentException("進(jìn)度progress不能小于0");
  }
  if(progress > max){
   progress = max;
  }
  if(progress <= max){
   this.progress = progress;
   postInvalidate();
  }

 }
}

在我們的xml中設(shè)置控件:

 <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功能實(shí)現(xiàn)代碼:

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

完結(jié)!

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼

    Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼

    這篇文章主要介紹了Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android自定義帶動(dòng)畫效果的圓形ProgressBar

    Android自定義帶動(dòng)畫效果的圓形ProgressBar

    這篇文章主要為大家詳細(xì)介紹了Android自定義帶動(dòng)畫效果的圓形ProgressBar,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android源碼中final關(guān)鍵字的用法及final,finally,finalize的區(qū)別

    Android源碼中final關(guān)鍵字的用法及final,finally,finalize的區(qū)別

    Android的源碼中很多地方對(duì)final關(guān)鍵字的用法很是“別出心裁”,之所以這么說是因?yàn)槲覐臎]看過是這么使用final關(guān)鍵字的,通過本文給大家分享Android源碼中final關(guān)鍵字的用法及final,finally,finalize的區(qū)別,感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android App中各種數(shù)據(jù)保存方式的使用實(shí)例總結(jié)

    Android App中各種數(shù)據(jù)保存方式的使用實(shí)例總結(jié)

    這篇文章主要介紹了Android App中各種數(shù)據(jù)保存方式的使用實(shí)例,列舉了SharedPreferences接口、機(jī)身空間存儲(chǔ)、SD卡存儲(chǔ)和SQLite數(shù)據(jù)庫四種方式的代碼例子,需要的朋友可以參考下
    2016-04-04
  • Android rom解包打包工具

    Android rom解包打包工具

    這篇文章主要介紹了Android rom解包打包工具的相關(guān)資料,對(duì)rom解包打包相關(guān)知識(shí)感興趣的朋友可以參考下
    2016-01-01
  • Android自定義ViewGroup多行多列效果

    Android自定義ViewGroup多行多列效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup多行多列效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android 添加TextView刪除線(代碼簡單)

    Android 添加TextView刪除線(代碼簡單)

    最近接了個(gè)項(xiàng)目,其中有項(xiàng)目需求是這樣的,有這么個(gè)需求,就是一個(gè)產(chǎn)品下有兩個(gè)價(jià)格,一個(gè)是市場價(jià),一個(gè)是銷售價(jià),這時(shí)要把市場價(jià)添加個(gè)刪除線;怎么實(shí)現(xiàn)呢?下面小編給大家分享一段簡單的代碼實(shí)現(xiàn)Android 添加TextView刪除線
    2016-02-02
  • Android 6.0指紋識(shí)別App開發(fā)案例

    Android 6.0指紋識(shí)別App開發(fā)案例

    這篇文章主要為大家詳細(xì)介紹了Android 6.0 指紋識(shí)別App開發(fā)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • android控件Spinner(下拉列表)的使用例子

    android控件Spinner(下拉列表)的使用例子

    這篇文章主要給大家介紹了關(guān)于android控件Spinner(下拉列表)的使用例子,在Android開發(fā)中下拉框(Spinner)是常用的UI控件之一,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • Android掃描本地音樂文件開發(fā)案例分享

    Android掃描本地音樂文件開發(fā)案例分享

    這篇文章主要為大家分享了Android掃描本地音樂文件開發(fā)案例,感興趣的小伙伴們可以參考一下
    2016-05-05

最新評(píng)論

扶沟县| 雅安市| 江川县| 石门县| 凤台县| 珠海市| 沂水县| 昔阳县| 贵南县| 枝江市| 碌曲县| 贺州市| 东丽区| 宽甸| 随州市| 喀喇沁旗| 赫章县| 盐山县| 上饶县| 彰武县| 武穴市| 武鸣县| 阿勒泰市| 拜泉县| 临沧市| 定西市| 滦平县| 东山县| 湖口县| 金溪县| 英超| 鄂温| 邢台县| 且末县| 衡南县| 绍兴县| 云和县| 中方县| 太和县| 新泰市| 稷山县|