Class
package com.qh.***.luck;
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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
/*
*
*
* 设置转盘的宽高
绘制圆形背景
绘制转盘
绘制转盘上的文字和图片
转盘的旋转动画以及监听
指定旋转的位置
* */
public class LuckView extends View implements View.OnClickListener {
//屏幕的宽高
private int screenWidth;
private int screenHeight;
//圆点中心
private int centerX;
private int centerY;
//画笔
private Paint mPaint;
private int[] colors = new int[]{Color.YELLOW, Color.BLUE, Color.LTGRAY, Color.RED, Color.GRAY, Color.RED, Color.GREEN};
private String[] desc = new String[]{"琴音", "棋圣", "书香", "画仙", "沉鱼", "落雁"};
//是否在旋转状态
private boolean isRote;
//旋转状态
private RotateAnimation rotateAnimation;
public LuckView(Context context) {
this(context, null);
}
public LuckView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LuckView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//屏幕的宽高
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels;
//获取屏幕中心坐标
centerX = screenWidth / 2;
centerY = screenHeight / 2;
//初始化画笔
initPaint();
//初始化旋转动画
initAnimation();
//给自己添加点击事件
this.setOnClickListener(this);
}
//画笔
private void initPaint() {
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(20);
mPaint.setStyle(Paint.Style.FILL);
//抗锯齿
mPaint.setAntiAlias(true);
}
//测量大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(0, 0);
}
//绘图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//移动画布的坐标原点
canvas.translate(centerX, centerY);
//绘制6个圆弧
RectF rect = new RectF(-300, -300, 300, 300);
//初始值 (角度)
float start = 60;
for (int i = 0; i < 6; i++) {
mPaint.setColor(colors[i]);
canvas.drawArc(rect, start * i, 60, true, mPaint);
}
//绘制中心的圆
mPaint.setColor(Color.CYAN);
canvas.drawCircle(0, 0, 100, mPaint);
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(40);
//获取文字宽度和高度
Rect rectText = new Rect();
mPaint.getTextBounds("start", 0, 5, rectText);
int width = rectText.width();
int height = rectText.height();
canvas.drawText("start", -width / 2, height / 2, mPaint);
//绘制描述信息
RectF rectF = new RectF(-200, -200, 200, 200);
for (int i = 0; i < 6; i++) {
mPaint.setColor(Color.WHITE);
Path path = new Path();
path.addArc(rectF, start * i + 15, 60);
canvas.drawTextOnPath(desc[i], path, 0, 0, mPaint);
}
}
//点击事件
@Override
public void onClick(View v) {
//正在旋转的状态
if (isRote) {
//停止状态
stopAnima();
setRoundDom();
} else {
//开启旋转状态
startAnima();
}
}
//转盘开始操作
private void startAnima() {
isRote = true;
//现在启动指定的动画。
startAnimation(rotateAnimation);
}
//转盘停止操作
private void stopAnima() {
isRote = false;
//取消此视图的任何动画
clearAnimation();
}
//给一个随机的抽奖结果
private void setRoundDom() {
double random = Math.random();
RotateAnimation rotateAnimation2 = new RotateAnimation(0, (float) (360 * random), centerX, centerY);
rotateAnimation2.setDuration(100);
//动画执行完后是否停留在执行完的状态
rotateAnimation2.setFillAfter(true);
//判断开启定时器
startAnimation(rotateAnimation2);
}
private void initAnimation() {
//设置旋转动画
rotateAnimation = new RotateAnimation(0, 360, centerX, centerY);
//时长
rotateAnimation.setDuration(800);
//动画执行完后是否停留在执行完的状态
//rotateAnimation.setFillAfter(true);
//设置重复次数 <0 无限重复
rotateAnimation.setRepeatCount(-1);
//插值器
rotateAnimation.setInterpolator(new LinearInterpolator());
//重复模型 定义动画在结束时应该做什么。这
//*仅当重复计数大于或大于时才应用设置
//* 0或{@link #INFINITE}。默认为{@link #RESTART}。
rotateAnimation.setRepeatMode(Animation.RESTART);
}
}
在布局文件里引用一下就OK啦