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

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义