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

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

上面說(shuō)了我們只需要處理中間狀態(tài)的變化就可以了,對(duì)于進(jìn)度的處理直接使用了弘洋文章中實(shí)現(xiàn):
http://blog.csdn.net/lmj623565791/article/details/43371299
下面開(kāi)始具體實(shí)現(xiàn)。
具體實(shí)現(xiàn)
自定義的實(shí)現(xiàn)還是按照官方提供的步驟來(lái),對(duì)于自定義View的步驟之前我也寫過(guò)一篇文章,感興趣的朋友可以看一下:Android自定義View的官方套路。
為了完整講解,下面還是會(huì)提到圓形進(jìn)度條的自定義,知道進(jìn)度的實(shí)現(xiàn)可以直接跳過(guò)部分步驟。
1、創(chuàng)建View
觀察要實(shí)現(xiàn)的外圈進(jìn)度條,有兩個(gè)進(jìn)度:一個(gè)用來(lái)表示默認(rèn)的圓形,另一個(gè)表示進(jìn)度的顏色。所以這里涉及到兩個(gè)進(jìn)度條顏色寬高的定義。要繪制圓肯定需要半徑了。
創(chuàng)建view有三小步
(1)、定義屬性
<declare-styleable name="ButtonCircleProgressBar"> <!--無(wú)進(jìn)度時(shí)的顏色--> <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" /> <!--無(wú)進(jìn)度時(shí)的邊框高--> <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用來(lái)表示當(dāng)前View的狀態(tài):End代碼結(jié)束,Starting正在進(jìn)行。我們用這兩個(gè)狀態(tài)來(lái)判定怎么去draw去合適的效果。提供了setStatus為Staus設(shè)置狀態(tài)。
mPath用來(lái)進(jìn)行繪制未開(kāi)始時(shí)候的三角形。
2、處理View的布局
這一步主要是onMeasure方法中測(cè)量出合適的寬高。
@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);
}
只需要處理寬高沒(méi)有精確指定的情況,通過(guò)padding加上整個(gè)圓以及Paint的寬度計(jì)算出具體的值。
接下來(lái)就是第三步,繪制效果。
3、繪制View
為了更加清晰一點(diǎn),這里先說(shuō)繪制圓的進(jìn)度,再說(shuō)圓中間的狀態(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);
通過(guò) canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);繪制默認(rèn)狀態(tài)下的圓。之后改變畫筆的顏色,根據(jù)進(jìn)度繪制圓弧。
(2)、繪制中間的狀態(tài)。
第一種是未開(kāi)始的情況,中間是一個(gè)三角形。我們使用Path來(lái)繪制三角形,主要通過(guò) moveTo(float x, float y)來(lái)設(shè)置第一個(gè)點(diǎn),然后通過(guò)lineTo(float x, float y)來(lái)連接一個(gè)三角形。再設(shè)置Paint為填充。
第一個(gè)點(diǎn)這里設(shè)置為三角形的左上角的頂點(diǎn)。
那么第一個(gè)點(diǎn)怎么算?
我們這里繪制一個(gè)等邊三角形,設(shè)置邊長(zhǎng)等于半徑。
第一個(gè)點(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)
第二個(gè)點(diǎn)這里選三角形的左下角,x坐標(biāo)不變,y值為半徑加上邊長(zhǎng)的一半:mRadius+(triangleLength/2)
第三個(gè)點(diǎn)選右邊的點(diǎn),x點(diǎn)的坐標(biāo)顯然就是第一個(gè)點(diǎn)的x坐標(biāo)加上三角形的高
即:(float) (leftX+Math.sqrt(3.0)/2*triangleLength),y點(diǎn)坐標(biāo)就是半徑mRadius。
最后再回到第一個(gè)點(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(jué)三角形設(shè)置在中間的效果不太好,所以讓他在原有基礎(chǔ)上增加0.2倍的距離。
有了mPath變量之后就可以在onDraw中繪制未開(kāi)始狀態(tài)的三角形了,看代碼
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop());
....
if (mStatus==Status.End){//未開(kāi)始狀態(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é)束為開(kāi)始y點(diǎn)的2/3倍
對(duì)與另外一條線,x點(diǎn)直徑減去mRadius*2/3,y點(diǎn)坐標(biāo)的變化和上一條線一樣。
這樣就完成了onDraw方法。
4、處理用戶交互
由于對(duì)于下載更新進(jìn)度的情況來(lái)說(shuō),該控件只做狀態(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);
}
}
});
}
}
好了,到這里一個(gè)圓形進(jìn)度條式按鈕就實(shí)現(xiàn)了.希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Webview實(shí)現(xiàn)android簡(jiǎn)單的瀏覽器實(shí)例代碼
這篇文章主要介紹了Webview實(shí)現(xiàn)android簡(jiǎn)單的瀏覽器實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android基于ViewDragHelper仿QQ5.0側(cè)滑界面效果
這篇文章主要介紹了Android基于ViewDragHelper仿QQ5.0側(cè)滑界面效果,具有一定的,感興趣的小伙伴們可以參考一下2016-07-07
史上最全Android build.gradle配置詳解(小結(jié))
這篇文章主要介紹了史上最全Android build.gradle配置詳解(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
Android 獲取系統(tǒng)語(yǔ)言的實(shí)例(兼容7.0)
下面小編就為大家?guī)?lái)一篇Android 獲取系統(tǒng)語(yǔ)言的實(shí)例(兼容7.0)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Android入門之使用SQLite內(nèi)嵌式數(shù)據(jù)庫(kù)詳解
Android內(nèi)帶SQLite內(nèi)嵌式數(shù)據(jù)庫(kù)了。這對(duì)于我們存儲(chǔ)一些更復(fù)雜的結(jié)構(gòu)化數(shù)據(jù)帶來(lái)了極大的便利。本文就來(lái)和大家聊聊具體的使用方法,希望對(duì)大家有所幫助2022-12-12
Android無(wú)需root實(shí)現(xiàn)apk的靜默安裝
這篇文章主要介紹了Android無(wú)需root實(shí)現(xiàn)apk的靜默安裝 的相關(guān)資料,需要的朋友可以參考下2016-01-01
android中Fragment+RadioButton實(shí)現(xiàn)底部導(dǎo)航欄
本篇文章主要介紹了android中Fragment+RadioButton實(shí)現(xiàn)底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

