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

先上效果:

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

1:滚轮型

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

public void onStartClicked(){if (isStart){binding.btn.setText("开始");isStart=false;new Thread(new Runnable() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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布局如下:

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"><RelativeLayoutandroid:id="@+id/item_bg"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:layout_width="50dp"android:layout_height="50dp"android:id="@+id/item_img"android:layout_centerInParent="true"/><Viewandroid: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;}}@Overridepublic 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() {@Overridepublic void run() {while (isRunning) {try {Thread.sleep(getInterruptTime());} catch (InterruptedException e) {e.printStackTrace();}post(new Runnable() {@Overridepublic 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();
}

android 老虎机实现相关推荐

  1. Android老虎机

    效果图如下: GitHub下载地址:https://github.com/wuqingsen/TigerWu csdn 下载地址:https://download.csdn.net/download/ ...

  2. Android RecyclerView实现类似于老虎机抽奖,数字滚动等动画效果

    1.RecyclerViewLoopScrollAnimation项目介绍 RecyclerViewLoopScrollAnimation 适用于Android RecyclerView的循环滚动动画 ...

  3. Android简易老虎机(转动式)

    说起老虎机,玩过的人应该记得这种类型的图(这是主界面),下面就从最基本的知识分析整个APP的制作过程,也同时把android中的一些基础知识巩固一遍 首先说一下这个APP的基本操作: 1.点击下注按钮 ...

  4. Android 自定义控件之——画个老虎机来玩玩

    小时候经常去镇上的游戏厅玩游戏,里面有很多赌游戏币的老虎机,当初也玩过,被坑了好多游戏币.现在有空,自己也画一个来玩玩.巩固一下自己在自定义控件 方面的知识,好了,不多废话了.效果图先走一波. 图中的 ...

  5. android 水果老虎机 文档一

    运用andrid最基础的sufaceView制作小时候的最爱,水果老虎机过程文档 先来张初始效果图,图片全部来在网络扣图,那个累呀......... 整个图型的画法,和转灯的画法 // 自定义画法pr ...

  6. Android Studio 选项菜单和动画结合_Android 应用与iOS 应用之间的设计差异对比!

    同一个App,为什么iOS 和Android 的交互操作有那么大的区别?本文将用大量原生设计案例,为你一一说明它们为什么应该这样做,赶紧学起来! 了解并适当结合平台规范与优势,才能做到最佳的用户体验. ...

  7. 10个最佳Android游戏模板

    介绍 如果您熟悉Android SDK,则可能知道使用它从头开始创建新游戏绝非易事. 选择正确的游戏引擎,支持多种屏幕尺寸和DPI,实现无错误的游戏玩法以及管理玩家数据都是一项艰巨的任务,这些任务可能 ...

  8. 干货!iOS 与 Android 的APP 设计差异

    了解并适当结合平台规范与优势,才能做到最佳的用户体验.在<最新Android & iOS设计尺寸规范>一文中介绍 APP 设计规范(https://ds.mockplus.cn), ...

  9. 安卓实现老虎机抽奖的案例

    最近看到某理财app上有一个类似老虎机的抽奖功能,觉得蛮好玩就想自己动手去实现下.就做了一个简单的案例,效果如下: 我说下思路吧,先准备两组图片,红色背景一组,黄色背景一组,如图    // 未开始抽 ...

最新文章

  1. Windows7上配置Python Protobuf 操作步骤
  2. swift_027(Swift 的扩展)
  3. Java LocalDate类| 带示例的compareTo()方法
  4. 罗技 连点 脚本_罗技推出多款《英雄联盟》联名外设 看了就忍不住想要
  5. c++ 将引用赋值给引用_5分钟掌握 Python 对象的引用
  6. libspark.swfassist的体会
  7. js简单操作Cookie
  8. Axure安装、破解、汉化一条龙
  9. 阿里云周明:因云而生的基础设施
  10. 百度+京东+美团Java面经合集
  11. Java如何发起http请求
  12. OpenCV边缘检测算法
  13. recover 没有捕获异常_Go的异常处理defer, panic, recover以及错误处理
  14. Latex多处引用同一脚注
  15. java xmemcached incr_XMemcached的基本使用
  16. 题解 【NOIP2016】魔法阵
  17. 西门子三开接线图解_接近开关三线制接线方法
  18. android平台开发板外接罗技C525摄像头不支持扫码有什么办法解决
  19. 什么是线程线程和进程的区别
  20. 2020年第一季度国外的垃圾邮件和网络钓鱼活动回顾

热门文章

  1. Rust入坑指南:千人千构
  2. Axure| Axure如何画线
  3. 2022-1-25 牛客C++项目 —— SIGCHID 信号
  4. 使用ant-design-vue实现换肤功能
  5. 交通方案 | 基于FET3399-C核心板打造公交二维码支付刷卡机
  6. creo自定义调用零件库_cero基础设置教程,五步cero5.0设置调用自定义标准件的方法...
  7. php 字节码查看,PHP-7.1 源代码学习:字节码在 Zend 虚拟机中的解释执行 之 概述...
  8. vs 选定内容没有属性页_【产品分析】从搜索功能看产品定位:抖音VS快手
  9. mysql 记录执行的sql_MySQL监控全部执行过的sql语句
  10. 亚马逊无货源店群ERP系统 无限开代理 OEM贴牌