canvas的drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint) 方法是关键
相关参数说明大家详看SDK文档
控件大小决定oval
画笔属性设置paint
useCenter表示是否填充
startAngle是绘制圆弧的起点,我们使用-90度,即12点钟方向
sweepAngle是从起点顺时针绘制覆盖的角度,意味着进度值为30%的话,就是 360 * 30%
设置进度的代码片段:
- public synchronized void setMainProgress (int progress)
- {
- mMainCurProgress = progress;
- if (mMainCurProgress < 0)
- {
- mMainCurProgress = 0;
- }
- if (mMainCurProgress > mMaxProgress)
- {
- mMainCurProgress = mMaxProgress;
- }
- invalidate();
- }
设置进度值之后触发重绘,计算sweepAngle的值,最后完成绘制效果,怎么样,是不是就对上了
该控件的自定义属性如下:
- <?xml version="1.0" encoding="utf-8"?>
- lt;resources>
- <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>
- lt;/resources>
再贴上本例的布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- xmlns:roundProgress="http://schemas.android.com/apk/res/com.genius.progress"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="#ffffff">
- <LinearLayout
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:id="@+id/linearLayout1"
- android:orientation="horizontal"
- android:gravity = "center_horizontal">
- <Button
- android:text="增加主进度条"
- android:id="@+id/buttonAddMainPro"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1">
- </Button>
- <Button
- android:text="增加子进度条"
- android:id="@+id/buttonAddSubPro"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1">
- </Button>
- <ImageButton
- android:id="@+id/buttonImage"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/background2"
- android:layout_weight="1" />
- </LinearLayout>
- <LinearLayout
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:id="@+id/linearLayout2"
- android:orientation="horizontal"
- android:background="#ff0000"
- android:gravity = "center_horizontal">
- <com.genius.circle.CircleProgress
- android:id="@+id/roundBar1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/background1"
- roundProgress:max="100"
- roundProgress:fill = "false"
- roundProgress:Inside_Interval="5"
- roundProgress:Paint_Width = "4"
- roundProgress:Paint_Color = "0xff0000ff"
- />
- <com.genius.circle.CircleProgress
- android:id="@+id/roundBar2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/background3"
- roundProgress:max="100"
- roundProgress:Inside_Interval="8"
- roundProgress:fill = "true"
- roundProgress:Paint_Width = "4"
- roundProgress:Paint_Color = "0xffaa5500"
- />
- <com.genius.circle.CircleProgress
- android:id="@+id/roundBar3"
- android:layout_width="96dp"
- android:layout_height="96dp"
- roundProgress:max="100"
- roundProgress:fill="false"
- roundProgress:Paint_Width="40"
- roundProgress:Inside_Interval="20"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:id="@+id/linearLayout3"
- android:orientation="horizontal"
- android:gravity = "center_horizontal|center_vertical"
- android:background="#00ff00">
- <Button
- android:text="启动动画"
- android:id="@+id/buttonStart"
- android:layout_width="100dip"
- android:layout_height="wrap_content">
- </Button>
- <com.genius.circle.CircleProgress
- android:id="@+id/roundBar4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/background1"
- roundProgress:Inside_Interval="6"
- roundProgress:Paint_Color = "0xff0000ff"
- />
- <Button
- android:text="停止动画"
- android:id="@+id/buttonStop"
- android:layout_width="100dip"
- android:layout_height="wrap_content">
- </Button>
- </LinearLayout>
- </LinearLayout>
