Android自定义View-九宫格抽奖转盘(两种实现)

2023-12-12 18:30

本文主要是介绍Android自定义View-九宫格抽奖转盘(两种实现),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

好久没写文章了,一来感觉自己技术没啥进步,二来各种杂事繁忙,以至于拖了许久。正好这个版本产品需求需要做一个九宫格样式的转盘抽奖机,感觉是个挺有意思的东西,把我的解决方案和中间遇到的问题发出来,供大家参考哈~

两种方案,先看成品的效果
在这里插入图片描述

开始做这个功能的时候,跟产品确定效果,要求动画是先慢后快再变慢,我第一时间想到的就是插值器。AccelerateDecelerateInterpolator就是属于开始和结束很慢,中间速度较快的那种插值器,完美符合需求。接下来需要考虑怎么来实现九宫格。

第一种实现

我第一版的实现是使用ConstraintLayout,根据约束条件来进行九宫格的布局,这样的缺点是布局文件难看,过多的include,实现起来倒是不怎么复杂。先来看下方案的代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><includeandroid:id="@+id/include_lottery0"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent" /><includeandroid:id="@+id/include_lottery1"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintLeft_toRightOf="@+id/include_lottery0"app:layout_constraintRight_toLeftOf="@+id/include_lottery2"app:layout_constraintTop_toTopOf="parent" /><includeandroid:id="@+id/include_lottery2"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><includeandroid:id="@+id/include_lottery7"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintBottom_toTopOf="@+id/include_lottery5"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toBottomOf="@+id/include_lottery1" /><includeandroid:id="@+id/include_button"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintBottom_toTopOf="@+id/include_lottery5"app:layout_constraintLeft_toRightOf="@+id/include_lottery7"app:layout_constraintRight_toLeftOf="@+id/include_lottery3"app:layout_constraintTop_toBottomOf="@+id/include_lottery1" /><includeandroid:id="@+id/include_lottery3"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintBottom_toTopOf="@+id/include_lottery5"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toBottomOf="@+id/include_lottery1" /><includeandroid:id="@+id/include_lottery6"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent" /><includeandroid:id="@+id/include_lottery5"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toRightOf="@+id/include_lottery6"app:layout_constraintRight_toLeftOf="@+id/include_lottery4" /><includeandroid:id="@+id/include_lottery4"layout="@layout/layout_lottery_item"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="1"app:layout_constraintWidth_percent="0.33"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toRightOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

罗列了9个include,并对每个布局进行了单独的约束,实现的样式如下:

在这里插入图片描述

接下来,自定义一个View,继承ConstraintLayout,在里面进行逻辑代码的编写,核心点有两个,第一个需要正确获取到肉眼所见的0~8这几个按钮的位置,这里我直接使用数组来标记这8个View,按照顺序排列好

init {inflate(context, R.layout.layout_lucky_draw, this)lotteryArray = arrayOf(findViewById(R.id.include_lottery0),findViewById(R.id.include_lottery1),findViewById(R.id.include_lottery2),findViewById(R.id.include_lottery3),findViewById(R.id.include_lottery4),findViewById(R.id.include_lottery5),findViewById(R.id.include_lottery6),findViewById(R.id.include_lottery7))...
}

到这一步,布局的问题就已经解决了,下面要解决动画执行和确定抽奖位置的问题。动画执行很容易可以想到使用属性动画,因为是循环转动,我们可以设置一个从0到指定位置x的范围,其中x的值对8取余就是我们事先确定的中奖的位置,代码如下:

init {animator.duration = 2000animator.interpolator = AccelerateDecelerateInterpolator()animator.addUpdateListener {val position = it.animatedValue as IntsetCurrentPosition(position % 8)}animator.addListener(object : AnimatorListenerAdapter() {override fun onAnimationEnd(animation: Animator?) {super.onAnimationEnd(animation)setCurrentPosition(luckyIndex)lotteryStatus = 2}})animator.setIntValues(0, 2 * 8 + luckyIndex)
}

这里可以看到动画执行了两圈之后停止在指定的luckyIndex这个位置,其中setCurrentposition用来对指定位置进行刷新,我这里偷了个懒,没有记录之前的位置,整体刷新了,后续可以优化下

第二种实现

后面的实现是感觉上一种方法布局写的太累,于是想到了使用RecyclerView来进行布局,算是对版本1的一个扩展。RecyclerView实现很简单,设置GridLayoutManager并设置spanCount为3即可,代码就不再贴了,有兴趣可以去看代码。这种方案跟版本1大同小异,唯一需要注意的问题在于刷新的时候因为我们肉眼看到的顺序其实对应的不是RecyclerView中item的位置,所以如果刷新不是使用notifyDataSetChanged的话,需要注意这里的刷新位置,简略看下代码实现

