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

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依