首先自已创建个类继承view
package com.example.zhuzhuangtu.weight;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.RotateAnimation;
import java.util.Random;
public class MyView extends View implements View.OnClickListener {
private Paint mPaint;
private int mWidth;
//private int mPadding;
private RectF mRectF;
private String mStr = "开始";
private float kaishijiaodu;
//1转盘首先想好几个扇形并创建数组
private String[] contents = new String[]{"美 女", "女 神", "热 舞", "丰 满", "性 感", "知 性"};
//2创建颜色数组
public int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//实例化画笔对象
mPaint = new Paint();
//设置点击开的旋转的监听
setOnClickListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制 6个椭圆
initArc(canvas);
//给小圆填充颜色
mPaint.setColor(Color.RED);
//让圆边更加圆滑
mPaint.setStyle(Paint.Style.FILL);
//绘画小圆
canvas.drawCircle(mWidth / 2, mWidth / 2, 50, mPaint);
//设置小圆里的字体颜色
mPaint.setColor(Color.WHITE);
//设置小圆里的字是否居中
Rect rect = new Rect();
mPaint.getTextBounds(mStr, 0, mStr.length(), rect);
int width = rect.width();
int height = rect.height();
canvas.drawText(mStr, mWidth / 2 - width / 2, mWidth / 2 + height / 2, mPaint);
}
private void initArc(Canvas canvas) {
for (int i = 0; i < 6; i++) {
//依次根据i来设置6个扇形的颜色
mPaint.setColor(colors[i]);
//绘制圆弧,含圆心
canvas.drawArc(mRectF, 60*i, 60, true, mPaint);
}
for (int i = 0; i < 6; i++) {
//设置6个扇形的字体颜色
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(24);
Path path = new Path();
//创建path把小圆上的字
path.addArc(mRectF, 60*i, 60);
//都保存添加到圆上
canvas.drawTextOnPath(contents[i], path, 60, 60, mPaint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(300, 300);
mWidth = getWidth();
//mPadding = 1;
initRectf();
}
private void initRectf() {
//initrectf既可以画矩形,也可以画圆,而rect不可以画圆
mRectF = new RectF(0, 0, mWidth, mWidth);
}
@Override
public void onClick(View v) {
Random random = new Random();
int f =random.nextInt(1000);
RotateAnimation rotateAnimation = new RotateAnimation(kaishijiaodu, f+360,mWidth/2,mWidth/2);
//旋转时间
rotateAnimation.setDuration(3000);
//执行完后的位置
rotateAnimation.setFillAfter(true);
startAnimation(rotateAnimation);
kaishijiaodu=f%360;
}
}
然后在main的布局里调用自己创建的类名
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.zhuzhuangtu.weight.MyView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>