android 自定義View輪盤抽獎
package com.bawie.customerview;
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.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
public class ChoujiangView extends View implements View.OnClickListener {
private int screenWith;//螢幕寬度
private int screenHeigh;//螢幕高度
private Paint mPaint;//畫筆工具
private int centerX;
private int centerY;
private int[] colors;
private RotateAnimation rotateAnimation;
private String[] desc = new String[]{"性感", "豐滿", "知性", "聰明", "賢惠", "優秀"};
private boolean isRote;//是否在旋轉狀態
public ChoujiangView(Context context) {
this(context, null);
}
public ChoujiangView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public ChoujiangView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//獲取螢幕寬高資訊
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
screenWith = displayMetrics.widthPixels;
screenHeigh = displayMetrics.heightPixels;
//獲取螢幕中心座標
centerX = screenWith / 2;
centerY = screenHeigh / 2;
//初始化畫筆
initPaint();
colors = new int[]{Color.RED, Color.GRAY, Color.YELLOW, Color.BLUE, Color.GREEN, Color.DKGRAY, Color.WHITE};
//初始化旋轉動畫
initAnimation();
//給自己新增點選事件
this.setOnClickListener(this);
}
//測量View大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(600, 600);
}
//繪圖
@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.RED);
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);
}
}
private void initPaint() {
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(20);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
}
private void initAnimation() {
rotateAnimation = new RotateAnimation(0, 360,
centerX, centerY);
rotateAnimation.setDuration(800);
rotateAnimation.setFillAfter(true);
rotateAnimation.setRepeatCount(-1);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setRepeatMode(Animation.RESTART);
}
private void startAnima() {
isRote=true;
startAnimation(rotateAnimation);
}
private void stopAnima() {
isRote=false;
clearAnimation();
}
@Override
public void onClick(View v) {
if (isRote) {
stopAnima();
setRoundDom();
}else {
startAnima();
}
}
//給一個隨機的抽獎結果
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);
}
}