Android实现全局悬浮框

 更新时间:2021年01月26日 11:54:05   作者:tracydragonlxy  
这篇文章主要为大家详细介绍了Android实现全局悬浮框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现全局悬浮框的具体代码,供大家参考,具体内容如下

效果图:

代码实现:

Androidmanifest.xml添加弹框权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

自定义悬浮窗类FloatWindow.java

public class FloatWindow implements View.OnTouchListener {

 private Context mContext;
 private WindowManager.LayoutParams mWindowParams;
 private WindowManager mWindowManager;

 private View mFloatLayout;
 private float mInViewX;
 private float mInViewY;
 private float mDownInScreenX;
 private float mDownInScreenY;
 private float mInScreenX;
 private float mInScreenY;
 private TextView infoText;

 public FloatWindow(Context context) {
  this.mContext = context;
  initFloatWindow();
 }

 private void initFloatWindow() {
  LayoutInflater inflater = LayoutInflater.from(mContext);
  if(inflater == null)
   return;
  mFloatLayout = (View) inflater.inflate(R.layout.layout_float, null);
  infoText = mFloatLayout.findViewById(R.id.textView);
  mFloatLayout.setOnTouchListener(this);

  mWindowParams = new WindowManager.LayoutParams();
  mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  if (Build.VERSION.SDK_INT >= 26) {//8.0新特性
   mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
  }else{
   mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  }
  mWindowParams.format = PixelFormat.RGBA_8888;
  mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  mWindowParams.gravity = Gravity.START | Gravity.TOP;
  mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
  mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
 }

 @Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
  return floatLayoutTouch(motionEvent);
 }

 private boolean floatLayoutTouch(MotionEvent motionEvent) {
  switch (motionEvent.getAction()) {
   case MotionEvent.ACTION_DOWN:
    // 获取相对View的坐标,即以此View左上角为原点
    mInViewX = motionEvent.getX();
    mInViewY = motionEvent.getY();
    // 获取相对屏幕的坐标,即以屏幕左上角为原点
    mDownInScreenX = motionEvent.getRawX();
    mDownInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
    mInScreenX = motionEvent.getRawX();
    mInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
    break;
   case MotionEvent.ACTION_MOVE:
    // 更新浮动窗口位置参数
    mInScreenX = motionEvent.getRawX();
    mInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
    mWindowParams.x = (int) (mInScreenX- mInViewX);
    mWindowParams.y = (int) (mInScreenY - mInViewY);
    // 手指移动的时候更新小悬浮窗的位置
    mWindowManager.updateViewLayout(mFloatLayout, mWindowParams);
    break;
   case MotionEvent.ACTION_UP:
    // 如果手指离开屏幕时,xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,则视为触发了单击事件。
    if (mDownInScreenX == mInScreenX && mDownInScreenY == mInScreenY){

    }
    break;
  }
  return true;
 }

 public void showFloatWindow(){
  if (mFloatLayout.getParent() == null){
   DisplayMetrics metrics = new DisplayMetrics();
   // 默认固定位置,靠屏幕右边缘的中间
   mWindowManager.getDefaultDisplay().getMetrics(metrics);
   mWindowParams.x = metrics.widthPixels;
   mWindowParams.y = metrics.heightPixels/2 - getSysBarHeight(mContext);
   mWindowManager.addView(mFloatLayout, mWindowParams);
  }
 }

 public void updateText(final String s) {
  infoText.setText(s);
 }

 public void hideFloatWindow(){
  if (mFloatLayout.getParent() != null)
   mWindowManager.removeView(mFloatLayout);
 }

 public void setFloatLayoutAlpha(boolean alpha){
  if (alpha)
   mFloatLayout.setAlpha((float) 0.5);
  else
   mFloatLayout.setAlpha(1);
 }

 // 获取系统状态栏高度
 public static int getSysBarHeight(Context contex) {
  Class<?> c;
  Object obj;
  Field field;
  int x;
  int sbar = 0;
  try {
   c = Class.forName("com.android.internal.R$dimen");
   obj = c.newInstance();
   field = c.getField("status_bar_height");
   x = Integer.parseInt(field.get(obj).toString());
   sbar = contex.getResources().getDimensionPixelSize(x);
  } catch (Exception e1) {
   e1.printStackTrace();
  }
  return sbar;
 }
}

