在HeyNine的博客基础上进行了完善。
代码:
首先在values下的attrs.xml下建立属性节点:
<declare-styleable name="TasksCompletedView">
<attr name="radius" format="dimension"/>
<attr name="strokeWidth" format="dimension"/>
<attr name="circleColor" format="color"/>
<attr name="ringColor" format="color"/>
<attr name="ringColor2" format="color"/>
</declare-styleable>
定义好控件的属性,尺寸,圆环颜色等。
然后在layout中调用这个控件,并且已经能设置定义好的属性:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tc="http://schemas.android.com/apk/res/com.snailws.taskscompleted"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
tools:context=".MainActivity" >
<include layout="@layout/header_common" />
<com.snailws.taskscompleted.activity.TasksCompletedView
android:id="@+id/tasks_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tc:radius="100dip"
tc:strokeWidth="20dip"
tc:circleColor="@color/circle_color"
tc:ringColor="@color/ring_color" />
</LinearLayout>
再定义这个控件的java文件,定义好颜色和画笔,载入属性文件,并且获取属性,并设置了默认值,
紧接着初始化画笔。
Android屏幕坐标系:
package com.snailws.taskscompleted.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import com.snailws.taskscompleted.R;
/**
*
* @author naiyu(http://snailws.com)
* @version 1.0
*/
public class MainActivity extends Activity {
private TasksCompletedView mTasksView;
private int mTotalProgress;
private int mCurrentProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initVariable();
initView();
new Thread(new ProgressRunable()).start();
}
private void initVariable() {
mTotalProgress = 100;
mCurrentProgress = 0;
}
private void initView() {
mTasksView = (TasksCompletedView) findViewById(R.id.tasks_view);
}
class ProgressRunable implements Runnable {
@Override
public void run() {
while (mCurrentProgress < mTotalProgress) {
mCurrentProgress += 1;
mTasksView.setProgress(mCurrentProgress);
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
显示的效果图如下:
源码下载:http://download.csdn.net/detail/nainai007/6554501