欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 進度條按鈕ProgressButton的實現(xiàn)代碼

 更新時間:2018年10月28日 09:04:18   作者:浮云Cloud  
這篇文章主要介紹了Android 進度條按鈕實現(xiàn)(ProgressButton)代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下

有些App在點擊下載按鈕的時候,可以在按鈕上顯示進度,我們可以通過繼承原生Button,重寫onDraw來實現(xiàn)帶進度條的按鈕。

Github:https://github.com/imcloudfloating/ProgressBar

1.效果:

2.原理:

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

3.自定義參數(shù):

在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的值,然后調(diào)用invalidate()重繪,因為我這里定義了一個minProgress(默認為0),所以在計算進度條寬度的時候,當前進度和最大進度都要先減去minProgress再做除法。

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

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

總結(jié)

以上所述是小編給大家介紹的Android 進度條按鈕ProgressButton的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言小編會及時回復(fù)大家的!

相關(guān)文章

  • Android實現(xiàn)類似qq微信消息懸浮窗通知功能

    Android實現(xiàn)類似qq微信消息懸浮窗通知功能

    這篇文章主要介紹了Android實現(xiàn)類似qq微信消息懸浮窗通知,需要的朋友可以參考下
    2018-02-02
  • 如何更改Dialog的標題與按鈕顏色詳解

    如何更改Dialog的標題與按鈕顏色詳解

    這篇文章主要給大家介紹了關(guān)于如何更改Dialog的標題與按鈕顏色的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考借鑒,下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Android基于OpenCV實現(xiàn)圖像脫色

    Android基于OpenCV實現(xiàn)圖像脫色

    脫色是將彩色圖像轉(zhuǎn)換為灰度圖像的過程。同時,它也是數(shù)字打印,風(fēng)格化的黑白照片渲染以及許多單通道圖像處理應(yīng)用程序中的基本工具。本文講述基于OpenCV實現(xiàn)圖像脫色的步驟
    2021-06-06
  • Android應(yīng)用中使用SharedPreferences類存儲數(shù)據(jù)的方法

    Android應(yīng)用中使用SharedPreferences類存儲數(shù)據(jù)的方法

    這篇文章主要介紹了Android應(yīng)用中使用SharedPreferences類存儲數(shù)據(jù)的方法,SharedPreferences使用鍵值對應(yīng)的方式進行存儲,使用于少量的數(shù)據(jù)保存,需要的朋友可以參考下
    2016-04-04
  • Android開發(fā)性能優(yōu)化總結(jié)

    Android開發(fā)性能優(yōu)化總結(jié)

    這篇文章主要介紹了Android開發(fā)性能優(yōu)化總結(jié)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Android中自定義ContentProvider實例

    Android中自定義ContentProvider實例

    應(yīng)用A(TestBaidu)調(diào)用另外一個應(yīng)用(TestContentProvider)中的自定義ContentProvider,具體實現(xiàn)如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android 圖片存儲到指定路徑和相冊的方法

    Android 圖片存儲到指定路徑和相冊的方法

    本篇文章主要介紹了Android 圖片存儲到指定路徑和相冊的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android自定義雙向滑動控件

    Android自定義雙向滑動控件

    這篇文章主要為大家詳細介紹了Android自定義雙向滑動控件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • android仿即刻點贊文字部分的自定義View的示例代碼

    android仿即刻點贊文字部分的自定義View的示例代碼

    本篇文章主要介紹了android仿即刻點贊文字部分的自定義View的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android使用ListView批量刪除item的方法

    Android使用ListView批量刪除item的方法

    這篇文章主要介紹了Android使用ListView批量刪除item的方法,實例分析了Android中ListView控件的相關(guān)操作技巧,需要的朋友可以參考下
    2016-07-07

最新評論