效果:
代码:
attr
<resources>
<declare-styleable name="MyView02">
<attr name="secondColor" format="color"></attr>
<attr name="firstColor" format="color"></attr>
<attr name="speed" format="integer"></attr>
<attr name="radius" format="dimension"></attr>
</declare-styleable>
</resources>
main_activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.zidingyiview.MainActivity" >
<com.example.zidingyiview.MyView02
android:layout_centerInParent="true"
android:layout_width="400dp"
android:layout_height="400dp"
android:id="@+id/view01"/>
</RelativeLayout>
MyView02
package com.example.zidingyiview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
public class MyView02 extends View {
Paint mPaint1;
Paint mPaint2;
private int color1;
private int color2;
int radius;
int speed;
int progress;
boolean isNext;
boolean flag = true;
// 防止类存泄漏
// 在程序退出时,调用该方法结束线程
public void closeView() {
flag = false;
}
public MyView02(Context context) {
this(context, null);
}
public MyView02(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView02(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 拿到自定义属性
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.MyView02, defStyleAttr, 0);
color1 = ta.getColor(R.styleable.MyView02_firstColor, Color.GREEN);
color2 = ta.getColor(R.styleable.MyView02_secondColor, Color.BLUE);
radius = ta.getDimensionPixelSize(R.styleable.MyView02_radius,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
100, getResources().getDisplayMetrics()));
speed = ta.getInteger(R.styleable.MyView02_speed, 10);
ta.recycle();
// 设置画笔
mPaint1 = new Paint();
mPaint1.setStyle(Style.STROKE);
mPaint1.setStrokeWidth(radius/2);
mPaint1.setAntiAlias(true);
mPaint2 = new Paint();
mPaint2.setStyle(Style.STROKE);
mPaint2.setStrokeWidth(radius/2);
mPaint2.setAntiAlias(true);
mPaint1.setColor(color1);
mPaint2.setColor(color2);
// 开启一个线程来画圈圈
new Thread(new Runnable() {
@Override
public void run() {
while (flag) {
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress += 1;
if (progress >= 360) {
progress = 0;
if (!isNext) {
isNext = true;
} else {
isNext = false;
}
}
postInvalidate();
}
}
}).start();
}
@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
int centre = getWidth() / 2;
RectF oval = new RectF(centre - radius, centre - radius, centre
+ radius, centre + radius);
canvas.drawArc(oval, 0, 360, false, mPaint2);
if (isNext) {
canvas.drawArc(oval, 0, 360, false, mPaint1);
canvas.drawArc(oval, 0, progress, false, mPaint2);
} else {
canvas.drawArc(oval, 0, progress, false, mPaint1);
}
}
}
转载于:http://blog.csdn.net/lmj623565791/article/details/24500107