溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么在Android應(yīng)用中添加一個(gè)圓形進(jìn)度條效果

發(fā)布時(shí)間:2020-12-05 16:42:17 來(lái)源:億速云 閱讀:107 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

這篇文章給大家介紹怎么在Android應(yīng)用中添加一個(gè)圓形進(jìn)度條效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

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

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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>

上我們自定義類(lèi)的實(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; //畫(huà)筆
 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è)置的屬性,如果沒(méi)有設(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à)背景圓環(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è)置抗鋸齒

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

  //畫(huà)進(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 + "%";
  //獲取畫(huà)筆的文字屬性,總共有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);
  }
  //畫(huà)圓弧
  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);
  //話(huà)進(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();
   }
  });

關(guān)于怎么在Android應(yīng)用中添加一個(gè)圓形進(jìn)度條效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI