溫馨提示×

溫馨提示×

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

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

怎么在Android中利用ProgressButton實現進度條按鈕

發布時間:2021-05-24 18:10:59 來源:億速云 閱讀:340 作者:Leah 欄目:移動開發

這期內容當中小編將會給大家帶來有關怎么在Android中利用ProgressButton實現進度條按鈕,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創建三個GradientDrawable作為按鈕背景、進度條背景和進度條前景,通過計算進度條的百分比來設置寬度,然后調用invalidate()重繪。GradientDrawable設置顏色、圓角等參數,當然你也可以直接加載xml作為背景。

3.自定義參數:

在values目錄建一個attrs.xml文件

 <?xml version="." encoding="utf-"?>
 <resources>
  <attr name="progressColor" format="color" />
  <attr name="progressBackColor" format="color" />
  <attr name="progress" format="integer" />
  <attr name="minProgress" format="integer" />
  <attr name="maxProgress" format="integer" />
  <declare-styleable name="ProgressButton">
   <attr name="progressColor" />
   <attr name="progressBackColor" />
   <attr name="buttonColor" format="color" />
   <attr name="cornerRadius" format="dimension" />
   <attr name="progress" />
   <attr name="minProgress" />
   <attr name="maxProgress" />
   <attr name="progressMargin" format="dimension" />
  </declare-styleable>
 </resources>

3.按鈕類:

在setProgress方法中改變mProgress的值,然后調用invalidate()重繪,因為我這里定義了一個minProgress(默認為0),所以在計算進度條寬度的時候,當前進度和最大進度都要先減去minProgress再做除法。

if (progressWidth < mCornerRadius * 2) {
 progressWidth = mCornerRadius * 2;
}

當進度條寬度小于2倍圓角半徑的時候,進度條的圓角就和背景的圓角不一致,所以加上了上面這段代碼。
獲取寬度和高度其實用getWidth()和getHeight()也可以,只不過在設計器中沒法看到效果,所以我用了getMeasuredWidth()和getMeasuredHeight()。

 package com.cloud.customviews;
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.drawable.GradientDrawable;
 import android.support.v.widget.AppCompatButton;
 import android.util.AttributeSet;
 public class ProgressButton extends AppCompatButton {
  private float mCornerRadius = ;
  private float mProgressMargin = ;
  private boolean mFinish;
  private int mProgress;
  private int mMaxProgress = ;
  private int mMinProgress = ;
  private GradientDrawable mDrawableButton;
  private GradientDrawable mDrawableProgressBackground;
  private GradientDrawable mDrawableProgress;
  public ProgressButton(Context context, AttributeSet attrs) {
   super(context, attrs);
   initialize(context, attrs);
  }
  public ProgressButton(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   initialize(context, attrs);
  }
  private void initialize(Context context, AttributeSet attrs) {
   //Progress background drawable
   mDrawableProgressBackground = new GradientDrawable();
   //Progress drawable
   mDrawableProgress = new GradientDrawable();
   //Normal drawable
   mDrawableButton = new GradientDrawable();
   //Get default normal color
   int defaultButtonColor = getResources().getColor(R.color.colorGray, null);
   //Get default progress color
   int defaultProgressColor = getResources().getColor(R.color.colorGreen, null);
   //Get default progress background color
   int defaultBackColor = getResources().getColor(R.color.colorGray, null);
   TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.ProgressButton);
   try {
    mProgressMargin = attr.getDimension(R.styleable.ProgressButton_progressMargin, mProgressMargin);
    mCornerRadius = attr.getDimension(R.styleable.ProgressButton_cornerRadius, mCornerRadius);
    //Get custom normal color
    int buttonColor = attr.getColor(R.styleable.ProgressButton_buttonColor, defaultButtonColor);
    //Set normal color
    mDrawableButton.setColor(buttonColor);
    //Get custom progress background color
    int progressBackColor = attr.getColor(R.styleable.ProgressButton_progressBackColor, defaultBackColor);
    //Set progress background drawable color
    mDrawableProgressBackground.setColor(progressBackColor);
    //Get custom progress color
    int progressColor = attr.getColor(R.styleable.ProgressButton_progressColor, defaultProgressColor);
    //Set progress drawable color
    mDrawableProgress.setColor(progressColor);
    //Get default progress
    mProgress = attr.getInteger(R.styleable.ProgressButton_progress, mProgress);
    //Get minimum progress
    mMinProgress = attr.getInteger(R.styleable.ProgressButton_minProgress, mMinProgress);
    //Get maximize progress
    mMaxProgress = attr.getInteger(R.styleable.ProgressButton_maxProgress, mMaxProgress);
   } finally {
    attr.recycle();
   }
   //Set corner radius
   mDrawableButton.setCornerRadius(mCornerRadius);
   mDrawableProgressBackground.setCornerRadius(mCornerRadius);
   mDrawableProgress.setCornerRadius(mCornerRadius - mProgressMargin);
   setBackgroundDrawable(mDrawableButton);
   mFinish = false;
  }
  @Override
  protected void onDraw(Canvas canvas) {
   if (mProgress > mMinProgress && mProgress <= mMaxProgress && !mFinish) {
    //Calculate the width of progress
    float progressWidth =
      (float) getMeasuredWidth() * ((float) (mProgress - mMinProgress) / mMaxProgress - mMinProgress);
    //If progress width less than x corner radius, the radius of progress will be wrong
    if (progressWidth < mCornerRadius * ) {
     progressWidth = mCornerRadius * ;
    }
    //Set rect of progress
    mDrawableProgress.setBounds((int) mProgressMargin, (int) mProgressMargin,
      (int) (progressWidth - mProgressMargin), getMeasuredHeight() - (int) mProgressMargin);
    //Draw progress
    mDrawableProgress.draw(canvas);
    if (mProgress == mMaxProgress) {
     setBackgroundDrawable(mDrawableButton);
     mFinish = true;
    }
   }
   super.onDraw(canvas);
  }
  /**
  * Set current progress
  */
  public void setProgress(int progress) {
   if (!mFinish) {
    mProgress = progress;
    setBackgroundDrawable(mDrawableProgressBackground);
    invalidate();
   }
  }
  public void setMaxProgress(int maxProgress) {
   mMaxProgress = maxProgress;
  }
  public void setMinProgress(int minProgress) {
   mMinProgress = minProgress;
  }
  public void reset() {
   mFinish = false;
   mProgress = mMinProgress;
  }
 }

 使用:

 <com.cloud.customviews.ProgressButton
    android:id="@+id/button_progress_green"
    android:layout_width="dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="dp"
    android:textAllCaps="false"
    android:textColor="@color/colorWhite"
    android:text="@string/button_progress"
    app:cornerRadius="dp"
    app:progressMargin="dp"
    app:progressColor="@color/colorGreen"
    app:buttonColor="@color/colorGreen" />

Android是什么

Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。

上述就是小編為大家分享的怎么在Android中利用ProgressButton實現進度條按鈕了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

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

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女