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

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景