Android的自定義的環形進度條實現有多種方法。以下是其中一個,可以實現一些復雜點的效果。
實現思路:繼承View類,并重寫onDraw方法。同時用一個類實時計算繪畫的進度,實現環形進度條的效果。
實現出來的效果:
1.添加了監聽接口,監控進度條的繪畫是否完成,即進度是100%。
2.可以設定進度條播放的時間
3.可以點擊暫停和繼續還有停止進行進度條的繪畫,可以當實時顯示音樂當前播放進度的按鈕。
4.更多的效果可有待繼續增加
image.png
xml文件中的屬性設置:
<!-- 環形進度條按鈕屬性 -->
<declare-styleable name="CircleProgressBar">
<attr name="max" format="integer"/> <!-- 進度條最大值 -->
<attr name="fill" format="boolean"/> <!-- 是否填充圓形區域 ,不填充就是環形的進度條了-->
<attr name="Paint_Width" format="integer"/> <!-- 畫筆寬度,填充模式下無效,會被重置為0 -->
<attr name="Paint_Color" format="integer"/> <!-- 畫筆顏色 -->
<attr name="Inside_Interval" format="integer"/> <!-- 圓形區域向里縮進的距離 -->
</declare-styleable>
默認變量:
private static String TAG = "CircleProgressButton";
private static final int DEFAULT_MAX_VALUE = 100; // 默認進度條最大值
private static final int DEFAULT_PAINT_WIDTH = 10; // 默認畫筆寬度
private static final int DEFAULT_PAINT_COLOR = 0xffffcc00; // 默認畫筆顏色
private static final boolean DEFAULT_FILL_MODE = true; // 默認填充模式
private static final int DEFAULT_INSIDE_VALUE = 0; // 默認縮進距離
private CircleAttribute mCircleAttribute; // 圓形進度條基本屬性
private int mMaxProgress; // 進度條最大值
private int mMainCurProgress; // 主進度條當前值
private CartoomEngine mCartoomEngine; // 動畫引擎
private boolean isBCartoom = false;//是否正在作畫
private Drawable mBackgroundPicture; // 背景圖
private boolean isPause = false; // 是否暫停
private int mPlayTime; // 播放時間
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類負責實時更新進度條的當前值,詳細代碼請下載源碼查看
源碼地址:http://download.csdn.net/detail/syun0929/7103809