中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android自定義圓弧進度條加數字動態變化的方法

發布時間:2020-07-22 16:10:24 來源:億速云 閱讀:183 作者:小豬 欄目:開發技術

這篇文章主要講解了Android自定義圓弧進度條加數字動態變化的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

效果如下:

思路:一個內環圓弧和一個外環圓弧,因為有一個圓圈是在圓弧上做圓周運動,所以在畫圓的時候必須要得到圓弧上的各個點的坐標,這里其實就用到了PathMeasure這個類,可以幫我們拿到這些點,在畫圓弧的時候也理所應當的要使用path,然后根據外界動態的傳值進行重繪就能達到動態的效果

代碼如下:

public class ProgressPathRainbow extends View {
  private Paint outPaint;
  private Paint innerPaint;
  private Paint mTextPaint;
  private Paint mRmbTextPaint;
  private int mBorderWidth = 40;
  private int mCircleRadius = 40;
  private int mCurrentProgress = 0;
  private int mMaxProgress = 0;
  private int startAngle = 180;

  private int sweepAngels = 180;
  private Paint mCirclePaint;
  private String rmb = "¥";
  private String currentText = "0.0";

  public void setCurrentText(String currentText) {
    this.currentText = currentText;
  }

  //儲存位置點
  private float[] pos =new float[2];

  public ProgressPathRainbow(Context context) {
    super(context);
    initPaint();
  }

  public ProgressPathRainbow(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initPaint();
  }

  public ProgressPathRainbow(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initPaint();
  }

  private void initPaint(){
    outPaint = new Paint();
    outPaint.setColor(0xFFECECEC);
    outPaint.setAntiAlias(true);
    outPaint.setStyle(Paint.Style.STROKE);
    outPaint.setStrokeCap(Paint.Cap.ROUND);
    outPaint.setStrokeWidth(mBorderWidth);

    //
    innerPaint = new Paint();
    innerPaint.setColor(0xffFBA123);
    innerPaint.setAntiAlias(true);
    innerPaint.setStyle(Paint.Style.STROKE);
    innerPaint.setStrokeCap(Paint.Cap.ROUND);
    innerPaint.setStrokeWidth(mBorderWidth);

    mCirclePaint = new Paint();
    mCirclePaint.setColor(Color.WHITE);
    mCirclePaint.setStyle(Paint.Style.FILL);

    mTextPaint = new Paint();
    mTextPaint.setAntiAlias(true);
    mTextPaint.setColor(0xffE5423D);
    mTextPaint.setFakeBoldText(true);
    mTextPaint.setTextSize(SizeUtils.sp2px(42));


    mRmbTextPaint = new Paint();
    mRmbTextPaint.setAntiAlias(true);
    mRmbTextPaint.setColor(0xffE5423D);
    mRmbTextPaint.setTextSize(SizeUtils.sp2px(18));

  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    if (width >= height){
      setMeasuredDimension(height,height);
    }else {
      setMeasuredDimension(width,width);
    }


  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    RectF rectF = new RectF(mBorderWidth,mBorderWidth,getWidth()-mBorderWidth,getHeight()-mBorderWidth);
    //畫內環圓弧
    Path outerPath = new Path();
    outerPath.arcTo(rectF,startAngle,sweepAngels);
    canvas.drawPath(outerPath,outPaint);
    //畫外環圓弧
    Path innerPah = new Path();
    float percent = (float)mCurrentProgress/mMaxProgress;
    innerPah.arcTo(rectF,startAngle,percent*sweepAngels);
    canvas.drawPath(innerPah,innerPaint);


    //畫金額
    String tempText = new BigDecimal(currentText).multiply(new BigDecimal(percent)).setScale(1, RoundingMode.HALF_UP).toString();
    Rect textBounds = new Rect();
    mTextPaint.getTextBounds(tempText, 0, tempText.length(), textBounds);
    int dx = getWidth()/2 - textBounds.width()/2;
    // 基線 baseLine
    Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
    int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
    int baseLine = getHeight()/3 + dy;
    canvas.drawText(tempText,dx,baseLine,mTextPaint);

    //畫人民幣符號

    Rect textBoundRmbs = new Rect();
    mTextPaint.getTextBounds(rmb, 0, rmb.length(), textBoundRmbs);
    int dxRmb = dx-50;
    // 基線 baseLine
    Paint.FontMetricsInt fontMetricsRmb = mTextPaint.getFontMetricsInt();
    int dyRmb = (fontMetricsRmb.bottom - fontMetricsRmb.top)/2 - fontMetricsRmb.bottom;
    int baseLineRmb = getHeight()/3 + dyRmb;
    canvas.drawText(rmb,dxRmb,baseLineRmb,mRmbTextPaint);

    //獲取圓弧上點的位置(坐標,畫一個圓)
    PathMeasure pathMeasure = new PathMeasure(outerPath,false);
    boolean posTan = pathMeasure.getPosTan(pathMeasure.getLength() * percent, pos, null);
    canvas.drawCircle(pos[0],pos[1],mCircleRadius,mCirclePaint);

  }

  public synchronized void setmCurrentProgress(int mCurrentProgress) {
    this.mCurrentProgress = mCurrentProgress;
    invalidate();
  }

  public synchronized void setmMaxProgress(int mMaxProgress) {
    this.mMaxProgress = mMaxProgress;
  }
}

以上就可以實現這個效果

使用的話可以這樣

detailRainbowPr.setmMaxProgress(100);
detailRainbowPr.setCurrentText("99.9");
ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, 100);
    valueAnimator.setDuration(5000);
    valueAnimator.setInterpolator(new DecelerateInterpolator());
    valueAnimator.addUpdateListener(valueAnimator1 -> {
      float step = (float) valueAnimator1.getAnimatedValue();
      detailRainbowPr.setmCurrentProgress((int) step);
    });
    valueAnimator.start();

看完上述內容,是不是對Android自定義圓弧進度條加數字動態變化的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

治多县| 班玛县| 濮阳市| 武义县| 惠安县| 霍林郭勒市| 河北区| 渭源县| 庆元县| 大厂| 临澧县| 南汇区| 贡觉县| 连州市| 和林格尔县| 荥经县| 长子县| 全椒县| 红原县| 南开区| 五原县| 东乡县| 沂南县| 石首市| 临桂县| 青州市| 博兴县| 北票市| 普陀区| 阿鲁科尔沁旗| 临朐县| 沙田区| 蓬安县| 南陵县| 石屏县| 濮阳县| 阜阳市| 林周县| 彰武县| 湘阴县| 阿合奇县|