var posMap =
mapOf<Int, Int>(0 to 0, 1 to 1, 2 to 2, 3 to 7, 4 to 8, 5 to 3, 6 to 6, 7 to 5, 8 to 4)
fun setSelectionPosition(selectPos: Int) {val lastPos = selectPositionselectPosition = selectPosif (lastPos != -1) {notifyItemChanged(reversePosition(lastPos))} else {notifyDataSetChanged()}notifyItemChanged(reversePosition(selectPos))
}/**
* 获取真实坐标
*/
private fun reversePosition(selectPos: Int): Int {for ((key, value) in posMap.entries) {if (value == selectPos) {return key}}return -1
}

其他的实现就跟上面的大同小异了

ok,这就是一个简单的九宫格抽奖转盘的实现了,算是水了一篇,需要源码的请移步代码地址

这篇关于Android自定义View-九宫格抽奖转盘(两种实现)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/485552

相关文章

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

通过SSH隧道实现通过远程服务器上外网

搭建隧道 autossh -M 0 -f -D 1080 -C -N user1@remotehost##验证隧道是否生效,查看1080端口是否启动netstat -tuln | grep 1080## 测试ssh 隧道是否生效curl -x socks5h://127.0.0.1:1080 -I http://www.github.com 将autossh 设置为服务,隧道开机启动

ROS话题通信流程自定义数据格式

ROS话题通信流程自定义数据格式 需求流程实现步骤定义msg文件编辑配置文件编译 在 ROS 通信协议中,数据载体是一个较为重要组成部分,ROS 中通过 std_msgs 封装了一些原生的数据类型,比如:String、Int32、Int64、Char、Bool、Empty… 但是,这些数据一般只包含一个 data 字段,结构的单一意味着功能上的局限性,当传输一些复杂的数据,比如:

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测 目录 时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测基本介绍程序设计参考资料 基本介绍 MATLAB实现LSTM时间序列未来多步预测-递归预测。LSTM是一种含有LSTM区块(blocks)或其他的一种类神经网络,文献或其他资料中LSTM区块可能被描述成智能网络单元,因为

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

Eclipse+ADT与Android Studio开发的区别

下文的EA指Eclipse+ADT,AS就是指Android Studio。 就编写界面布局来说AS可以边开发边预览(所见即所得,以及多个屏幕预览),这个优势比较大。AS运行时占的内存比EA的要小。AS创建项目时要创建gradle项目框架,so,创建项目时AS比较慢。android studio基于gradle构建项目,你无法同时集中管理和维护多个项目的源码,而eclipse ADT可以同时打开

android 免费短信验证功能

没有太复杂的使用的话,功能实现比较简单粗暴。 在www.mob.com网站中可以申请使用免费短信验证功能。 步骤: 1.注册登录。 2.选择“短信验证码SDK” 3.下载对应的sdk包,我这是选studio的。 4.从头像那进入后台并创建短信验证应用,获取到key跟secret 5.根据技术文档操作(initSDK方法写在setContentView上面) 6.关键:在有用到的Mo

android一键分享功能部分实现

为什么叫做部分实现呢,其实是我只实现一部分的分享。如新浪微博,那还有没去实现的是微信分享。还有一部分奇怪的问题:我QQ分享跟QQ空间的分享功能,我都没配置key那些都是原本集成就有的key也可以实现分享,谁清楚的麻烦详解下。 实现分享功能我们可以去www.mob.com这个网站集成。免费的,而且还有短信验证功能。等这分享研究完后就研究下短信验证功能。 开始实现步骤(新浪分享,以下是本人自己实现

Android我的二维码扫描功能发展史(完整)

最近在研究下二维码扫描功能,跟据从网上查阅的资料到自己勉强已实现扫描功能来一一介绍我的二维码扫描功能实现的发展历程: 首页通过网络搜索发现做android二维码扫描功能看去都是基于google的ZXing项目开发。 2、搜索怎么使用ZXing实现自己的二维码扫描:从网上下载ZXing-2.2.zip以及core-2.2-source.jar文件,分别解压两个文件。然后把.jar解压出来的整个c

android 带与不带logo的二维码生成

该代码基于ZXing项目,这个网上能下载得到。 定义的控件以及属性: public static final int SCAN_CODE = 1;private ImageView iv;private EditText et;private Button qr_btn,add_logo;private Bitmap logo,bitmap,bmp; //logo图标private st