成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Android實(shí)現(xiàn)帶數(shù)字的圓形進(jìn)度條(自定義進(jìn)度條)

開(kāi)發(fā)

10年積累的做網(wǎng)站、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有龍灣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

設(shè)計(jì)搞了一個(gè)帶圓形進(jìn)度的進(jìn)度條,在GitHub上逛了一圈,發(fā)現(xiàn)沒(méi)有,自己擼吧。

先看界面效果:

Android實(shí)現(xiàn)帶數(shù)字的圓形進(jìn)度條(自定義進(jìn)度條)

主要思路是寫(xiě)一個(gè)繼承ProgressBar的自定義View,不廢話,直接上代碼:

package com.fun.progressbarwithnumber;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;
public class HorizontalProgressBarWithNumber extends ProgressBar {
  private static final int DEFAULT_TEXT_SIZE = 10;
  private static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1;
  private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da;
  private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;
  private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;
  private static final int DEFAULT_CIRCLE_COLOR = 0XFF3F51B5;
  protected Paint mPaint = new Paint();
  // 字體顏色
  protected int mTextColor = DEFAULT_TEXT_COLOR;
  // 字體大小
  protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
  // 覆蓋進(jìn)度高度
  protected int mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);
  // 覆蓋進(jìn)度顏色
  protected int mReachedBarColor = DEFAULT_TEXT_COLOR;
  // 未覆蓋進(jìn)度高度
  protected int mUnReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);
  // 未覆蓋進(jìn)度顏色
  protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;
  // 圓的顏色
  protected int mCircleColor = DEFAULT_CIRCLE_COLOR;
  protected int mRealWidth;
  protected boolean mIfDrawText = true;
  protected boolean mIfDrawCircle = true;
  protected static final int VISIBLE = 0;
  public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
  public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    obtainStyledAttributes(attrs);
    mPaint.setTextSize(mTextSize);
    mPaint.setColor(mTextColor);
    mPaint.setAntiAlias(true);
  }
  private void obtainStyledAttributes(AttributeSet attrs) {
    // 獲取自定義屬性
    final TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBarWithNumber);
    mTextColor = attributes.getColor(R.styleable.HorizontalProgressBarWithNumber_progress_text_color, DEFAULT_TEXT_COLOR);
    mTextSize = (int) attributes.getDimension(R.styleable.HorizontalProgressBarWithNumber_progress_text_size, mTextSize);
    mCircleColor = attributes.getColor(R.styleable.HorizontalProgressBarWithNumber_progress_circle_color, DEFAULT_CIRCLE_COLOR);
    mReachedBarColor = attributes.getColor(R.styleable.HorizontalProgressBarWithNumber_progress_reached_color, mTextColor);
    mUnReachedBarColor = attributes.getColor(R.styleable.HorizontalProgressBarWithNumber_progress_unreached_color, DEFAULT_COLOR_UNREACHED_COLOR);
    mReachedProgressBarHeight = (int) attributes.getDimension(R.styleable.HorizontalProgressBarWithNumber_progress_reached_bar_height, mReachedProgressBarHeight);
    mUnReachedProgressBarHeight = (int) attributes.getDimension(R.styleable.HorizontalProgressBarWithNumber_progress_unreached_bar_height, mUnReachedProgressBarHeight);
    int textVisible = attributes.getInt(R.styleable.HorizontalProgressBarWithNumber_progress_text_visibility, VISIBLE);
    if (textVisible != VISIBLE) {
      mIfDrawText = false;
    }
    attributes.recycle();
    int left = (int) (mReachedProgressBarHeight * 0.8), right = (int) (mReachedProgressBarHeight * 0.8);
    int top = (int) (mReachedProgressBarHeight * 0.3 + dp2px(1)), bottom = (int) (mReachedProgressBarHeight * 0.3 + dp2px(1));
    setPadding(left, top, right, bottom);
  }
  @Override
  protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = measureHeight(heightMeasureSpec);
    setMeasuredDimension(width, height);
    mRealWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
  }
  private int measureHeight(int measureSpec) {
    int result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);
    if (specMode == MeasureSpec.EXACTLY) {
      result = specSize;
    } else {
      float textHeight = (mPaint.descent() - mPaint.ascent());
      result = (int) (getPaddingTop() + getPaddingBottom() + Math.max(
          Math.max(mReachedProgressBarHeight, mUnReachedProgressBarHeight), Math.abs(textHeight)));
      if (specMode == MeasureSpec.AT_MOST) {
        result = Math.min(result, specSize);
      }
    }
    return result;
  }
  @Override
  protected synchronized void onDraw(Canvas canvas) {
    canvas.save();
    canvas.translate(getPaddingLeft(), getHeight() / 2);
    boolean noNeedBg = false;
    float radio = getProgress() * 1.0f / getMax();
    float progressPosX = (int) (mRealWidth * radio);
    String text = getProgress() + "%";
    float textWidth = mPaint.measureText(text);
    float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;
    float radius = (mReachedProgressBarHeight + getPaddingBottom() + getPaddingTop()) / 2;
    // 覆蓋的進(jìn)度
    float endX = progressPosX;
    if (endX > -1) {
      mPaint.setColor(mReachedBarColor);
      RectF rectF = new RectF(0, 0 - getPaddingTop() - getPaddingBottom(),
          endX, mReachedProgressBarHeight - getPaddingBottom());
      canvas.drawRoundRect(rectF, 25, 25, mPaint);
    }
    // 未覆蓋的進(jìn)度
    if (!noNeedBg) {
      float start = progressPosX;
      mPaint.setColor(mUnReachedBarColor);
      RectF rectF = new RectF(start, 0 - getPaddingTop() - getPaddingBottom(),
          mRealWidth + getPaddingRight() - radius, mReachedProgressBarHeight - getPaddingBottom());
      canvas.drawRoundRect(rectF, 25, 25, mPaint);
    }
    // 圓
    if (mIfDrawCircle) {
      mPaint.setColor(mCircleColor);
      canvas.drawCircle(progressPosX, 0, radius, mPaint);
    }
    // 文本
    if (mIfDrawText) {
      mPaint.setColor(mTextColor);
      canvas.drawText(text, progressPosX - textWidth / 2, -textHeight, mPaint);
    }
    canvas.restore();
  }
  /**
   * dp 2 px
   */
  protected int dp2px(int dpVal) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
  }
  /**
   * sp 2 px
   */
  protected int sp2px(int spVal) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, getResources().getDisplayMetrics());
  }
}

