1. 程式人生 > >android studio抽獎轉盤實現

android studio抽獎轉盤實現

首先建立一個類,繼承view實現三個方法

package com.zhuanpan.turntable;

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;

/**

  • date:2018/11/3.
  • author:遷就
  • function:
    */

public class Truntable extends View implements View.OnClickListener{
//定義顏色的陣列
private int[] color = new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.DKGRAY,Color.GRAY};
//定義文字
private String[] textColor = new String[]{“紅 色”,“綠 色”,“藍 色”,“黃 色”,“深 灰”,“淺 灰”};
//螢幕的中心點
private int mWidth;
private int mHeight;
private Paint mPaint;
private RotateAnimation mAnimation;
private boolean isRote;

public Truntable(Context context) {
    this(context,null);
}

public Truntable(Context context,  AttributeSet attrs) {
    this(context, attrs,-1);
}

public Truntable(Context context,  AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //獲取螢幕適配
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    //獲取螢幕的寬和高
    int widthPixels = displayMetrics.widthPixels;
    int heightPixels = displayMetrics.heightPixels;
    //得到中心點 所以要除2
    mWidth = widthPixels / 2;
    mHeight = heightPixels / 2;
    //初始化資料 畫筆
    initPaint();
    //動畫
    initAnimation();
    //點選事件
    setOnClickListener(this);
}

private void initPaint() {
    //建立一個畫筆
    mPaint = new Paint();
    //給畫筆設定顏色
    mPaint.setColor(Color.RED);
    //給畫筆設定寬度
    mPaint.setStrokeWidth(2);
    //消除毛邊
    mPaint.setAntiAlias(true);
    //設定樣式填充
    mPaint.setStyle(Paint.Style.FILL);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //動畫  找到中心點  平移
    canvas.translate(mWidth,mHeight);
    RectF rectF = new RectF(-260,-260,260,260);
    //定義一個
    float start = 60;
    //分為6個
    for (int i = 0; i < 6; i++) {
        //迴圈每個不同的顏色
        mPaint.setColor(color[i]);
        //畫圓弧
        canvas.drawArc(rectF,start*i,60,true,mPaint);
    }
    //畫筆設定顏色
    mPaint.setColor(Color.RED);
    //開始畫圓           x     y        半徑
    canvas.drawCircle(0,0,100,mPaint);
    //設定字型顏色和大小
    mPaint.setColor(Color.WHITE);
    mPaint.setTextSize(20);
    //建立
    Rect rect = new Rect();
    mPaint.getTextBounds("start",0,5,rect);
    //定義rect寬和高
    int width = rect.width();
    int height = rect.height();
    canvas.drawText("start",-width/2,height/2,mPaint);

    RectF rectF1 = new RectF(-200,-200,200,200);
    //這個是文字的迴圈
    for (int i = 0; i <6 ; i++) {
        //文字
        mPaint.setColor(Color.WHITE);
        mPaint.setTextSize(20);
        Path path = new Path();
        path.addArc(rectF1,start*i+20,60);
        canvas.drawTextOnPath(textColor[i],path,0,0,mPaint);
    }

}

/**
 * 動畫  旋轉
 */
private void initAnimation() {
    mAnimation = new RotateAnimation(0, 360, mWidth, mHeight);
    //設定重複的次數
    mAnimation.setRepeatCount(-1);
    //設定終止填充
    mAnimation.setFillAfter(true);
    mAnimation.setDuration(400);
    //是Animation的xml的一個屬性
    mAnimation.setInterpolator(new LinearInterpolator());
    //設定重複模式
    mAnimation.setRepeatMode(Animation.RESTART);

}
private void start(){
    startAnimation(mAnimation);
    isRote = true;
}
private void stop(){
    isRote = false;
    clearAnimation();
}

/**
 * 點選事件
 * @param
 */
@Override
public void onClick(View v) {
    if (isRote){
        stop();
        // radom();
    }else {
        start();
    }
}

/*private void radom() {
    //獲取隨機的顏色值
    double random = Math.random();
    RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (random * 100), mWidth, mHeight);
    rotateAnimation.setDuration(4000);
    rotateAnimation.setFillAfter(true);
    startAnimation(rotateAnimation);
}*/

}

在Activity主佈局中匯入Truntable類佈局

<com.zhuanpan.turntable.Truntable
android:layout_width=“wrap_content”
android:layout_height=“wrap_content” />