Android自定義幸運轉盤
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 "";
}
}
}
相關文章
- android自定義鍵盤 自定義身份證鍵盤Android
- thinkphp5 抽獎幸運大轉盤PHP
- android 自定義鍵盤Android
- 自定義Android鍵盤Android
- android自定義view(自定義數字鍵盤)AndroidView
- Android自定義數字鍵盤Android
- Android自定義收銀鍵盤Android
- Android自定義view之emoji鍵盤AndroidView
- Android自定義View之星球運動AndroidView
- 商家如何製作幸運大轉盤抽獎小程式 快速吸粉?
- Android - JoystickView 虛擬手柄,控制盤,自定義UIAndroidJoystickView虛擬手柄UI
- vue自定義鍵盤Vue
- 自定義鍵盤(一)
- 自定義鍵盤(二)
- 自定義儀表盤
- Swift 自定義運算子Swift
- 幸運數字
- Swift中自定義運算子Swift
- 自定義 Android 鐘表盤,這一篇就夠了Android
- Android 最簡單的自定義數字鍵盤之一Android
- 小米手環錶盤自定義
- 自定義身份證鍵盤(Swift)Swift
- Android 自定義viewAndroidView
- Android 自定義 TabLayoutAndroidTabLayout
- Android: 自定義ViewAndroidView
- Android自定義ToastAndroidAST
- Android 自定義 DrawableAndroid
- android自定義View&自定義ViewGroup(下)AndroidView
- android自定義View&自定義ViewGroup(上)AndroidView
- Android自定義控制元件——自定義屬性Android控制元件
- Android SeekBar 自定義thumb,thumb旋轉動畫效果Android動畫
- Android自定義控制元件之自定義屬性Android控制元件
- (Android自定義控制元件)Android自定義狀態提示圖表Android控制元件
- Swift自定義表情鍵盤+錄音Swift
- iOS 自定義鍵盤字母按鈕iOS
- Android 自定義UI元件AndroidUI元件
- Android自定義遮罩層Android遮罩
- Android自定義View整合AndroidView