高仿苹果虚拟home键,手势拖动,放手会弹到屏幕边缘。
来源:互联网 发布:域名反解析 编辑:程序博客网 时间:2024/05/01 21:05
在最近的项目开发过程中,产品提出了一个需求,就是要求在屏幕上面有一个悬浮按钮,然后手势可以拖动,在手抬起的时候会回到屏幕边缘,类似于苹果的虚拟home键。
不多说了,直接上代码了,代码里面会有注释,可以解释清楚:整个Demo只有一个类,继承FloatingActionButton:
import android.animation.ObjectAnimator;import android.content.Context;import android.support.design.widget.FloatingActionButton;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.animation.DecelerateInterpolator;import com.rhzt.xgj.util.DisplayUtil;/** * Created by lixuce on 2017/5/8. */public class DragFloatActionButton extends FloatingActionButton { private int screenWidth; private int screenHeight; private int screenWidthHalf; private int statusHeight; public DragFloatActionButton(Context context) { super(context); init(); } public DragFloatActionButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public DragFloatActionButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init(){ //获取屏幕宽 screenWidth= DisplayUtil.getScreenWidth(getContext()); screenWidthHalf=screenWidth/2; //屏幕的高 screenHeight=DisplayUtil.getScreenHight(getContext()); //状态栏的高度 // 如果状态栏可以拖动 就获取状态栏的高度 目前设置成0 statusHeight=0; } private int lastX; private int lastY; private boolean isDrag; @Override public boolean onTouchEvent(MotionEvent event) { int rawX = (int) event.getRawX(); int rawY = (int) event.getRawY(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: isDrag=false; getParent().requestDisallowInterceptTouchEvent(true); lastX=rawX; lastY=rawY; break; case MotionEvent.ACTION_MOVE: 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>screenWidth-getWidth()?screenWidth-getWidth():x; y=y<statusHeight?statusHeight:y+getHeight()>screenHeight?screenHeight-getHeight():y; setX(x); setY(y); lastX=rawX; lastY=rawY; //Log.i("getX="+getX()+";getY="+getY()+";screenHeight="+screenHeight); break; case MotionEvent.ACTION_UP: if(isDrag){ //恢复按压效果 setPressed(false);// Log.i("getX="+getX()+";screenWidthHalf="+screenWidthHalf); if(rawX>=screenWidthHalf){ animate().setInterpolator(new DecelerateInterpolator()) .setDuration(500) .xBy(screenWidth-getWidth()-getX()) .start(); }else { ObjectAnimator oa=ObjectAnimator.ofFloat(this,"x",getX(),0); oa.setInterpolator(new DecelerateInterpolator()); oa.setDuration(500); oa.start(); } } break; } //如果是拖拽则消耗事件,否则正常传递即可。你 return isDrag || super.onTouchEvent(event); }}
很简单就可以实现效果了,代码是应对我的需求来写的,你也可以自己来写,只是一个思路,希望可以帮到你。
0 0
- 高仿苹果虚拟home键,手势拖动,放手会弹到屏幕边缘。
- 高仿苹果通讯录布局
- 高仿支付宝手势解锁
- 高仿支付宝手势解锁
- android 屏幕home键
- 高仿苹果7 plus 3592H
- win10实现屏幕边缘热区切换虚拟桌面
- 拖动手势
- iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法
- iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法
- 高仿launcher和墨迹左右拖动效果
- 使用ViewPager实现高仿launcher拖动效果
- 高仿launcher和墨迹左右拖动效果
- 高仿launcher和墨迹左右拖动效果
- 使用ViewPager实现高仿Launcher的拖动效果
- 使用ViewPager实现高仿launcher拖动效果
- 使用ViewPager实现高仿launcher拖动效果
- 高仿小米launcher(ZAKER)跨屏拖动item
- Codeforces Round #412 D. Dynamic Problem Scoring (枚举 + 贪心)
- 教你一招让你高效搞定高品质的H5交互动画
- PHP随写笔记,类和对象
- 基础Android之访问网络
- 番茄工作法简介
- 高仿苹果虚拟home键,手势拖动,放手会弹到屏幕边缘。
- Hibernate第十篇【Hibernate查询详解、分页查询】
- PHP:基于百度大脑api实现OCR文字识别
- 浏览器f12, ::before ::after是什么意思?
- VMware 安装kali——linux
- 运行时(runtime)-方法交换
- USB/File system相关内核配置
- GObject消息系统之Closures
- angularJs页面弹框的实现