Android自定義幸運轉盤

lwang057發表於2017-12-18

luckyTurnTable

由於公司前段時間抽獎活動的需求,所以小農依葫蘆畫瓢做了兩款幸運轉盤。

附上GitHub地址,如果老鐵感覺有用請記得star!!!!!
https://github.com/lwang057/luckyTurnTable.git
效果展示:
  • 圓形轉盤
    圓形轉盤

  • 方形轉盤
    這裡寫圖片描述

  • 廢話不多說直接上程式碼:

首先是圓形轉盤

package com.lwang.luckyturntable;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Random;


public class CircleTurntableActivity extends AppCompatActivity implements View.OnClickListener {

    private Animation mStartAnimation;
    private Animation mEndAnimation;
    private ImageView mLuckyTurntable;
    private boolean isRunning;
    private int mPrizeGrade = 6; //獎品級別,0代表沒有
    private int mItemCount = 3;
    private int[] mPrizePosition = {0, 4, 2, 1, 5, 3}; //獎品在轉盤中的位置(到達一等獎的距離)


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_turntable);

        mLuckyTurntable = (ImageView) findViewById(R.id.id_lucky_turntable);
        ImageView mStartBtn = (ImageView) findViewById(R.id.id_start_btn);
        mStartBtn.setOnClickListener(this);

        mStartAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
        mStartAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });


    }


    @Override
    public void onClick(View v) {

        // 未抽過獎並有抽獎的機會
        if (!isRunning) {

            isRunning = true;
            mStartAnimation.reset();
            mLuckyTurntable.startAnimation(mStartAnimation);

            if (mEndAnimation != null) {
                mEndAnimation.cancel();
            }

            new Handler().postDelayed(new Runnable() {

                public void run() {
                    endAnimation();
                }
            }, 2000);
        }

    }


    // 結束動畫,慢慢停止轉動,抽中的獎品定格在指標指向的位置
    private void endAnimation() {

        int position = mPrizePosition[mPrizeGrade - 1];
        float toDegreeMin = 360 / mItemCount * (position - 0.5f) + 1;
        Random random = new Random();
        int randomInt = random.nextInt(360 / mItemCount - 1);
        float toDegree = toDegreeMin + randomInt + 360 * 5; //5周 + 偏移量

        // 按中心點旋轉 toDegree度
        // 引數:旋轉的開始角度、旋轉的結束角度、X軸的伸縮模式、X座標的伸縮值、Y軸的伸縮模式、Y座標的伸縮值
        mEndAnimation = new RotateAnimation(0, toDegreeMin, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mEndAnimation.setDuration(3000); // 設定旋轉時間
        mEndAnimation.setRepeatCount(0); // 設定重複次數
        mEndAnimation.setFillAfter(true);// 動畫執行完後是否停留在執行完的狀態
        mEndAnimation.setInterpolator(new DecelerateInterpolator()); // 動畫播放的速度
        mEndAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isRunning = false;
                Toast.makeText(CircleTurntableActivity.this, "富光350ml水杯", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mLuckyTurntable.startAnimation(mEndAnimation);
        mStartAnimation.cancel();
    }


    //停止動畫(異常情況,沒有獎品)
    private void stopAnimation() {

        //轉盤停止回到初始狀態
        if (isRunning) {

            mStartAnimation.cancel();
            mLuckyTurntable.clearAnimation();
            isRunning = false;
        }
    }

}

方形轉盤

package com.lwang.luckyturntable;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.lwang.luckyturntable.view.LuckyMonkeyPanelView;

import java.util.Random;

public class SudokuTurnTableActivity extends AppCompatActivity {


    private LuckyMonkeyPanelView luckyPanelView;
    private ImageView mDrawBtn;
    private long drawTime; //抽獎時間
    private int MARK_LUCKY = 6; //中獎標記
    private static Handler handler = new Handler();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sudoku_turn_table);

        luckyPanelView = (LuckyMonkeyPanelView) findViewById(R.id.lucky_panel);
        mDrawBtn = (ImageView) findViewById(R.id.id_draw_btn);

        mDrawBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (System.currentTimeMillis() - drawTime < 5000) {
                    Toast.makeText(SudokuTurnTableActivity.this, "心急吃不了熱豆腐,請5秒後再點選哦", Toast.LENGTH_SHORT).show();
                    return;
                }

                //開始抽獎
                if (!luckyPanelView.isGameRunning()) {
                    drawTime = System.currentTimeMillis();
                    luckyPanelView.startGame();
                    getLuck();
                }
            }
        });
    }



    private void getLuck() {

        long delay = 0; //延長時間
        long duration = System.currentTimeMillis() - drawTime;
        if (duration < 5000) {
            delay = 5000 - duration;
        }

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                if (SudokuTurnTableActivity.this.isFinishing()) {
                    return;
                }

                luckyPanelView.tryToStop(getPrizePosition(MARK_LUCKY));
                luckyPanelView.setGameListener(new LuckyMonkeyPanelView.LuckyMonkeyAnimationListener() {
                    @Override
                    public void onAnimationEnd() {
                        //延長1S彈出抽獎結果
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(SudokuTurnTableActivity.this, getPrizeName(MARK_LUCKY), Toast.LENGTH_SHORT).show();
                            }
                        }, 1000);
                    }
                });
            }
        }, delay);
    }


    /**
     * 根據獎品等級計算出獎品位置
     * @param prizeGrade
     * @return
     */
    private int getPrizePosition(int prizeGrade) {
        switch (prizeGrade) {
            case 1:
                return 0;
            case 2:
                return 4;
            case 3:
                return 2;
            case 4:
                return 5;
            case 5:
                return 7;
            case 6: //六等獎有三個位置,隨機取一個
                int[] position = {1, 3, 6};
                Random random = new Random();
                return position[random.nextInt(3)];
        }
        return prizeGrade;
    }


    /**
     * 獎品名稱
     * @param grade
     * @return
     */
    private String getPrizeName(int grade) {
        switch (grade) {
            case 1:
                return "iPhone 8 手機一部";
            case 2:
                return "Beats 耳機一副";
            case 3:
                return "周大福轉運珠一顆";
            case 4:
                return "小米體重稱一個";
            case 5:
                return "暴風魔鏡VR眼鏡一副";
            case 6:
                return "愛奇藝月卡會員";
            default:
                return "";
        }
    }

}

相關文章