当前位置:   article > 正文

Android自定义可跟随手指拖动的全局悬浮窗_anddroid 悬浮手指

anddroid 悬浮手指

一丶首先是自定义一个跟随手指滑动的View,目前只支持简单的悬浮拖动,可在此基础上进行功能扩展,比如直播类APP的悬浮播放窗也是这个原理,挺简单的,不多说了,直接上代码,

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.os.Build;
  5. import android.provider.Settings;
  6. import android.support.v7.widget.AppCompatImageView;
  7. import android.util.AttributeSet;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.ViewConfiguration;
  11. import android.view.WindowManager;
  12. import android.widget.Toast;
  13. /**
  14. * Created by vaiyee on 2019/12/28 0028.
  15. */
  16. public class FloatWindowImageView extends AppCompatImageView implements View.OnTouchListener {
  17. private WindowManager windowManager;
  18. private WindowManager.LayoutParams layoutParams;
  19. private Boolean isMove = false;
  20. private float lastX;
  21. public FloatWindowImageView(WindowManager windowManager,WindowManager.LayoutParams layoutParams,Context context) {
  22. super(context);
  23. this.windowManager = windowManager;
  24. this.layoutParams = layoutParams;
  25. setOnTouchListener(this);
  26. setOnClickListener(v -> Toast.makeText(context,"点击了",Toast.LENGTH_LONG).show());
  27. requestSettingCanDrawOverlays();
  28. }
  29. public FloatWindowImageView(Context context, AttributeSet attrs) {
  30. super(context, attrs);
  31. }
  32. public FloatWindowImageView(Context context, AttributeSet attrs, int defStyleAttr) {
  33. super(context, attrs, defStyleAttr);
  34. }
  35. @Override
  36. public boolean onTouch(View v, MotionEvent event) {
  37. float x = event.getRawX();
  38. switch (event.getAction()){
  39. case MotionEvent.ACTION_DOWN:
  40. isMove = false;
  41. break;
  42. case MotionEvent.ACTION_MOVE:
  43. if (Math.abs(x - lastX) > ViewConfiguration.get(getContext()).getScaledTouchSlop()){
  44. isMove = true;
  45. layoutParams.x = (int) (event.getRawX() - getWidth() / 2);
  46. layoutParams.y = (int) (event.getRawY() - getHeight() / 2);
  47. windowManager.updateViewLayout(this,layoutParams);
  48. }
  49. return true;
  50. case MotionEvent.ACTION_UP:
  51. if (isMove){
  52. return true;
  53. }
  54. }
  55. lastX = x;
  56. return super.onTouchEvent(event);
  57. }
  58. // 申请悬浮权限
  59. private void requestSettingCanDrawOverlays() {
  60. try {
  61. //判断当前系统版本
  62. if (Build.VERSION.SDK_INT >= 23) {
  63. //判断权限是否已经申请过了(加上这个判断,则使用的悬浮窗的时候;如果权限已经申请则不再跳转到权限开启界面)
  64. if (!Settings.canDrawOverlays(getContext())) {
  65. //申请权限
  66. Intent intent2 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
  67. ((Activity)getContext()).startActivityForResult(intent2, 1001);
  68. } else {
  69. //创建悬浮窗
  70. windowManager.addView(this, layoutParams);
  71. }
  72. } else {
  73. windowManager.addView(this, layoutParams);
  74. }
  75. System.out.println("Build.VERSION.SDK_INT::::" + Build.VERSION.SDK_INT);
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. // 申请完权限后显示出来
  81. public void showFloatWindow(){
  82. windowManager.addView(this, layoutParams);
  83. }
  84. }

 

二丶然后就是在activity中使用了:

  1. WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
  2. WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0
  4. layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
  5. } else {
  6. layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  7. }
  8. layoutParams.format = PixelFormat.TRANSLUCENT;
  9. layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  10. layoutParams.x = 0;
  11. layoutParams.y = 0;
  12. layoutParams.gravity = Gravity.START | Gravity.TOP;
  13. layoutParams.width = 200;
  14. layoutParams.height = 200;
  15. imageView = new FloatWindowImageView(windowManager,layoutParams,this);
  16. imageView.setImageResource(R.drawable.weixin);

 

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

闽ICP备14008679号