public class LuckView extends View implements View.OnClickListener {
private Paint mPaint;
private String[] contents = new String[]{“A”,“B”,“C”,“D”,“E”,“F”,“G”,“H”};
//颜色时int类型的
private int[] colors = new int[]{Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700"),Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
private int mWidth;
private String mStr = “start”;
public LuckView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
setOnClickListener(this);//点击事件
}
//画图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//1.用RectF画扇形
RectF rectF = new RectF(0,0,mWidth,mWidth);
//设置样式--填充
mPaint.setStyle(Paint.Style.FILL);
//for循环,将颜色填充
for (int i = 0; i <colors.length ; i++) {
//数据
mPaint.setColor(colors[i]);
//起始角度的计算
int starts = i * 45;
//圆弧
canvas.drawArc(rectF,starts,45,true,mPaint);
}
//2.扇形中的字体
//设置字体的颜色、大小
mPaint.setTextSize(25);
mPaint.setColor(Color.BLACK);
//for循环,将字体填充
for (int i = 0; i <contents.length ; i++) {
int starts = i * 45;
Path path = new Path();
path.addArc(rectF,starts,45);
canvas.drawTextOnPath(contents[i],path,60,60,mPaint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(500,500);
mWidth = getMeasuredWidth();//得到测量后的宽度
}
@Override
public void onClick(View v) {
Random random = new Random();
RotateAnimation animation = new RotateAnimation(0f,random.nextInt(3600),mWidth/2,mWidth/2);
animation.setDuration(3000);
animation.setFillAfter(true);
startAnimation(animation);
}
}