RolePlayActivity.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.xunao.effectdemo.activity;
  2. import androidx.recyclerview.widget.LinearLayoutManager;
  3. import androidx.recyclerview.widget.RecyclerView;
  4. import androidx.recyclerview.widget.SimpleItemAnimator;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import com.xunao.effectdemo.R;
  12. import com.xunao.effectdemo.adapter.RolePlayAdapter;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Objects;
  16. import java.util.Random;
  17. import java.util.Timer;
  18. import java.util.TimerTask;
  19. public class RolePlayActivity extends Activity {
  20. private static final String TAG = "RolePlayActivity";
  21. private Button btnStart;
  22. private Button btnGo;
  23. private RecyclerView rvMarquee;
  24. private RolePlayAdapter rolePlayAdapter;
  25. private List<String> roleList = new ArrayList<>();
  26. private String currentRole = "角色1"; // 拿到随机选中的角色
  27. // 跑马灯
  28. private boolean isGameRunning = false; //是否处于转动
  29. private boolean isTryToStop = false;//是否增速/减速
  30. private int currentTotal = 0;
  31. private static final int DEFAULT_SPEED = 300; //默认/最慢速度
  32. private static final int MIN_SPEED = 50;//最快速度
  33. private int currentSpeed = DEFAULT_SPEED;//当前速度
  34. Timer timer = new Timer();
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_role_play);
  39. initView();
  40. }
  41. private void initView(){
  42. btnGo = findViewById(R.id.btn_go);
  43. btnGo.setOnClickListener(v -> {
  44. Intent intent = new Intent(RolePlayActivity.this, VideoPlayActivity.class);
  45. intent.putExtra("role", currentRole);
  46. Log.i(TAG, "传的角色:"+currentRole);
  47. startActivity(intent);
  48. });
  49. btnStart = findViewById(R.id.btn_start);
  50. btnStart.setOnClickListener(v -> {
  51. int stayIndex = new Random().nextInt(roleList.size());
  52. Log.i(TAG, "stayIndex:" + stayIndex);
  53. startGame(stayIndex);
  54. });
  55. rvMarquee = findViewById(R.id.rv_marquee);
  56. ((SimpleItemAnimator) Objects.requireNonNull(rvMarquee.getItemAnimator())).setSupportsChangeAnimations(false);
  57. // 设置横向布局
  58. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false){
  59. @Override
  60. public boolean canScrollHorizontally() {
  61. return false;
  62. }
  63. };
  64. rvMarquee.setLayoutManager(linearLayoutManager);
  65. roleList.add("角色1");
  66. roleList.add("角色2");
  67. roleList.add("角色3");
  68. roleList.add("角色4");
  69. roleList.add("角色5");
  70. roleList.add("角色6");
  71. rolePlayAdapter = new RolePlayAdapter(roleList, this);
  72. rvMarquee.setAdapter(rolePlayAdapter);
  73. }
  74. //开始抽奖
  75. public void startGame(final int stayIndex) {
  76. if (isGameRunning) { //运动中
  77. return;
  78. }
  79. isGameRunning = true;
  80. isTryToStop = false;
  81. currentSpeed = DEFAULT_SPEED;
  82. new Thread(() -> {
  83. while (isGameRunning) {
  84. try {
  85. Thread.sleep(getInterruptTime());
  86. } catch (InterruptedException e) {
  87. e.printStackTrace();
  88. }
  89. // 防止线程多走了一次
  90. if (!isGameRunning) {
  91. return;
  92. }
  93. // 改变高亮项的索引
  94. runOnUiThread(() -> rolePlayAdapter.changeIndex());
  95. Log.i(TAG, "到" + roleList.get(rolePlayAdapter.getCurrentIndex()) + "--" + rolePlayAdapter.getCurrentIndex());
  96. //减速&&速度达到最小&&当前view为指定view——停止
  97. if (isTryToStop && currentSpeed == DEFAULT_SPEED && stayIndex == rolePlayAdapter.getCurrentIndex()) {
  98. isGameRunning = false;
  99. Log.i(TAG, "结束:角色为" + roleList.get(rolePlayAdapter.getCurrentIndex()) + "--" + rolePlayAdapter.getCurrentIndex());
  100. // listener.onShop(currentIndex);
  101. currentRole = roleList.get(rolePlayAdapter.getCurrentIndex());
  102. }
  103. }
  104. }).start();
  105. //2秒后开始减速
  106. timer.schedule(new TimerTask() {
  107. @Override
  108. public void run() {
  109. tryToStop();
  110. }
  111. }, 5000);
  112. }
  113. //改变加速状态
  114. public void tryToStop() {
  115. isTryToStop = true;
  116. }
  117. //获取sleep时间,即转盘运动速度,越小即越快。
  118. private long getInterruptTime() {
  119. currentTotal++;
  120. if (isTryToStop) {//减速直至恢复为默认速度
  121. currentSpeed += 10;
  122. if (currentSpeed > DEFAULT_SPEED) {
  123. currentSpeed = DEFAULT_SPEED;
  124. }
  125. } else {//增速直至最快速度
  126. //第一圈不做加速
  127. if (currentTotal / roleList.size() > 0) {
  128. currentSpeed -= 20;
  129. }
  130. if (currentSpeed < MIN_SPEED) {
  131. currentSpeed = MIN_SPEED;
  132. }
  133. }
  134. return currentSpeed;
  135. }
  136. }