android 老虎机实现

最近一直在看波场的小游戏,自己试着手动搞了个老虎机。

先上效果:

 主要有两个效果,一个滚轮型,另一种是转盘的。

1:滚轮型

     主要是左中右三个滚轮,我这里没有用wheelView来实现,直接用了recyclerview主要实现了下效果,没有太细节的大家包含。滚轮型效果是左侧滚轮停下后,中间和右侧的逐渐停止,右边的最后停下。

public void onStartClicked(){

        if (isStart){
            binding.btn.setText("开始");
            isStart=false;
            
            new Thread(new Runnable() {
                @Override
                public void run() {
                   for (int i=0;i<20;i++){
                        if (i<10){
                            try {
                                Thread.sleep(50);
                                handler.sendEmptyMessage(2);
                                handler.sendEmptyMessage(3);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else{
                            try {
                                Thread.sleep(50);
                                handler.sendEmptyMessage(3);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }).start();

        }else{
            isStart=true;
            binding.btn.setText("停止");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (isStart){
                            Thread.sleep(50);
                            handler.sendEmptyMessage(1);
                            handler.sendEmptyMessage(2);
                            handler.sendEmptyMessage(3);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

handler的代码如下:

 Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what==1){
                if (numLeft>Integer.MAX_VALUE-100){
                    numLeft=0;
                }
                numLeft+=(Math.random()*10+1);
                binding.recyclerLeft.scrollToPosition(numLeft);
            }else if (msg.what==2){
                if (numCenter>Integer.MAX_VALUE-100){
                    numCenter=0;
                }
                numCenter+=(Math.random()*10+1);
                binding.recyclerCenter.scrollToPosition(numCenter);
            }else if (msg.what==3){
                if (numRight>Integer.MAX_VALUE-100){
                    numRight=0;
                }
                numRight+=(Math.random()*10+1);
                binding.recyclerRight.scrollToPosition(numRight);
            }
            Log.e(TAG,"left--"+numLeft+"###center--"+numCenter+"###right--"+numRight);
            return false;
        }
    });

2:轮盘型

     转盘型的老虎机,我这里主要给出了四种水果,以及一种轮空的图片,所以布局上下五个。

      这里使用组合控件自定义view,实现上是两部分:一部分是item,另一部分主要panel。

     item布局如下:

  

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/item_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/item_img"
            android:layout_centerInParent="true"
            />
        <View
            android:id="@+id/item_layer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/item_layer"
            android:visibility="invisible"
            />

    </RelativeLayout>

</FrameLayout>

 十分简单的布局,图片以及阴影效果;

在写item的java文件之前,我们先定义个接口,用于判断滚动到item时是否阴影的显示。

 

/**
 * create by zj on 2018/12/14
 */
public interface ItemView {
    void setFocus(boolean isFocused);
}

 接口定义完成,我们可以来编写item了。

 item主要就一个参数,一个是type,我这里用type来判断每个item的水果图片。

然后继承framelayout,实现接口ItemView。

 

/**
 * create by zj on 2018/12/14
 */
public class TigerItemPanel extends FrameLayout implements ItemView {

    private View view;
    private ImageView imageView;
    public TigerItemPanel(@NonNull Context context) {
        this(context,null);
    }

    public TigerItemPanel(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TigerItemPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TigerItemPanel);

        int type=mTypedArray.getInteger(R.styleable.TigerItemPanel_bg_type,0);
        inflate(context, R.layout.item_turnplate,this);
        view=findViewById(R.id.item_layer);
        imageView=findViewById(R.id.item_img);

        switch (type){
            case 0:
                imageView.setImageResource(R.drawable.apple);
                break;
            case 1:
                imageView.setImageResource(R.drawable.banana);
                break;
            case 2:
                imageView.setImageResource(R.drawable.lime);
                break;
            case 3:
                imageView.setImageResource(R.drawable.watermelon);
                break;
            case 4:
                imageView.setImageResource(R.drawable.item_null);
                break;
        }
    }


    @Override
    public void setFocus(boolean isFocused) {
        view.setVisibility(isFocused?VISIBLE:INVISIBLE);
    }
}

  item编写完成,开始panel的编写。

  布局文件就不写了,主要就是放了十几个item形成转盘。

  这里view主要有几个方法,一个是开始游戏,一个是结束游戏,另外再加上游戏的状态isRunning获取。

  另外就是获取thread的sleep的时间,开始游戏时,sleep的时间需要逐渐变小,速度逐渐加快,结束相反,达到最慢的速度时,

停止到指定的index。

/**
 * create by zj on 2018/12/14
 * 转盘老虎机
 */
public class TigerPanel extends FrameLayout {
    private TigerItemPanel itemPanel1,itemPanel2,itemPanel3,itemPanel4,itemPanel5,itemPanel6,itemPanel7,itemPanel8,
            itemPanel9,itemPanel10,itemPanel11,itemPanel12,itemPanel13,itemPanel14,itemPanel15,itemPanel16;

    private ItemView[] views=new ItemView[16];
    private static final int DEFAULT_SPEED = 80;
    private static final  int MIN_SPEED=20;
    private int currentSpeed = 50;
    private boolean isToStop;
    private boolean isRunning;
    private int currentIndex=0;
    private int currentTotal=0;
    private int stayIndex = 0;
    public TigerPanel(@NonNull Context context) {
        this(context,null);
    }

    public TigerPanel(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TigerPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_panel,this);
        initView();
    }

    private void initView() {
        itemPanel1=findViewById(R.id.item1);
        itemPanel2=findViewById(R.id.item2);
        itemPanel3=findViewById(R.id.item3);
        itemPanel4=findViewById(R.id.item4);
        itemPanel5=findViewById(R.id.item5);
        itemPanel6=findViewById(R.id.item6);
        itemPanel7=findViewById(R.id.item7);
        itemPanel8=findViewById(R.id.item8);
        itemPanel9=findViewById(R.id.item9);
        itemPanel10=findViewById(R.id.item10);
        itemPanel11=findViewById(R.id.item11);
        itemPanel12=findViewById(R.id.item12);
        itemPanel13=findViewById(R.id.item13);
        itemPanel14=findViewById(R.id.item14);
        itemPanel15=findViewById(R.id.item15);
        itemPanel16=findViewById(R.id.item16);

        views[0]=itemPanel1;
        views[1]=itemPanel2;
        views[2]=itemPanel3;
        views[3]=itemPanel4;
        views[4]=itemPanel5;
        views[5]=itemPanel7;
        views[6]=itemPanel9;
        views[7]=itemPanel11;
        views[8]=itemPanel16;
        views[9]=itemPanel15;
        views[10]=itemPanel14;
        views[11]=itemPanel13;
        views[12]=itemPanel12;
        views[13]=itemPanel10;
        views[14]=itemPanel8;
        views[15]=itemPanel6;

    }

    private long getInterruptTime() {
        currentTotal++;

        if (isToStop) {
            currentSpeed += 10;
            //速度逐渐减慢
            if (currentSpeed > DEFAULT_SPEED) {
                currentSpeed = DEFAULT_SPEED;
            }
        } else {
            //逐渐加快,达到最快时速度稳定下来
            if (currentTotal / views.length > 0) {
                currentSpeed -= 10;
            }
            if (currentSpeed < MIN_SPEED) {
                currentSpeed = MIN_SPEED;
            }
        }
        return currentSpeed;
    }

    //开始游戏
    public void startGame(){
        isRunning=true;
        isToStop=false;

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRunning) {
                    try {
                        Thread.sleep(getInterruptTime());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    post(new Runnable() {
                        @Override
                        public void run() {
                            int preIndex = currentIndex;
                            currentIndex++;
                            if (currentIndex >= views.length) {
                                currentIndex = 0;
                            }

                            views[preIndex].setFocus(false);
                            views[currentIndex].setFocus(true);

                            if (isToStop && currentSpeed == DEFAULT_SPEED && stayIndex == currentIndex) {
                                isRunning = false;
                            }
                        }
                    });
                }
            }
        }).start();

    }


    public boolean getRunningStaus(){
        return  isRunning;
    }

    public void tryToStop(int position) {
        stayIndex = position;
        isToStop = true;
    }
}

  最后是activity的实现。

 

 if (tigerPanel.getRunningStaus()){
            int stayIndex = new Random().nextInt(16);
            tigerPanel.tryToStop(stayIndex);
     }else{
            tigerPanel.startGame();
}

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值