Android自定義View之圓形進(jìn)度條式按鈕
介紹
今天上班的時候有個哥們問我怎么去實(shí)現(xiàn)一個按鈕式的進(jìn)度條,先來看看他需要實(shí)現(xiàn)的效果圖。

和普通的圓形進(jìn)度條類似,只是中間的地方有兩個狀態(tài)表示,未開始,暫停狀態(tài)。而且他說圓形進(jìn)度的功能已經(jīng)實(shí)現(xiàn)了。那么我們只需要對中間的兩個狀態(tài)做處理就行了。
先來看看實(shí)現(xiàn)的效果圖:

上面說了我們只需要處理中間狀態(tài)的變化就可以了,對于進(jìn)度的處理直接使用了弘洋文章中實(shí)現(xiàn):
http://blog.csdn.net/lmj623565791/article/details/43371299
下面開始具體實(shí)現(xiàn)。
具體實(shí)現(xiàn)
自定義的實(shí)現(xiàn)還是按照官方提供的步驟來,對于自定義View的步驟之前我也寫過一篇文章,感興趣的朋友可以看一下:Android自定義View的官方套路。
為了完整講解,下面還是會提到圓形進(jìn)度條的自定義,知道進(jìn)度的實(shí)現(xiàn)可以直接跳過部分步驟。
1、創(chuàng)建View
觀察要實(shí)現(xiàn)的外圈進(jìn)度條,有兩個進(jìn)度:一個用來表示默認(rèn)的圓形,另一個表示進(jìn)度的顏色。所以這里涉及到兩個進(jìn)度條顏色寬高的定義。要繪制圓肯定需要半徑了。
創(chuàng)建view有三小步
(1)、定義屬性
<declare-styleable name="ButtonCircleProgressBar"> <!--無進(jìn)度時的顏色--> <attr name="progress_unreached_color" format="color" /> <!--進(jìn)度顏色--> <attr name="progress_reached_color" format="color" /> <!--進(jìn)度條的高--> <attr name="progress_reached_bar_height" format="dimension" /> <!--無進(jìn)度時的邊框高--> <attr name="progress_unreached_bar_height" format="dimension" /> <!--圓的半徑--> <attr name="radius" format="dimension" /> </declare-styleable>
(1)、定義屬性變量以及構(gòu)造方法中獲取屬性
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;
/**
* The status of this view currently;
*/
private Status mStatus = Status.End;
/**
* painter of all drawing things
*/
protected Paint mPaint = new Paint();
/**
* height of reached progress bar
*/
protected int mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);
/**
* color of reached bar
*/
protected int mReachedBarColor = DEFAULT_TEXT_COLOR;
/**
* color of unreached bar
*/
protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;
/**
* height of unreached progress bar
*/
protected int mUnReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);
/**
* the length of triangle
*/
private int triangleLength;
/**
* use path to draw triangle
*/
private Path mPath;
/**
* mRadius of view
*/
private int mRadius = dp2px(30);
public ButtonCircleProgressBar(Context context) {
this(context,null);
}
public ButtonCircleProgressBar(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public ButtonCircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// init values from custom attributes
final TypedArray attributes = getContext().obtainStyledAttributes(
attrs, R.styleable.ButtonCircleProgressBar);
mReachedBarColor = attributes
.getColor(
R.styleable.ButtonCircleProgressBar_progress_reached_color,
Color.BLUE);
mUnReachedBarColor = attributes
.getColor(
R.styleable.ButtonCircleProgressBar_progress_unreached_color,
DEFAULT_COLOR_UNREACHED_COLOR);
mReachedProgressBarHeight = (int) attributes
.getDimension(
R.styleable.ButtonCircleProgressBar_progress_reached_bar_height,
mReachedProgressBarHeight);
mUnReachedProgressBarHeight = (int) attributes
.getDimension(
R.styleable.ButtonCircleProgressBar_progress_unreached_bar_height,
mUnReachedProgressBarHeight);
mRadius = (int) attributes.getDimension(
R.styleable.ButtonCircleProgressBar_radius, mRadius);
triangleLength = mRadius;
attributes.recycle();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPath = new Path();//need path to draw triangle
}
public Status getStatus() {
return mStatus;
}
public void setStatus(Status status) {
mStatus = status;
invalidate();
}
public enum Status{
End,
Starting
}
獲取基礎(chǔ)的一些屬性,這里mStatus用來表示當(dāng)前View的狀態(tài):End代碼結(jié)束,Starting正在進(jìn)行。我們用這兩個狀態(tài)來判定怎么去draw去合適的效果。提供了setStatus為Staus設(shè)置狀態(tài)。
mPath用來進(jìn)行繪制未開始時候的三角形。
2、處理View的布局
這一步主要是onMeasure方法中測量出合適的寬高。
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int paintWidth = Math.max(mReachedProgressBarHeight,
mUnReachedProgressBarHeight);
if (heightMode != MeasureSpec.EXACTLY) {
int exceptHeight = (int) (getPaddingTop() + getPaddingBottom()
+ mRadius * 2 + paintWidth);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(exceptHeight,
MeasureSpec.EXACTLY);
}
if (widthMode != MeasureSpec.EXACTLY) {
int exceptWidth = (int) (getPaddingLeft() + getPaddingRight()
+ mRadius * 2 + paintWidth);
widthMeasureSpec = MeasureSpec.makeMeasureSpec(exceptWidth,
MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
只需要處理寬高沒有精確指定的情況,通過padding加上整個圓以及Paint的寬度計算出具體的值。
接下來就是第三步,繪制效果。
3、繪制View
為了更加清晰一點(diǎn),這里先說繪制圓的進(jìn)度,再說圓中間的狀態(tài)。
(1)、繪制圓
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop());
mPaint.setStyle(Paint.Style.STROKE);
// draw unreaded bar
mPaint.setColor(mUnReachedBarColor);
mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
// draw reached bar
mPaint.setColor(mReachedBarColor);
mPaint.setStrokeWidth(mReachedProgressBarHeight);
float sweepAngle = getProgress() * 1.0f / getMax() * 360;
canvas.drawArc(new RectF(0, 0, mRadius * 2, mRadius * 2), 0,
sweepAngle, false, mPaint);
通過 canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);繪制默認(rèn)狀態(tài)下的圓。之后改變畫筆的顏色,根據(jù)進(jìn)度繪制圓弧。
(2)、繪制中間的狀態(tài)。
第一種是未開始的情況,中間是一個三角形。我們使用Path來繪制三角形,主要通過 moveTo(float x, float y)來設(shè)置第一個點(diǎn),然后通過lineTo(float x, float y)來連接一個三角形。再設(shè)置Paint為填充。
第一個點(diǎn)這里設(shè)置為三角形的左上角的頂點(diǎn)。
那么第一個點(diǎn)怎么算?
我們這里繪制一個等邊三角形,設(shè)置邊長等于半徑。
第一個點(diǎn)的x坐標(biāo)就應(yīng)該是圓的直徑減去三角形的高之后除以2,即:
float leftX = (float) ((2*mRadius-Math.sqrt(3.0)/2*triangleLength)/2);
y坐標(biāo)就為:mRadius-(triangleLength/2)
第二個點(diǎn)這里選三角形的左下角,x坐標(biāo)不變,y值為半徑加上邊長的一半:mRadius+(triangleLength/2)
第三個點(diǎn)選右邊的點(diǎn),x點(diǎn)的坐標(biāo)顯然就是第一個點(diǎn)的x坐標(biāo)加上三角形的高
即:(float) (leftX+Math.sqrt(3.0)/2*triangleLength),y點(diǎn)坐標(biāo)就是半徑mRadius。
最后再回到第一個點(diǎn)就連接成三角形了。
mPath設(shè)置的完整代碼如下
public class ButtonCircleProgressBar extends ProgressBar {
.........
mPath = new Path();//need path to draw triangle
triangleLength = mRadius;
float leftX = (float) ((2*mRadius-Math.sqrt(3.0)/2*triangleLength)/2);
float realX = (float) (leftX+leftX*0.2);
mPath.moveTo(realX,mRadius-(triangleLength/2));
mPath.lineTo(realX,mRadius+(triangleLength/2));
mPath.lineTo((float) (realX+Math.sqrt(3.0)/2*triangleLength),mRadius);
mPath.lineTo(realX,mRadius-(triangleLength/2));
}
這里用了realX設(shè)置成了leftX的兩倍,是因?yàn)槲腋杏X三角形設(shè)置在中間的效果不太好,所以讓他在原有基礎(chǔ)上增加0.2倍的距離。
有了mPath變量之后就可以在onDraw中繪制未開始狀態(tài)的三角形了,看代碼
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop());
....
if (mStatus==Status.End){//未開始狀態(tài),畫筆填充
mPaint.setStyle(Paint.Style.FILL);
canvas.drawPath(mPath,mPaint);//直接drawPath
}else{
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(dp2px(5));
canvas.drawLine(mRadius*2/3,mRadius*2/3,mRadius*2/3,2*mRadius*2/3,mPaint);
canvas.drawLine(2*mRadius-(mRadius*2/3),mRadius*2/3,2*mRadius-(mRadius*2/3),2*mRadius*2/3,mPaint);
}
canvas.restore();
}
進(jìn)行中的狀態(tài)就是畫兩條線,第一條線x直接設(shè)為半徑的2/3倍,起始y點(diǎn)為2/3倍,結(jié)束為開始y點(diǎn)的2/3倍
對與另外一條線,x點(diǎn)直徑減去mRadius*2/3,y點(diǎn)坐標(biāo)的變化和上一條線一樣。
這樣就完成了onDraw方法。
4、處理用戶交互
由于對于下載更新進(jìn)度的情況來說,該控件只做狀態(tài)顯示,所以這一步不需要了,要使用的話自己設(shè)置點(diǎn)擊事件就可以了。
使用
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.qiangyu.test.buttoncircleprogress.MainActivity"> <com.qiangyu.test.buttoncircleprogress.view.ButtonCircleProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dip" android:padding="5dp" android:progress="30" /> </RelativeLayout>
Activity里設(shè)置點(diǎn)擊事件修改狀態(tài),具體根據(jù)自己邏輯處理。
public class MainActivity extends AppCompatActivity {
private ButtonCircleProgressBar mProgressBar;
private static final int MSG_PROGRESS_UPDATE = 0x110;
private int progress;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
progress = mProgressBar.getProgress();
mProgressBar.setProgress(++progress);
if (progress >= 100) {
mHandler.removeMessages(MSG_PROGRESS_UPDATE);
progress = 0;
mProgressBar.setStatus(ButtonCircleProgressBar.Status.End);
mProgressBar.setProgress(0);
}else{
mHandler.sendEmptyMessageDelayed(MSG_PROGRESS_UPDATE, 100);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = (ButtonCircleProgressBar) findViewById(R.id.progressBar);
mProgressBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mProgressBar.getStatus()== ButtonCircleProgressBar.Status.Starting){
mProgressBar.setStatus(ButtonCircleProgressBar.Status.End);
mHandler.removeMessages(MSG_PROGRESS_UPDATE);
}else{
mHandler.sendEmptyMessage(MSG_PROGRESS_UPDATE);
mProgressBar.setStatus(ButtonCircleProgressBar.Status.Starting);
}
}
});
}
}
好了,到這里一個圓形進(jìn)度條式按鈕就實(shí)現(xiàn)了.希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android開發(fā)筆記之:返回鍵的復(fù)寫onBackPressed()介紹
本篇文章是對Android中返回鍵的復(fù)寫onBackPressed()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android Studio發(fā)布項(xiàng)目到Jcenter倉庫步驟(圖文)
這篇文章主要介紹了Android Studio發(fā)布項(xiàng)目到Jcenter倉庫步驟(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
Android使用animator實(shí)現(xiàn)fragment的3D翻轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Android使用animator實(shí)現(xiàn)fragment的3D翻轉(zhuǎn)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android Room數(shù)據(jù)庫容易遇到的問題以及解決方法
這篇文章給大家介紹了我們在Android Room數(shù)據(jù)庫容易遇到的坑以及解決方法,文中有詳細(xì)的代碼示例供我們參考,具有一定的參考價值,需要的朋友可以參考下2023-09-09
Android利用反射機(jī)制調(diào)用截屏方法和獲取屏幕寬高的方法
這篇文章主要介紹了Android利用反射機(jī)制調(diào)用截屏方法和獲取屏幕寬高的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Android studio配置國內(nèi)鏡像源的實(shí)現(xiàn)
這篇文章主要介紹了Android studio配置國內(nèi)鏡像源的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Android入門之BroadCast模擬實(shí)現(xiàn)異地登錄事件發(fā)生后的主動退出
隨著對BroadCast的越來越深入,我們今天要實(shí)現(xiàn)一個稍微復(fù)雜一點(diǎn)的BroadCast。即只允許一個設(shè)備登錄一個帳號時,APP會彈一個對話框如:您的賬號在別處登錄,請重新登陸!感興趣的可以了解一下2022-12-12
Android 實(shí)現(xiàn)滑動方法總結(jié)
這篇文章主要介紹了Android 實(shí)現(xiàn)滑動方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-07-07

