91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

Android自定義view漸變圓形動畫

發布時間:2020-09-27 06:59:56 來源:腳本之家 閱讀:165 作者:halaoda 欄目:移動開發

本文實例為大家分享了Android自定義view漸變圓形動畫的具體代碼,供大家參考,具體內容如下

直接上效果圖

Android自定義view漸變圓形動畫

自定義屬性

attrs.xml文件

<resources>
  <declare-styleable name="ProgressRing">
    <!--進度起始色-->
    <attr name="pr_progress_start_color" format="color" />
    <!--進度結束色-->
    <attr name="pr_progress_end_color" format="color" />
    <!--背景起始色-->
    <attr name="pr_bg_start_color" format="color" />
    <!--背景中間色-->
    <attr name="pr_bg_mid_color" format="color" />
    <!--背景結束色-->
    <attr name="pr_bg_end_color" format="color" />
    <!--進度值 介于0-100-->
    <attr name="pr_progress" format="integer" />
    <!--進度寬度-->
    <attr name="pr_progress_width" format="dimension" />
    <!--起始角度-->
    <attr name="pr_start_angle" format="integer" />
    <!--掃過的角度-->
    <attr name="pr_sweep_angle" format="integer" />
    <!--是否顯示動畫-->
    <attr name="pr_show_anim" format="boolean" />
  </declare-styleable>

</resources>

創建一個類 ProgressRing繼承自 view

public class ProgressRing extends View {

  private int progressStartColor;
  private int progressEndColor;
  private int bgStartColor;
  private int bgMidColor;
  private int bgEndColor;
  private int progress;
  private float progressWidth;
  private int startAngle;
  private int sweepAngle;
  private boolean showAnim;

  private int mMeasureHeight;
  private int mMeasureWidth;

