广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现系统级悬浮按钮
  • 496
分享到

Android实现系统级悬浮按钮

系统按钮Android 2022-06-06 04:06:49 496人浏览 独家记忆
摘要

本文实例为大家分享了Android系统级悬浮按钮的具体代码,供大家参考,具体内容如下 具体的需求 1、就是做一个系统级的悬浮按钮,就像iPhone 桌面的那个悬浮按钮效果一样

本文实例为大家分享了Android系统级悬浮按钮的具体代码,供大家参考,具体内容如下

具体的需求

1、就是做一个系统级的悬浮按钮,就像iPhone 桌面的那个悬浮按钮效果一样,能随意拖动,并且手一放开,悬浮按钮就自动靠边。
2、可以点击并且可以随意拖动。
3、悬浮按钮自动靠边的时候,或者移动到边上的时候,自动隐藏半边。
4、横竖屏切换都兼容

1、就在WindowManager 里面添加View,这个View通过自定义控件来实现。
2、在onTouch里的MotionEvent.ACTION_MOVE事件里头,通过控制悬浮按钮的具体坐标来实现随意移动。
3、在onTouch里的MotionEvent.ACTION_UP事件里头,来控制悬浮按钮自动靠边,并且自动隐藏半边,不过在这里onTouch和onClick这两个事件是一起触发的,不过这也有解决办法,你可以在手放开的瞬间,通过移动的距离,来决定是否触发点击事件,,如果返回false,就会触发点击事件,如果返回true就会触发点击事件
4、通过自定义控件onLayout方法,来捕获横竖屏切换事件,
5、还有一个靠哪边停靠的问题,通过坐标来判读更靠近哪一边。就靠哪边停靠。
![以中间这个中心点为准,以更短的X轴画一个正方形]

下面是具体实现代码:


import android.content.Context;
import android.graphics.canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import com.iapppay.openid.channel.LoginResultCallback;
import com.iapppay.openid.channel.OpenIDApplication;
import com.iapppay.openid.channel.util.DisplayUtil;
import com.iapppay.openid.channel.util.LogUtil;
import com.iapppay.openid.channel.util.Res;

