自定義view實現抽獎轉盤
------>自定義view類
public class LotteryView extends View implements View.OnClickListener {
private Paint mPaint;
private DisplayMetrics displayMetrics;
private int widthPixels;
private int heightPixels;
private int centerX;
private int centerY;
private int[] colors;
private String[] descPath = new String[]{
"親一下","抱一下","喝一杯","找人喝","一口悶","大冒險"
};
private boolean isPath;//定義一個變數用於判斷旋轉狀態
public LotteryView(Context context) {
this(context, null);
}
public LotteryView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public LotteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//獲取螢幕寬高資訊
displayMetrics = context.getResources().getDisplayMetrics();
widthPixels = displayMetrics.widthPixels;//寬
heightPixels = displayMetrics.heightPixels;//高
//獲取螢幕中心座標
centerX = widthPixels / 2;
centerY = heightPixels / 2;
//初始化畫筆
initPaint();
//給每個扇形弧度新增顏色
colors = new int[]{
Color.CYAN, Color.GRAY, Color.DKGRAY,
Color.BLUE, Color.GREEN, Color.RED
};
//給自己新增點選事件
this.setOnClickListener(this);
}
//測量view大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
//繪圖
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//移動畫布的座標原點
canvas.translate(centerX, centerY);
//繪製6個弧度,扇形,並新增顏色
RectF f = new RectF(-300, -300, 300, 300);
float start = 60;
for (int i = 0; i < 6; i++) {
mPaint.setColor(colors[i]);
canvas.drawArc(f, start * i, 60, true, mPaint);
}
//繪製中心的圓
mPaint.setColor(Color.BLUE);//設定中間圓的顏色
canvas.drawCircle(0, 0, 100, mPaint);
mPaint.setColor(Color.WHITE);//設定字型顏色
mPaint.setTextSize(30);//設定字型大小
//獲取文字的寬高
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(descPath[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);
}
@Override
public void onClick(View view) {
if (!isPath){
//旋轉狀態
startAnima();
}
}
//旋轉狀態
private void startAnima() {
isPath = true;
double random = Math.random();
RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (720 * random), centerX, centerY);
rotateAnimation.setDuration(800);
rotateAnimation.setFillAfter(true);
//設定重複次數
rotateAnimation.setRepeatMode(Animation.RESTART);
//給動畫新增監聽
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
isPath = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
startAnimation(rotateAnimation);
}
//定製隨機數,給出一個隨機結果
private void setRoundDom(){
double random = Math.random();
RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (360 * random), centerX, centerY);
rotateAnimation.setDuration(100);
rotateAnimation.setFillAfter(true);
startAnimation(rotateAnimation);
}
}