自定义悬浮窗界面布局文件layout_float.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto">

 <ImageView
  android:id="@+id/imageView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@mipmap/float_win"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent"/>

 <TextView
  android:id="@+id/textView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#00ffffff"
  android:text="hello"
  android:textSize="12sp"
  app:layout_constraintLeft_toLeftOf="@id/imageView"
  app:layout_constraintRight_toRightOf="@id/imageView"
  app:layout_constraintTop_toBottomOf="@id/imageView"/>

</android.support.constraint.ConstraintLayout>

在Activity中使用悬浮窗。

public class MainActivity extends AppCompatActivity {

 private Button btnShow;
 FloatWindow floatWindow;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // 权限判断
  if (Build.VERSION.SDK_INT >= 23) {
   if(!Settings.canDrawOverlays(getApplicationContext())) {
    // 启动Activity让用户授权
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent,10);
   } else {
    // 执行6.0以上绘制代码
    initView();
   }
  } else {
   // 执行6.0以下绘制代码
   initView();
  }
 }

 @Override
 protected void onResume() {
  super.onResume();
  // 权限判断
  if (Build.VERSION.SDK_INT >= 23) {
   if(Settings.canDrawOverlays(getApplicationContext())) {
    initView();
   }
  } else {
   //执行6.0以下绘制代码
   initView();
  }
 }

 private void initView() {
  setContentView(R.layout.activity_main);
  floatWindow = new FloatWindow(getApplicationContext());

  btnShow = findViewById(R.id.btn_show);
  btnShow.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    if (null != floatWindow) {
     floatWindow.showFloatWindow();
    }
   }
  });

  Button btnrefresh = findViewById(R.id.btn_refresh);
  btnrefresh.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    int random = (int) (Math.random() * 10);
    if (null != floatWindow) {
     floatWindow.updateText(String.valueOf(random));
    }
   }
  });
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  if (null != floatWindow) {
   floatWindow.hideFloatWindow();
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • Android Studio 中aidl的自定义类的使用详解

    Android Studio 中aidl的自定义类的使用详解

    这篇文章主要介绍了Android Studio 中aidl的自定义类的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android如何获取双卡手机IMEI的方法示例

    Android如何获取双卡手机IMEI的方法示例

    这篇文章主要介绍了Android如何获取双卡手机IMEI的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • Android如何实现一个DocumentProvider示例详解

    Android如何实现一个DocumentProvider示例详解

    这篇文章主要为大家介绍了Android如何实现一个DocumentProvider示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • 一文带你了解Flutter数据表格的使用

    一文带你了解Flutter数据表格的使用

    目前,越来越多的管理层(所谓的领导)都希望在手机端查看各种各样的数据报表,以达到随时随地关注经营业绩(监督干活)的目的。本篇我们就来介绍 Flutter 的数据表格的使用,希望对大家有所帮助
    2022-11-11
  • Flutter搞定宽高不统一布局开发的方法详解

    Flutter搞定宽高不统一布局开发的方法详解

    我们在开发移动端界面的时候,经常会遇到一组尺寸不一的组件需要作为同一组展示,所以本文就将利用Wrap组件搞定宽高不统一布局开发,需要的可以参考一下
    2023-06-06
  • Android基础之Activity生命周期

    Android基础之Activity生命周期

    activity类是Android 应用生命周期的重要部分。在系统中的Activity被一个Activity栈所管理。当一个新的Activity启动时,将被放置到栈顶,成为运行中的Activity,前一个Activity保留在栈中,不再放到前台,直到新的Activity退出为止。
    2016-05-05
  • Android仿银行客户签名并且保存签名的截图文件并命名为本地时间

    Android仿银行客户签名并且保存签名的截图文件并命名为本地时间

    本文通过实例代码给大家介绍了Android仿银行客户签名并且保存签名的截图文件并命名为本地时间,需要的朋友可以参考下
    2017-07-07
  • Android apk 项目一键打包并上传到蒲公英的实现方法

    Android apk 项目一键打包并上传到蒲公英的实现方法

    这篇文章主要介绍了Android apk 项目一键打包并上传到蒲公英,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • android 自定义控件 自定义属性详细介绍

    android 自定义控件 自定义属性详细介绍

    在android相关应用开发过程中,固定的一些属性可能满足不了开发的需求,所以在一些特殊情况下,需要自定义控件与属性,本文将以此问题进行详细介绍,需要的朋友可以参考下
    2012-11-11
  • android listview实现新闻列表展示效果

    android listview实现新闻列表展示效果

    这篇文章主要为大家详细介绍了android listview实现新闻列表展示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03

最新评论