使用

在布局文件中加入:

<com.fun.progressbarwithnumber.HorizontalProgressBarWithNumber
    android:id="@+id/hpbwn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    fun:progress_circle_color="#ff000000"
    fun:progress_reached_bar_height="20dp"
    fun:progress_reached_color="#FFFF4081"
    fun:progress_text_color="#ffffffff"
    fun:progress_text_size="14sp"
    fun:progress_unreached_bar_height="20dp"
    fun:progress_unreached_color="#ffBCB4E8" />

progress_reached_bar_height:當(dāng)前進(jìn)度的高度
progress_unreached_bar_height:剩余進(jìn)度的高度
progress_text_size:圓圈內(nèi)文字的大小

注意:

當(dāng)前進(jìn)度和剩余進(jìn)度的高度要一致,圓圈大小和圓圈內(nèi)文字的大小要配合Java代碼調(diào)整。

項(xiàng)目源碼:

https://github.com/hfrommane/ProgressBarWithNumber

以上所述是小編給大家介紹的Android實(shí)現(xiàn)帶數(shù)字的圓形進(jìn)度條(自定義進(jìn)度條),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

分享標(biāo)題:Android實(shí)現(xiàn)帶數(shù)字的圓形進(jìn)度條(自定義進(jìn)度條)
路徑分享:http://jinyejixie.com/article2/pdsoic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)、品牌網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈、搜索引擎優(yōu)化、ChatGPT、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
石棉县| 武清区| 临夏县| 碌曲县| 巴塘县| 贵溪市| 西峡县| 独山县| 宝应县| 周宁县| 嘉鱼县| 巫山县| 商南县| 连南| 肥乡县| 平原县| 乌拉特后旗| 永城市| 漳浦县| 南京市| 赤城县| 金川县| 濮阳市| 潼关县| 高密市| 五原县| 英德市| 万安县| 长乐市| 托克逊县| 洛阳市| 祥云县| 清苑县| 衡南县| 会理县| 靖宇县| 塔河县| 乳山市| 西林县| 淮阳县| 宁都县|