Android的自定義的環(huán)形進(jìn)度條實(shí)現(xiàn)有多種方法。以下是其中一個(gè),可以實(shí)現(xiàn)一些復(fù)雜點(diǎn)的效果。
實(shí)現(xiàn)思路:繼承View類,并重寫onDraw方法。同時(shí)用一個(gè)類實(shí)時(shí)計(jì)算繪畫的進(jìn)度,實(shí)現(xiàn)環(huán)形進(jìn)度條的效果。
實(shí)現(xiàn)出來的效果:
1.添加了監(jiān)聽接口,監(jiān)控進(jìn)度條的繪畫是否完成,即進(jìn)度是100%。
2.可以設(shè)定進(jìn)度條播放的時(shí)間
3.可以點(diǎn)擊暫停和繼續(xù)還有停止進(jìn)行進(jìn)度條的繪畫,可以當(dāng)實(shí)時(shí)顯示音樂當(dāng)前播放進(jìn)度的按鈕。
4.更多的效果可有待繼續(xù)增加

image.png
xml文件中的屬性設(shè)置:
<!-- 環(huán)形進(jìn)度條按鈕屬性 -->
<declare-styleable name="CircleProgressBar">
<attr name="max" format="integer"/> <!-- 進(jìn)度條最大值 -->
<attr name="fill" format="boolean"/> <!-- 是否填充圓形區(qū)域 ,不填充就是環(huán)形的進(jìn)度條了-->
<attr name="Paint_Width" format="integer"/> <!-- 畫筆寬度,填充模式下無效,會(huì)被重置為0 -->
<attr name="Paint_Color" format="integer"/> <!-- 畫筆顏色 -->
<attr name="Inside_Interval" format="integer"/> <!-- 圓形區(qū)域向里縮進(jìn)的距離 -->
</declare-styleable>
默認(rèn)變量:
private static String TAG = "CircleProgressButton";
private static final int DEFAULT_MAX_VALUE = 100; // 默認(rèn)進(jìn)度條最大值
private static final int DEFAULT_PAINT_WIDTH = 10; // 默認(rèn)畫筆寬度
private static final int DEFAULT_PAINT_COLOR = 0xffffcc00; // 默認(rèn)畫筆顏色
private static final boolean DEFAULT_FILL_MODE = true; // 默認(rèn)填充模式
private static final int DEFAULT_INSIDE_VALUE = 0; // 默認(rèn)縮進(jìn)距離
private CircleAttribute mCircleAttribute; // 圓形進(jìn)度條基本屬性
private int mMaxProgress; // 進(jìn)度條最大值
private int mMainCurProgress; // 主進(jìn)度條當(dāng)前值
private CartoomEngine mCartoomEngine; // 動(dòng)畫引擎
private boolean isBCartoom = false;//是否正在作畫
private Drawable mBackgroundPicture; // 背景圖
private boolean isPause = false; // 是否暫停
private int mPlayTime; // 播放時(shí)間
private OnCompletedListener mCompLsn;
private boolean finishFlag = false;
重寫onDraw方法:
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (mBackgroundPicture == null) // 沒背景圖的話就繪制底色
{
canvas.drawArc(mCircleAttribute.mRoundOval, 0, 360,
mCircleAttribute.mBRoundPaintsFill,
mCircleAttribute.mBottomPaint);
}
float rate = (float) mMainCurProgress / mMaxProgress;
float sweep = 360 * rate;
canvas.drawArc(mCircleAttribute.mRoundOval, mCircleAttribute.mDrawPos,
sweep, mCircleAttribute.mBRoundPaintsFill,
mCircleAttribute.mMainPaints);
postInvalidate();
}
CartoomEngine類負(fù)責(zé)實(shí)時(shí)更新進(jìn)度條的當(dāng)前值,詳細(xì)代碼請下載源碼查看
源碼地址:http://download.csdn.net/detail/syun0929/7103809