原文链接:http://blog.csdn.net/lmj623565791/article/details/24500107
自定义view的步骤:
1.res/values/下新建一个attrs.xml,创建自定义的属性
属性类型可以取:string,color,demension,integer,enum,reference,float,boolean,fraction,flag
2.在布局文件中添加自定义的view,为自定义的属性赋值
自定义的属性属于不同的命名空间
3.为自定义的view重写方法,获取自定义的属性
1.attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="firstColor" format="color" />
<attr name="secondColor" format="color" />
<attr name="circleWidth" format="dimension" />
<attr name="speed" format="integer" />
<declare-styleable name="CustomProgressBar">
<attr name="firstColor" />
<attr name="secondColor" />
<attr name="circleWidth" />
<attr name="speed" />
</declare-styleable>
</resources>
2.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.testview3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.testview3.CustomProgressBar
android:id="@+id/customProgressBar"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
custom:circleWidth="15dp"
custom:firstColor="#887E77"
custom:secondColor="#338FCC"
custom:speed="20"/>
</LinearLayout>
3.
package com.example.testview3;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
//颜色代码表:http://www.114la.com/other/rgb.htm
public class CustomProgressBar extends View {
private int firstColor;
private int secondColor;
private int circleWidth;
private int speed;
private int progress;
private Paint paint;
public CustomProgressBar(Context context) {
super(context);
}
//默认的布局文件调用的是两个参数的构造方法
public CustomProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomProgressBar(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
TypedArray typedArray = context.getTheme().
obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyle, 0);
firstColor = typedArray.getColor(R.styleable.CustomProgressBar_firstColor, Color.GREEN);
secondColor = typedArray.getColor(R.styleable.CustomProgressBar_secondColor, Color.BLACK);
circleWidth = (int) typedArray.getDimension(R.styleable.CustomProgressBar_circleWidth, 80);
speed = typedArray.getInt(R.styleable.CustomProgressBar_speed, 0);
paint = new Paint();
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
int centre = getWidth() / 2;
int radius = centre - circleWidth;
paint.setStrokeWidth(circleWidth); // 设置圆环的宽度
paint.setAntiAlias(true); // 消除锯齿
paint.setStyle(Paint.Style.STROKE); // 设置空心
RectF oval = new RectF(centre - radius, centre - radius,
centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
paint.setColor(firstColor); // 设置圆环的颜色
canvas.drawCircle(centre, centre, radius, paint); // 画出圆环
paint.setColor(secondColor); // 设置圆环的颜色
canvas.drawArc(oval, -90, progress, false, paint); // 根据进度画圆弧
}
public void start(final int targetProgress)
{
new Thread()
{
public void run()
{
progress = 0;
//targetProgress在0-100
//对应弧度在0-360
while (progress < targetProgress * 3.6)
{
progress++;
postInvalidate();
try
{
Thread.sleep(speed);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
}.start();
}
}
4.
package com.example.testview3;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private CustomProgressBar customProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customProgressBar = (CustomProgressBar) findViewById(R.id.customProgressBar);
customProgressBar.start(90);
}
}
效果图: