Android九宫格抽奖效果实现逻辑,要求中奖位置可控

ps:最近在写app活动抽奖,其中有九宫格效果(按道理不应该是前端写好了,app直接用页面吗。但是莫名其妙让Android开发者使用原生控件实现,可能看我比较闲吧)

实现逻辑

  1. 抽奖分为初始化,加速n次,一直匀速(等待网络请求结果),补位到0位置,减速到指定位置,5个步骤。
  2. 其他补缴好理解,补位的作用就是实现中奖位置可控的关键,当我们网络请求得到要高亮的位置后,我们只要发选中的位置补到0位置,然后再转到位置就可以了。
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.xiaodou.mobile.R;
import com.xiaodou.mobile.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

public class TestActivity extends BaseActivity {
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.view5)
    View view5;
    @BindView(R.id.view6)
    View view6;
    @BindView(R.id.view7)
    View view7;
    @BindView(R.id.view8)
    View view8;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        handler = null;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dou_activity_luck;
    }

    @Override
    public void afterLoadLayout(Bundle savedInstanceState) {
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);
        viewList.add(view5);
        viewList.add(view6);
        viewList.add(view7);
        viewList.add(view8);
    }


    @OnClick({R.id.btnStart, R.id.btnStop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnStart:
                // 开始抽奖,viewList.size()就是加速的次数。从startIntervalTime 加到runIntervalTime
                handler.sendMessage(Message.obtain(handler, 0, viewList.size(), 0));
                break;
            case R.id.btnStop:
                 // 停止抽奖,中奖位置可控
                int win = new Random().nextInt(viewList.size());
                Log.i(TAG, "onViewClicked: 中奖位置 " + win);
                // viewList.size() + win是减速次数,多加viewList.size() ,防止win为0
                handler.sendMessage(Message.obtain(handler, 3, viewList.size() + win, 0));
                break;
        }
    }

    // 8个跑马灯view
    private List viewList = new ArrayList<>();

    // 第一次,每一次,最后一次动画时间间隔
    private final int startIntervalTime = 300;
    private final int runIntervalTime = 100;
    private final int endIntervalTime = 400;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 0: // 开始
                    runCount = 0;
                    handler.sendMessage(Message.obtain(handler, 1, message.arg1, 0));
                    replyUI();
                    runCount++;
                    select(runCount);
                    break;
                case 1: // 加速
                    handler.removeMessages(0);
                    if (message.arg1 > message.arg2) {
                        handler.sendMessageDelayed(Message.obtain(handler, 1, message.arg1, ++message.arg2),
                                startIntervalTime - (startIntervalTime - runIntervalTime) / message.arg1 * message.arg2);
                        runCount++;
                        select(runCount);
                    } else {
                        handler.sendEmptyMessageDelayed(2, runIntervalTime);
                    }
                    break;
                case 2: // 匀速
                    handler.removeMessages(1);
                    handler.sendEmptyMessageDelayed(2, runIntervalTime);
                    runCount++;
                    select(runCount);
                    break;
                case 3: // 补位到0位置
                    handler.removeMessages(2);
                    if (isSelectUI()) {
                        handler.sendMessageDelayed(Message.obtain(handler, 4, message.arg1, message.arg2), runIntervalTime);
                    } else {
                        handler.sendMessageDelayed(Message.obtain(handler, 3, message.arg1, message.arg2), runIntervalTime);
                        runCount++;
                        select(runCount);
                    }
                    break;
                case 4: // 减速
                    handler.removeMessages(3);
                    if (message.arg1 > message.arg2) {
                        handler.sendMessageDelayed(Message.obtain(handler, 4, message.arg1, ++message.arg2),
                                runIntervalTime + ((endIntervalTime - runIntervalTime) / message.arg1) * message.arg2);
                        runCount++;
                        select(runCount);
                    } else {
                        handler.removeMessages(4);
                    }
                    break;
            }
            return false;
        }
    });

    // 计数,跑了多少次
    private int runCount = 0;

    /**
     * UI 选中那一个,并且回复上一个
     */
    public void select(int index) {
        try {
            if (index < 0) {
                replyUI();
                return;
            }
            // 要选中哪一个
            int selectIndex = index % viewList.size();
            // 恢复上一个
            int upIndex = ((selectIndex - 1) + viewList.size()) % viewList.size();
            viewList.get(selectIndex).setBackgroundColor(Color.RED);
            viewList.get(selectIndex).setTag(true);
            viewList.get(upIndex).setBackgroundColor(Color.WHITE);
            viewList.get(upIndex).setTag(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * UI 恢复全白
     */
    public void replyUI() {
        for (View view : viewList) {
            view.setBackgroundColor(Color.WHITE);
        }
    }

    /**
     * 判断0位置是否是选中状态
     */
    public boolean isSelectUI() {
        return (Boolean) viewList.get(0).getTag();
    }

}

你可能感兴趣的:(Android九宫格抽奖效果实现逻辑,要求中奖位置可控)