自定义 浮动button 自动靠边 附加收缩功能

2024-08-31 09:38

本文主要是介绍自定义 浮动button 自动靠边 附加收缩功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

悬浮按钮可全屏滑动,左吸附、右吸附,外加右吸附后2秒后收缩功能,收缩完点击弹出 ,先看下效果,右吸附和收缩:

左吸附部分注释掉了,可根据实际情况修改使用 

package com.example.test1.customView;import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;import androidx.annotation.NonNull;import com.example.test1.utils.LoggUtils;/*** Created by WJY.* Date: 2021/3/10* Time: 15:10* Description: 自定义 浮动button 自动靠边*/
@SuppressLint("AppCompatCustomView")
public class DragFloatActionButton extends Button {private int parentHeight;private int parentWidth;private int lastX;private int lastY;private boolean isDrag;Handler mHndler = new Handler(){@Overridepublic void handleMessage(@NonNull Message msg) {super.handleMessage(msg);switch (msg.what){case 0:animate().setInterpolator(new DecelerateInterpolator()).setDuration(200).xBy((float) (parentWidth - getWidth() - getX() + (0.7*getWidth()))).start();break;}}};public DragFloatActionButton(Context context) {super(context);}public DragFloatActionButton(Context context, AttributeSet attrs) {super(context, attrs);}public DragFloatActionButton(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean onTouchEvent(MotionEvent event) {int rawX = (int) event.getRawX();int rawY = (int) event.getRawY();switch (event.getAction() & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:setPressed(true);isDrag = false;getParent().requestDisallowInterceptTouchEvent(true);lastX = rawX;lastY = rawY;ViewGroup parent;if (getParent() != null) {parent = (ViewGroup) getParent();parentHeight = parent.getHeight();parentWidth = parent.getWidth();}break;case MotionEvent.ACTION_MOVE:if (parentHeight <= 0 || parentWidth == 0) {isDrag = false;break;} else {isDrag = true;}int dx = rawX - lastX;int dy = rawY - lastY;//这里修复一些华为手机无法触发点击事件int distance = (int) Math.sqrt(dx * dx + dy * dy);if (distance == 0) {isDrag = false;break;}float x = getX() + dx;float y = getY() + dy;//检测是否到达边缘 左上右下x = x < 0 ? 0 : x > parentWidth - getWidth() ? parentWidth - getWidth() : x;y = getY() < 0 ? 0 : getY() + getHeight() > parentHeight ? parentHeight - getHeight() : y;setX(x);setY(y);lastX = rawX;lastY = rawY;Log.e("Log", "isDrag=" + isDrag + "getX=" + getX() + ";getY=" + getY() + ";parentWidth=" + parentWidth);break;case MotionEvent.ACTION_UP:if (!isNotDrag()) {//恢复按压效果setPressed(false);//靠右吸附animate().setInterpolator(new DecelerateInterpolator()).setDuration(200).xBy(parentWidth - getWidth() - getX()).start();LoggUtils.e("靠右吸附","getX()="+getX()+"---getWidth()="+getWidth());new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(2000);mHndler.sendEmptyMessage(0);} catch (InterruptedException e) {e.printStackTrace();}}}).start();//下面注释掉的是靠左和靠右吸附两种情况,可根据实际使用修改
//                    if (rawX >= parentWidth / 2) {
//                        //靠右吸附
//                        animate().setInterpolator(new DecelerateInterpolator())
//                                .setDuration(200)
//                                .xBy(parentWidth - getWidth() - getX())
//                                .start();
//                        LoggUtils.e("靠右吸附","getX()="+getX()+"---getWidth()="+getWidth());
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                try {
//                                    Thread.sleep(2000);
//                                    mHndler.sendEmptyMessage(0);
//                                } catch (InterruptedException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }).start();
//                    } else {
//                        //靠左吸附
//                        ObjectAnimator oa = ObjectAnimator.ofFloat(this, "x", getX(), 0);
//                        oa.setInterpolator(new DecelerateInterpolator());
//                        oa.setDuration(200);
//                        oa.start();
//                        LoggUtils.e("靠左吸附","getX()="+getX()+"---getWidth()="+getWidth());
//                    }}break;}//如果是拖拽则消耗事件,否则正常传递即可。return !isNotDrag() || super.onTouchEvent(event);}private boolean isNotDrag() {return !isDrag && (getX() == 0 || (getX() == parentWidth - getWidth()));}
}

在布局中使用,就相当与一个控件

<com.example.test1.customView.DragFloatActionButtonandroid:id="@+id/floatBtn"android:layout_width="@dimen/dimen_81dp"android:layout_height="@dimen/dimen_40dp"android:background="@drawable/bg_blue_leftcorner"android:drawableLeft="@mipmap/icon_left"android:paddingLeft="@dimen/dimen_14dp"android:paddingRight="@dimen/dimen_14dp"android:text="吸附"android:textColor="@color/white"android:textSize="14sp"android:layout_gravity="bottom|right"android:visibility="visible"/>

其中用到的一个button背景是自定义的一个文件bg_blue_leftcorner.xml,也可根据实际需求来写

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="@color/blue_10" /><corners android:bottomLeftRadius="@dimen/dimen_20dp"android:topLeftRadius="@dimen/dimen_20dp"/></shape>

到此,一个可吸附的悬浮按钮就完成了,不足之处欢迎指正。

这篇关于自定义 浮动button 自动靠边 附加收缩功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Qt实现对Word网页的读取功能

《Qt实现对Word网页的读取功能》文章介绍了几种在Qt中实现Word文档(.docx/.doc)读写功能的方法,包括基于QAxObject的COM接口调用、DOCX模板替换及跨平台解决方案,重点讨论... 目录1. 核心实现方式2. 基于QAxObject的COM接口调用(Windows专用)2.1 环境

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

SpringBoot+Vue3整合SSE实现实时消息推送功能

《SpringBoot+Vue3整合SSE实现实时消息推送功能》在日常开发中,我们经常需要实现实时消息推送的功能,这篇文章将基于SpringBoot和Vue3来简单实现一个入门级的例子,下面小编就和大... 目录前言先大概介绍下SSE后端实现(SpringBoot)前端实现(vue3)1. 数据类型定义2.

SpringBoot整合Apache Spark实现一个简单的数据分析功能

《SpringBoot整合ApacheSpark实现一个简单的数据分析功能》ApacheSpark是一个开源的大数据处理框架,它提供了丰富的功能和API,用于分布式数据处理、数据分析和机器学习等任务... 目录第一步、添加android依赖第二步、编写配置类第三步、编写控制类启动项目并测试总结ApacheS

Python实现繁体转简体功能的三种方案

《Python实现繁体转简体功能的三种方案》在中文信息处理中,繁体字与简体字的转换是一个常见需求,无论是处理港澳台地区的文本数据,还是开发面向不同中文用户群体的应用,繁简转换都是不可或缺的功能,本文将... 目录前言为什么需要繁简转换?python实现方案方案一:使用opencc库方案二:使用zhconv库

Apache服务器IP自动跳转域名的问题及解决方案

《Apache服务器IP自动跳转域名的问题及解决方案》本教程将详细介绍如何通过Apache虚拟主机配置实现这一功能,并解决常见问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录​​问题背景​​解决方案​​方法 1:修改 httpd-vhosts.conf(推荐)​​步骤

idea-java序列化serialversionUID自动生成方式

《idea-java序列化serialversionUID自动生成方式》Java的Serializable接口用于实现对象的序列化和反序列化,通过将对象转换为字节流来存储或传输,实现Serializa... 目录简介实现序列化serialVersionUID配置使用总结简介Java.io.Seripyth

kafka自定义分区器使用详解

《kafka自定义分区器使用详解》本文介绍了如何根据企业需求自定义Kafka分区器,只需实现Partitioner接口并重写partition()方法,示例中,包含cuihaida的数据发送到0号分区... 目录kafka自定义分区器假设现在有一个需求使用分区器的方法总结kafka自定义分区器根据企业需求

Qt实现删除布局与布局切换功能

《Qt实现删除布局与布局切换功能》在Qt应用开发中,动态管理布局是一个常见需求,比如根据用户操作动态删除某个布局,或在不同布局间进行切换,本文将详细介绍如何实现这些功能,并通过完整示例展示具体操作,需... 目录一、Qt动态删除布局1. 布局删除的注意事项2. 动态删除布局的实现步骤示例:删除vboxLay

Spring Boot整合Redis注解实现增删改查功能(Redis注解使用)

《SpringBoot整合Redis注解实现增删改查功能(Redis注解使用)》文章介绍了如何使用SpringBoot整合Redis注解实现增删改查功能,包括配置、实体类、Repository、Se... 目录配置Redis连接定义实体类创建Repository接口增删改查操作示例插入数据查询数据删除数据更