自定义实现圆形播放进度条(Android,飞一般的感觉)。
废话不多说,先上效果图:
Android提供了很多基本的控件实现,但不是一个完整、强大的实现。
幸运的是,Android提供了自定义控件的实现,有了自定义控件,我们就可以再Android的基础控件上实现我们想要的功能或者自定义的外观。
以ProgressBar为例,对于可调节的进度条似乎只有长条形的ProgressBar(圆形的都是不停转动的那种)
假如我们想要一个可调节进度的圆形进度条呢。。。
Ok,下面我们直接切入主题(关于自定义控件的相关实现细节就不多说了,还不太清楚的童鞋先找相关资料补习一下)
该自定义控件的实现思路是继承自View,然后重写onDraw
先看看该类有哪些成员变量:
- public class CircleProgress extends View{
-
-
- 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 int mSubCurProgress;
-
- private CartoomEngine mCartoomEngine;
-
- private Drawable mBackgroundPicture;
- class CircleAttribute
- {
- public RectF mRoundOval;
- public boolean mBRoundPaintsFill;
- public int mSidePaintInterval;
- public int mPaintWidth;
- public int mPaintColor;
- public int mDrawPos;
-
- public Paint mMainPaints;
- public Paint mSubPaint;
-
- public Paint mBottomPaint;
- class CartoomEngine
- {
- public Handler mHandler;
- public boolean mBCartoom;
- public Timer mTimer;
- public MyTimerTask mTimerTask;
- public int mSaveMax;
- public int mTimerInterval;
- public float mCurFloatProcess;
下面看看onDraw的代码片段:
- public void onDraw(Canvas canvas) {
-
- super.onDraw(canvas);
-
- if (mBackgroundPicture == null)
- {
- canvas.drawArc(mCircleAttribute.mRoundOval, 0, 360, mCircleAttribute.mBRoundPaintsFill, mCircleAttribute.mBottomPaint);
- }
-
-
- float subRate = (float)mSubCurProgress / mMaxProgress;
- float subSweep = 360 * subRate;
- canvas.drawArc(mCircleAttribute.mRoundOval, mCircleAttribute.mDrawPos, subSweep, mCircleAttribute.mBRoundPaintsFill, mCircleAttribute.mSubPaint);
-
- float rate = (float)mMainCurProgress / mMaxProgress;
- float sweep = 360 * rate;
- canvas.drawArc(mCircleAttribute.mRoundOval, mCircleAttribute.mDrawPos, sweep, mCircleAttribute.mBRoundPaintsFill, mCircleAttribute.mMainPaints);
-
-
- }