当前位置:   article > 正文

android实现全局悬浮球_andlua悬浮球代码

andlua悬浮球代码

android实现悬浮球功能:

设置悬浮球View操作代码:

  1. package lwnewoa.zjsos.com.floadballdemo;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.graphics.PixelFormat;
  5. import android.os.Build;
  6. import android.view.Gravity;
  7. import android.view.LayoutInflater;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.WindowManager;
  11. /**
  12. * Author:Stephen
  13. * Blog: https://me.csdn.net/jifenglie
  14. * Date:2020/3/16
  15. * Description:
  16. */
  17. public class FloatBallView {
  18. private Context context;
  19. private int height = 0;
  20. private int width = 0;
  21. public static FloatBallView floatView2;
  22. public static FloatBallView getInstance(Context context) {
  23. if (floatView2 == null) {
  24. floatView2 = new FloatBallView(context);
  25. }
  26. return floatView2;
  27. }
  28. public FloatBallView(Context c) {
  29. this.context = c;
  30. }
  31. private WindowManager wm;
  32. private View view;// 浮动按钮
  33. WindowManager.LayoutParams params;
  34. /**
  35. * 添加悬浮View
  36. *
  37. * @param
  38. */
  39. public void createFloatView() {
  40. if (view == null) {
  41. view = LayoutInflater.from(context).inflate(R.layout.home_floatview, null);
  42. }
  43. wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  44. height = wm.getDefaultDisplay().getHeight();
  45. width = wm.getDefaultDisplay().getWidth();
  46. params = new WindowManager.LayoutParams();
  47. params.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;// 所有程序窗口的“基地”窗口,其他应用程序窗口都显示在它上面。
  48. params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  49. params.format = PixelFormat.TRANSLUCENT;// 不设置这个弹出框的透明遮罩显示为黑色
  50. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  51. params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
  52. } else {
  53. params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  54. }
  55. params.width = WindowManager.LayoutParams.WRAP_CONTENT;
  56. params.height = WindowManager.LayoutParams.WRAP_CONTENT;
  57. params.gravity = Gravity.TOP | Gravity.LEFT;
  58. int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
  59. int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
  60. params.y = screenHeight - height / 3;//设置距离底部高度为屏幕三分之一
  61. params.x = screenWidth;
  62. view.setBackgroundColor(Color.TRANSPARENT);
  63. view.setVisibility(View.VISIBLE);
  64. view.setOnTouchListener(new View.OnTouchListener() {
  65. // 触屏监听
  66. float lastX, lastY;
  67. int oldOffsetX, oldOffsetY;
  68. int tag = 0;// 悬浮球 所需成员变量
  69. @Override
  70. public boolean onTouch(View v, MotionEvent event) {
  71. final int action = event.getAction();
  72. float x = event.getX();
  73. float y = event.getY();
  74. if (tag == 0) {
  75. oldOffsetX = params.x; // 偏移量
  76. oldOffsetY = params.y; // 偏移量
  77. }
  78. if (action == MotionEvent.ACTION_DOWN) {
  79. lastX = x;
  80. lastY = y;
  81. } else if (action == MotionEvent.ACTION_MOVE) {
  82. params.x += (int) (x - lastX) / 3; // 减小偏移量,防止过度抖动
  83. params.y += (int) (y - lastY) / 3; // 减小偏移量,防止过度抖动
  84. tag = 1;
  85. wm.updateViewLayout(view, params);
  86. } else if (action == MotionEvent.ACTION_UP) {
  87. int newOffsetX = params.x;
  88. int newOffsetY = params.y;
  89. // 只要按钮一动位置不是很大,就认为是点击事件
  90. if (Math.abs(oldOffsetX - newOffsetX) <= 20
  91. && Math.abs(oldOffsetY - newOffsetY) <= 20) {
  92. if (l != null) {
  93. l.onClick(view);
  94. }
  95. } else {
  96. if (params.x < width / 2) {
  97. params.x = 0;
  98. } else {
  99. params.x = width;
  100. }
  101. wm.updateViewLayout(view, params);
  102. tag = 0;
  103. }
  104. }
  105. return true;
  106. }
  107. });
  108. try {
  109. wm.addView(view, params);
  110. } catch (Exception e) {
  111. }
  112. /*floatView2.onFloatViewClick(new View.OnClickListener() {
  113. @Override
  114. public void onClick(View v) {
  115. //这边是点击悬浮按钮的响应事件
  116. Toast.makeText(context, "点击了悬浮球", Toast.LENGTH_LONG);
  117. }
  118. });*/
  119. }
  120. /**
  121. * 点击浮动按钮触发事件,需要override该方法
  122. */
  123. private View.OnClickListener l;
  124. public void onFloatViewClick(View.OnClickListener l) {
  125. this.l = l;
  126. }
  127. /**
  128. * 将悬浮View从WindowManager中移除,需要与createFloatView()成对出现
  129. */
  130. public void removeFloatView() {
  131. if (wm != null && view != null) {
  132. wm.removeViewImmediate(view);
  133. // wm.removeView(view);//不要调用这个,WindowLeaked
  134. view = null;
  135. wm = null;
  136. }
  137. }
  138. /**
  139. * 隐藏悬浮View
  140. */
  141. public void hideFloatView() {
  142. if (wm != null && view != null && view.isShown()) {
  143. view.setVisibility(View.GONE);
  144. }
  145. }
  146. /**
  147. * 显示悬浮View
  148. */
  149. public void showFloatView() {
  150. if (wm != null && view != null && !view.isShown()) {
  151. view.setVisibility(View.VISIBLE);
  152. }
  153. }
  154. public void updateViewLayout() {
  155. if (wm != null) {
  156. int screenWidth = (int) 480;
  157. int screenHeight = (int) 720;
  158. if (screenWidth == 0) {
  159. screenWidth = context.getResources().getDisplayMetrics().widthPixels;
  160. }
  161. if (screenHeight == 0) {
  162. screenHeight = context.getResources().getDisplayMetrics().heightPixels;
  163. params.y = screenHeight - height / 3;//设置距离底部高度为屏幕三分之一
  164. } else {
  165. params.y = screenHeight;
  166. }
  167. params.x = screenWidth;
  168. wm.updateViewLayout(view, params);
  169. }
  170. }
  171. }