public class DragFloatActionButton extends ImageView implements View.OnTouchListener, View.OnClickListener {
  public static String TAG = "DragFloatActionButton";
  private Context context;
  float lastX, lastY;
  float originX, originY;
  int screenWidth;
  int screenHeight;
  private int originWidth;
  private WindowManager windowManager;
  //  // 此windowManagerParams变量为获取的全局变量,用以保存悬浮窗口的属性
  private WindowManager.LayoutParams windowManagerParams;
  private LoginResultCallback resultCallback; //悬浮按钮点击回调
  public DragFloatActionButton(Context context, boolean isForceLogin, LoginResultCallback resultCallback) {
    this(context, null);
    OpenIDApplication.getInstance().setForceLogin(isForceLogin);
    this.resultCallback = resultCallback;
  }
  public DragFloatActionButton(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
  public DragFloatActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    Point screenSize = DisplayUtil.getScreenSize(context);
    screenWidth = screenSize.x;
    screenHeight = screenSize.y;
    setImageResource(Res.drawable(context, "ipay_float_btn_bg"));
    setOnTouchListener(this);
    setOnClickListener(this);
    windowManager = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
  }
  public int getOriginWidth() {
    return originWidth;
  }
  public void setOriginWidth(int originWidth) {
    this.originWidth = originWidth;
  }
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    windowManagerParams = (WindowManager.LayoutParams) this.getLayoutParams();
    //获取到状态栏的高度
    Rect frame = new Rect();
    getWindowVisibleDisplayFrame(frame);
    int ea = event.getAction();
    switch (ea) {
      case MotionEvent.ACTION_DOWN:
        lastX = event.getRawX();// 获取触摸事件触摸位置的原始X坐标
        lastY = event.getRawY();
        originX = lastX;
        originY = lastY;
        break;
      case MotionEvent.ACTION_MOVE:
        float dx = event.getRawX() - lastX;
        float dy = event.getRawY() - lastY;
        windowManagerParams.x += dx;
        windowManagerParams.y += dy;
        LogUtil.d(TAG, "移动距离:dx=" + dx + ",dy=" + dy);
        showAllBtn();
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
        break;
      case MotionEvent.ACTION_UP:
        float lastMoveDx = Math.abs(event.getRawX() - originX);
        float lastMoveDy = Math.abs(event.getRawY() - originY);
        LogUtil.d(TAG, "松开时,移动距离:lastMoveDx=" + lastMoveDx + ", lastMoveDy=" + lastMoveDy);
        if (lastMoveDx < 10 && lastMoveDy < 10) { //移动距离太小,视为点击,
          return false;
        } else {
          updateViewLayout(event);
          isFirstClick = true;
          return true;
        }
    }
    return false;
  }
  
  public void showAllBtn() {
    windowManagerParams.width = originWidth;
    windowManagerParams.height = originWidth;
    setImageResource(Res.drawable(context, "ipay_float_btn_bg"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新显示
  }
  
  private void showInLeft() {
    windowManagerParams.x = 0;
    windowManagerParams.width = originWidth / 2;
    windowManagerParams.height = originWidth;
    setImageResource(Res.drawable(context, "ipay_float_btn_left_hidden"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新显示
  }
  
  private void showInRight() {
    windowManagerParams.width = originWidth / 2;
    windowManagerParams.height = originWidth;
    windowManagerParams.x = screenWidth - windowManagerParams.width;
    setImageResource(Res.drawable(context, "ipay_float_btn_right_hidden"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新显示
  }
  
  private void showInTop() {
    windowManagerParams.y = 0;
    windowManagerParams.width = originWidth;
    windowManagerParams.height = originWidth / 2;
    setImageResource(Res.drawable(context, "ipay_float_btn_top_hidden"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新显示
  }
  
  private void showInBottom() {
    windowManagerParams.width = originWidth;
    windowManagerParams.height = originWidth / 2;
    windowManagerParams.y = screenHeight - windowManagerParams.width;
    setImageResource(Res.drawable(context, "ipay_float_btn_bottom_hidden"));
    windowManager.updateViewLayout(this, windowManagerParams); // 刷新显示
  }
  
  public void updateViewLayout(MotionEvent event) {
    Point center = new Point(screenWidth / 2, screenHeight / 2); //屏幕中心点
    float xOffset, yOffset;//以屏幕中心点为原点,X轴和Y轴上的偏移量
    if (event != null) {//手动移动的
      xOffset = event.getRawX() - center.x;
      yOffset = event.getRawY() - center.y;
    } else {//自动隐藏
      xOffset = lastX - center.x;
      yOffset = lastY - center.y;
    }
    if (Math.abs(xOffset) >= Math.abs(yOffset)) {//向左或向右缩进隐藏
      if (xOffset <= 0) { //向左缩进
        showInLeft();
      } else {
        showInRight();
      }
    } else {//向上或向下缩进隐藏
      if (yOffset <= 0) {//向上缩进
        showInTop();
      } else {
        showInBottom();
      }
    }
  }
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }
  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    Point screenSize = DisplayUtil.getScreenSize(context);
    if (screenWidth != screenSize.x) {//屏幕旋转切换
      screenWidth = screenSize.x;
      screenHeight = screenSize.y;
      lastY = windowManagerParams.x;
      lastX = windowManagerParams.y;
      windowManagerParams.x = (int) lastX;
      windowManagerParams.y = (int) lastY;
      updateViewLayout(null);
    }
  }
  private boolean isFirstClick = true;
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
  }
  @Override
  public void onClick(View v) {
    LogUtil.d(TAG, "执行点击事件");
    if (!isFirstClick) {
      OpenIDApplication.getInstance().floatBtnClick(context, OpenIDApplication.getInstance().isForceLogin(), resultCallback);
    } else {//半隐藏状态,点击显示全部
      isFirstClick = false;
      showAllBtn();
    }
  }
}

调用实现代码,这里注意有个问题,弹出系统级的悬浮窗,需要配置权限:

并且Android 6.0以上的手机,还要弹出对话框问用户是否运行,如果这个用户拒绝了,就不能弹出系统级的悬浮窗了,还有个别手机厂商修改了android源码,还需要进系统设置里去允许这个应用弹出悬浮窗。这样的话就体验感非常不好,不过这里有个小技巧,按下面方式设置为toast类型就完全解决,既不用配置权限,也不弹出窗来向用户获取权限,完全解决问题。


WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST, 
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
PixelFORMat.TRANSLUCENT);

具体实现代码如下:


DragFloatActionButton floatBtn = new DragFloatActionButton(context, isForceLogin, mResultCallback);
   WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
   // 设置LayoutParams(全局变量)相关参数
   WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST,
     WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
     PixelFormat.TRANSLUCENT);
   
   // 调整悬浮窗口至左上角,便于调整坐标
   windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
   // 以屏幕左上角为原点,设置x、y初始值
   windowManagerParams.x = 0;
   windowManagerParams.y = 0;
   // 设置悬浮窗口长宽数据
   floatBtn.measure(0, 0);
   floatBtn.setOriginWidth(floatBtn.getMeasuredWidth() - 50);
   windowManagerParams.width = floatBtn.getOriginWidth();
   windowManagerParams.height = windowManagerParams.width;
   // 显示myFloatView图像
   windowManager.addView(floatBtn, windowManagerParams);
您可能感兴趣的文章:Android自定义可拖拽的悬浮按钮DragFloatingActionButtonAndroid中FloatingActionButton实现悬浮按钮实例Android悬浮按钮点击返回顶部FloatingActionButtonAndroid开发模仿qq视频通话悬浮按钮(实例代码)Android利用WindowManager生成悬浮按钮及悬浮菜单Android开发中在TableView上添加悬浮按钮的方法Android开发悬浮按钮 Floating ActionButton的实现方法Android利用悬浮按钮实现翻页效果圣诞节,写个程序练练手————Android 全界面悬浮按钮实现Android悬浮按钮的使用方法


--结束END--

本文标题: Android实现系统级悬浮按钮

本文链接: https://www.lsjlt.com/news/22069.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android实现系统级悬浮按钮
    本文实例为大家分享了Android系统级悬浮按钮的具体代码,供大家参考,具体内容如下 具体的需求 1、就是做一个系统级的悬浮按钮,就像iPhone 桌面的那个悬浮按钮效果一样...
    99+
    2022-06-06
    系统 按钮 Android
  • iOS实现悬浮按钮
    前言 开发中会遇到有悬浮按钮功能。 效果 上代码 SuspensionButton.h #import <UIKit/UIKit.h> NS_ASSUME_NON...
    99+
    2022-06-04
    iOS 按钮
  • Android中FloatingActionButton实现悬浮按钮实例
    Android中FloatingActionButton(悬浮按钮) 使用不是特别多,常规性APP应用中很少使用该控件. 当然他的使用方法其实很简单.直接上代码:xml:<?xml version="1.0" encoding...
    99+
    2023-05-31
    android 悬浮按钮 roi
  • iOS实现全局悬浮按钮
    本文实例为大家分享了iOS实现全局悬浮按钮的具体代码,供大家参考,具体内容如下 现在有很多app都做这个全局按钮 如上面两张图的效果,完成一个全局悬浮的按钮,而且不会划出屏幕外既然...
    99+
    2022-11-13
  • Android利用悬浮按钮实现翻页效果
    今天给大家分享下自己用悬浮按钮点击实现翻页效果的例子。 首先,一个按钮要实现悬浮,就要用到系统顶级窗口相关的WindowManager,WindowManager.Layout...
    99+
    2022-06-06
    按钮 Android
  • 怎么在Android中实现一个悬浮按钮
    怎么在Android中实现一个悬浮按钮?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。具体实现代码:import android.content.Context;i...
    99+
    2023-05-31
    android roi %d
  • Android开发悬浮按钮 Floating ActionButton的实现方法
    一、介绍 这个类是继承自ImageView的,所以对于这个控件我们可以使用ImageView的所有属性 android.support.design.widget.Flo...
    99+
    2022-06-06
    方法 android开发 按钮 Android
  • iOS如何实现全局悬浮按钮
    这篇文章给大家分享的是有关iOS如何实现全局悬浮按钮的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。具体内容如下现在有很多app都做这个全局按钮如上面两张图的效果,完成一个全局悬浮的按钮,而且不会划出屏幕外既然是全...
    99+
    2023-06-29
  • 在Android开发中使用WindowManager实现一个悬浮按钮
    这篇文章将为大家详细讲解有关在Android开发中使用WindowManager实现一个悬浮按钮,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。效果:整体思路360手机卫士的内存球其实就是一个...
    99+
    2023-05-31
    android windowmanager age
  • vue实现移动端拖拽悬浮按钮
    目录功能介绍:大致需求:整体思路:具体实现:一、position:fixed布局:二、touch事件绑定:三、页面引入:本文实例为大家分享了vue实现移动端拖拽悬浮按钮的具体代码,供...
    99+
    2022-09-27
  • Android如何实现自定义可拖拽的悬浮按钮DragFloatingActionButton
    这篇文章主要介绍Android如何实现自定义可拖拽的悬浮按钮DragFloatingActionButton,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!悬浮按钮FloatingActionButton是Androi...
    99+
    2023-05-31
    android
  • 怎么在Android中利用FloatingActionButton实现一个悬浮按钮效果
    今天就跟大家聊聊有关怎么在Android中利用FloatingActionButton实现一个悬浮按钮效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。首先是这个最小的Tag:这个T...
    99+
    2023-05-31
    android floatingbutton roi
  • android 应用内部悬浮可拖动按钮简单实现代码
    本文介绍了android 应用内部悬浮可拖动按钮简单实现代码,分享给大家,具体如下:可以悬浮在activity上面,在加载fragment时悬浮按钮不会消失实现方式很简单,因为是在应用内部拖动的,只需要通过Activity获取WindowM...
    99+
    2023-05-30
    android 悬浮 拖动
  • jquery实现界面点击按钮弹出悬浮框
    本文实例为大家分享了jquery实现界面点击按钮弹出悬浮框的具体代码,供大家参考,具体内容如下 首先定义两个div: 一个是背景,一个是悬浮窗。 <input id="Butt...
    99+
    2022-11-13
  • vue如何实现移动端拖拽悬浮按钮
    这篇文章主要讲解了“vue如何实现移动端拖拽悬浮按钮”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue如何实现移动端拖拽悬浮按钮”吧!功能介绍:在移动端开发中,实现悬浮按钮在侧边显示,为不...
    99+
    2023-07-02
  • Kotlin实现Android系统悬浮窗详解
    目录Android 弹窗浅谈系统悬浮窗具体实现权限申请代码设计具体实现FloatWindowService 类FloatWindowManager 类FloatWindowManag...
    99+
    2022-11-12
  • Kotlin如何实现Android系统悬浮窗
    本篇内容介绍了“Kotlin如何实现Android系统悬浮窗”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Android 弹窗浅谈我们知道 ...
    99+
    2023-06-22
  • CSS3按钮鼠标悬浮怎么实现光圈效果
    这篇文章主要介绍了CSS3按钮鼠标悬浮怎么实现光圈效果的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇CSS3按钮鼠标悬浮怎么实现光圈效果文章都会有所收获,下面我们一起来看看吧。1 、HTML相关知识点 &nbs...
    99+
    2023-07-04
  • 圣诞节,写个程序练练手————Android 全界面悬浮按钮实现
    开始我以为悬浮窗口,可以用Android中得PopupWindow 来实现,虽然也实现了,但局限性非常大。比如PopupWindow必须要有载体View,也就是说,必须要指定在...
    99+
    2022-06-06
    圣诞节 程序 界面 按钮 Android
  • Android开发模仿qq视频通话悬浮按钮(实例代码)
    模仿qq视频通话的悬浮按钮的实例代码,如下所示; public class FloatingWindowService extends Service{ private ...
    99+
    2022-06-06
    android开发 按钮 Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作