luckyTurnTable
由于公司前段时间的抽奖活动的需求,所以做了两款幸运大转盘,下面是效果图:
圆形转盘
方形转盘
废话不多说直接上代码:
首先是圆形转盘
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 "";
}
}
}
最后附上源码地址,欢迎来star哦
https://github.com/lwang057/luckyTurnTable.git