  private Paint bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
  private Paint progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);

  private RectF pRectF;

  private float unitAngle;

  private int curProgress = 0;

  public ProgressRing(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ProgressRing);
    progressStartColor = ta.getColor(R.styleable.ProgressRing_pr_progress_start_color, Color.YELLOW);
    progressEndColor = ta.getColor(R.styleable.ProgressRing_pr_progress_end_color, progressStartColor);
    bgStartColor = ta.getColor(R.styleable.ProgressRing_pr_bg_start_color, Color.LTGRAY);
    bgMidColor = ta.getColor(R.styleable.ProgressRing_pr_bg_mid_color, bgStartColor);
    bgEndColor = ta.getColor(R.styleable.ProgressRing_pr_bg_end_color, bgStartColor);
    progress = ta.getInt(R.styleable.ProgressRing_pr_progress, 0);
    progressWidth = ta.getDimension(R.styleable.ProgressRing_pr_progress_width, 8f);
    startAngle = ta.getInt(R.styleable.ProgressRing_pr_start_angle, 150);
    sweepAngle = ta.getInt(R.styleable.ProgressRing_pr_sweep_angle, 240);
    showAnim = ta.getBoolean(R.styleable.ProgressRing_pr_show_anim, true);
    ta.recycle();

    unitAngle = (float) (sweepAngle / 100.0);

    bgPaint.setStyle(Paint.Style.STROKE);
    bgPaint.setStrokeCap(Paint.Cap.ROUND);
    bgPaint.setStrokeWidth(progressWidth);

    progressPaint.setStyle(Paint.Style.STROKE);
    progressPaint.setStrokeCap(Paint.Cap.ROUND);
    progressPaint.setStrokeWidth(progressWidth);

  }

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

    /*for (int i = 0, end = (int) (progress * unitAngle); i <= end; i++) {
      progressPaint.setColor(getGradient(i / (float) end, progressStartColor, progressEndColor));
      canvas.drawArc(pRectF,
          startAngle + i,
          1,
          false,
          progressPaint);
    }*/
    if (!showAnim) {
      curProgress = progress;
    }

    drawBg(canvas);
    drawProgress(canvas);

    if (curProgress < progress) {
      curProgress++;
      postInvalidate();
    }
  }

  // 只需要畫進度之外的背景即可
  private void drawBg(Canvas canvas) {
    float halfSweep = sweepAngle / 2;
    for (int i = sweepAngle, st = (int) (curProgress * unitAngle); i > st; --i) {
      if (i - halfSweep > 0) {
        bgPaint.setColor(getGradient((i - halfSweep) / halfSweep, bgMidColor, bgEndColor));
      } else {
        bgPaint.setColor(getGradient((halfSweep - i) / halfSweep, bgMidColor, bgStartColor));
      }
      canvas.drawArc(pRectF,
          startAngle + i,
          1,
          false,
          bgPaint);
    }
  }

  private void drawProgress(Canvas canvas) {
    for (int i = 0, end = (int) (curProgress * unitAngle); i <= end; i++) {
      progressPaint.setColor(getGradient(i / (float) end, progressStartColor, progressEndColor));
      canvas.drawArc(pRectF,
          startAngle + i,
          1,
          false,
          progressPaint);
    }
  }

  public void setProgress(@IntRange(from = 0, to = 100) int progress) {
    this.progress = progress;
    invalidate();
  }

  public int getProgress() {
    return progress;
  }

  public int getGradient(float fraction, int startColor, int endColor) {
    if (fraction > 1) fraction = 1;
    int alphaStart = Color.alpha(startColor);
    int redStart = Color.red(startColor);
    int blueStart = Color.blue(startColor);
    int greenStart = Color.green(startColor);
    int alphaEnd = Color.alpha(endColor);
    int redEnd = Color.red(endColor);
    int blueEnd = Color.blue(endColor);
    int greenEnd = Color.green(endColor);
    int alphaDifference = alphaEnd - alphaStart;
    int redDifference = redEnd - redStart;
    int blueDifference = blueEnd - blueStart;
    int greenDifference = greenEnd - greenStart;
    int alphaCurrent = (int) (alphaStart + fraction * alphaDifference);
    int redCurrent = (int) (redStart + fraction * redDifference);
    int blueCurrent = (int) (blueStart + fraction * blueDifference);
    int greenCurrent = (int) (greenStart + fraction * greenDifference);
    return Color.argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mMeasureWidth = getMeasuredWidth();
    mMeasureHeight = getMeasuredHeight();
    if (pRectF == null) {
      float halfProgressWidth = progressWidth / 2;
      pRectF = new RectF(halfProgressWidth + getPaddingLeft(),
          halfProgressWidth + getPaddingTop(),
          mMeasureWidth - halfProgressWidth - getPaddingRight(),
          mMeasureHeight - halfProgressWidth - getPaddingBottom());
    }

  }
}

xml布局

<myCircle.ProgressRing
  android:layout_width="320dp"
  android:layout_height="320dp"
  android:layout_gravity="center_horizontal"
  app:pr_bg_end_color="#00ffffff"
  app:pr_bg_mid_color="#CCCCCC"
  app:pr_bg_start_color="#00ffffff"
  app:pr_progress="70"
  app:pr_progress_end_color="#F78930"
  app:pr_progress_start_color="#00ffffff"
  app:pr_progress_width="40dp" />

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

向AI問一下細節

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

AI

通海县| 奎屯市| 民权县| 晋宁县| 渭南市| 沐川县| 延庆县| 徐汇区| 时尚| 高邑县| 武安市| 台南市| 黄平县| 武陟县| 沽源县| 仁化县| 景泰县| 柳州市| 桑日县| 静安区| 松阳县| 浙江省| 修水县| 平江县| 德格县| 醴陵市| 邹城市| 威信县| 焦作市| 余干县| 乐陵市| 东乌珠穆沁旗| 宿州市| 永新县| 耿马| 鸡泽县| 宣汉县| 新泰市| 志丹县| 台前县| 阿瓦提县|