import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint = new Paint();
mPaint = new Paint();
}
/**
* 画笔对象的引用
*/
private Paint paint,mPaint,paint2;
int i = 0;
/**
* 圆环的宽度
*/
private float roundWidth = 10;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 画最外层的大圆环
*/
int centre = getWidth() / 2; // 获取圆心的x坐标
int radius = (int) (centre - roundWidth / 2); // 圆环的半径
paint.setColor(Color.CYAN);
paint.setStyle(Paint.Style.FILL); // 设置空心
paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
paint.setAntiAlias(true); // 消除锯齿
canvas.drawCircle(centre, centre, radius, paint); // 画出圆环
i += 1;
/**
* 画圆弧 ,画圆环的进度
*/
// 设置进度是实心还是空心
paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
RectF oval = new RectF(centre - radius, centre - radius, centre
+ radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
int degree = 360 * i / 360;
if (degree%360 == 0) {
int x = (int) (Math.random() * 16777777);
String hex = Integer.toHexString(x);
if (hex.length() == 6) {
mPaint.setColor(Color.parseColor("#" + hex));
} else {
mPaint.setColor(Color.RED);
}
i=0;
}
canvas.drawArc(oval, 0, degree, true, mPaint);
invalidate();
}
}
布局如下:
<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"
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.day30_shanview.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Vimport android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint = new Paint();
mPaint = new Paint();
}
/**
* 画笔对象的引用
*/
private Paint paint,mPaint,paint2;
int i = 0;
/**
* 圆环的宽度
*/
private float roundWidth = 10;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 画最外层的大圆环
*/
int centre = getWidth() / 2; // 获取圆心的x坐标
int radius = (int) (centre - roundWidth / 2); // 圆环的半径
paint.setColor(Color.CYAN);
paint.setStyle(Paint.Style.FILL); // 设置空心
paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
paint.setAntiAlias(true); // 消除锯齿
canvas.drawCircle(centre, centre, radius, paint); // 画出圆环
i += 1;
/**
* 画圆弧 ,画圆环的进度
*/
// 设置进度是实心还是空心
paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
RectF oval = new RectF(centre - radius, centre - radius, centre
+ radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
int degree = 360 * i / 360;
if (degree%360 == 0) {
int x = (int) (Math.random() * 16777777);
String hex = Integer.toHexString(x);
if (hex.length() == 6) {
mPaint.setColor(Color.parseColor("#" + hex));
} else {
mPaint.setColor(Color.RED);
}
i=0;
}
canvas.drawArc(oval, 0, degree, true, mPaint);
invalidate();
}
}
布局如下:
<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"
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.day30_shanview.MyView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>