home_floatview.xml : 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <de.hdodenhof.circleimageview.CircleImageView
  6. android:layout_width="96dp"
  7. android:layout_height="96dp"
  8. android:src="@drawable/ic_home_zfxzs" />
  9. </android.support.constraint.ConstraintLayout>

项目首页使用:

  1. package lwnewoa.zjsos.com.floadballdemo;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Build;
  5. import android.os.Bundle;
  6. import android.provider.Settings;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.Toast;
  11. public class MainActivity extends AppCompatActivity {
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. Button button1 = findViewById(R.id.button1);
  17. Button button2 = findViewById(R.id.button2);
  18. button1.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  22. //检查是否已经授予权限
  23. if (!Settings.canDrawOverlays(MainActivity.this)) {
  24. //若未授权则请求权限
  25. Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
  26. intent.setData(Uri.parse("package:" + getPackageName()));
  27. startActivityForResult(intent, 0);
  28. }
  29. }
  30. }
  31. });
  32. button2.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. FloatBallView.getInstance(MainActivity.this).createFloatView();
  36. }
  37. });
  38. //这边是点击悬浮按钮的响应事件
  39. FloatBallView.getInstance(MainActivity.this).onFloatViewClick(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. Toast.makeText(MainActivity.this, "点击了悬浮球", Toast.LENGTH_LONG).show();
  43. }
  44. });
  45. }
  46. }

 activity_main :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context="lwnewoa.zjsos.com.floadballdemo.MainActivity">
  8. <Button
  9. android:id="@+id/button1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_marginLeft="30dp"
  13. android:layout_marginTop="50dp"
  14. android:text="获取悬浮球权限" />
  15. <Button
  16. android:id="@+id/button2"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_marginLeft="30dp"
  20. android:layout_marginTop="10dp"
  21. android:text="打开悬浮球" />
  22. </LinearLayout>

权限:

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

第三方 圆形图片

implementation 'de.hdodenhof:circleimageview:2.1.0'

项目地址:FloadBallDemo.zip-Android代码类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/751817
推荐阅读
相关标签
  

闽ICP备14008679号