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

相关文章

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机