下面是效果图,为了方便大家直接修改,我会在最下面贴出代码的下载地址,节省大家使用的时间。
进度环效果如上,我们分析上面这个组件,不同地方遇到时需要设置的。
大多数情况下:
初始化时候设置的:进度条宽、高、字体大小、已完成颜色、未完成颜色、环形的宽度
需要动态获取数据时候设置的:进度条的进度,这个值常常是获取后台的一个值或者是用线程控制变化的。
对于需要动态变的:我们写一个方法,直接传进来
public void setProgress(int progress) {
this.mProgress = progress;
this.invalidate();
}
对于初始化时候设置的,我们可以用自定义属性来做。首先在attr.xml中添加
<declare-styleable name="circle_view">
<attr name="circle_width" format="dimension" />
<attr name="text_size" format="dimension"/>
<attr name="undone_color" format="color"/>
<attr name="done_color" format="color"/>
</declare-styleable>
在使用时:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:xuan="http://schemas.android.com/apk/res/com.example.testproject"
android:layout_width="match_parent"
android:layout_height="match_parent"
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=".MainActivity" >
<com.example.testproject.CircleProgressView
android:id="@+id/circleProgressbar"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true"
xuan:circle_width="20dip"
xuan:done_color="#0199CA"
xuan:text_size="23sp"
xuan:undone_color="#E9E9E9" />
</RelativeLayout>
注意两部分:
xmlns:xuan="http://schemas.android.com/apk/res/com.example.testproject"
和
xuan:circle_width="20dip"
xuan:done_color="#0199CA"
xuan:text_size="23sp"
xuan:undone_color="#E9E9E9"
上面的自定义属性空间xuan对应下面的自定义属性的开头,自定义控件末尾是我建的项目的包名。
然后在自定义组件中解析定义到的属性
public CircleProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.circle_view);
mCircleLineStrokeWidth = a.getDimensionPixelSize(
R.styleable.circle_view_circle_width, CIRCLE_WIDTH);
mDoneColor = a.getColor(R.styleable.circle_view_done_color, DONE_COLOR);
mUndoneColor = a.getColor(R.styleable.circle_view_undone_color,
UNDONE_COLOR);
mTextSize = a.getDimensionPixelSize(R.styleable.circle_view_text_size,
TEXT_SIZE);
mContext = context;
mRectF = new RectF();
mPaint = new Paint();
a.recycle();
}
获取一个属性的方式如下:
mCircleLineStrokeWidth = a.getDimensionPixelSize(
R.styleable.circle_view_circle_width, CIRCLE_WIDTH);
这个需要传入一个默认值,当xml中没有使用这个属性时,就用默认值代替。
代码下载地址:http://download.csdn.net/download/u010047390/9665965