Android Demo : 悬浮窗

Android Demo : 悬浮窗

设计思路分析

本Demo的设计思路如下:

一个MainActivity作为App的窗口,APP在打开时启动MainAcitivity,MainActivity在确定权限等操作后转到Service并关闭自己。
一个Service作为Windowmanager的载体。在Service中我们进行悬浮窗的初始设置并开启它。
WindowManager,配套一个XML文件,里面是悬浮窗的布局和里面的各种组件,本Demo中就放一个小小的ImageButton。

跟我一起动手做

  • 1、我们新建一个Project,这个Project有一个MainActivity。Demo中的MainActivity就用BlackActivity。

  • 2、有了MainActivity后,我们要去除掉它的XML文件,在MainActivity中仅保留以下代码

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }
}

然后,把layout文件夹中MainActivity对应的XML文件放心地删除。

  • 3、创建一个Service的Java文件,在Demo中我把它取名为MainService。
    该Service的onBind直接return一个null就行了,另外一定要在Manifest文件中注册它。

    • 4、在MainActivity中启动Service并且让MainActivity结束自己。
      注意,这里重点来了。
      在Android 6.0后,Android需要动态获取权限,要使用权限,不仅仅要在Manifest文件中定义,还要在代码中动态获取
      详细了解请看这里,有专业级介绍。
      http://blog.csdn.net/caroline_wendy/article/details/50587230
      我们就直奔主题吧

if (Build.VERSION.SDK_INT >= 23) {
            if (Settings.canDrawOverlays(MainActivity.this)) {
                Intent intent = new Intent(MainActivity.this, MainService.class);
                Toast.makeText(MainActivity.this,"已开启Toucher",Toast.LENGTH_SHORT).show();
                startService(intent);
                finish();
            } else {
                //若没有权限,提示获取.
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                Toast.makeText(MainActivity.this,"需要取得权限以使用悬浮窗",Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        } else {
            //SDK在23以下,不用管.
            Intent intent = new Intent(MainActivity.this, MainService.class);
            startService(intent);
            finish();
        }

上面的代码是要写的动态权限检测,同时在Manifest文件中还要添加对应权限,我粘贴的源码中也提示我们了。
我们需要两个权限,一个ALERT_WINDOW,一个OVERLAY_WINDOW

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

作者:LightingContour
链接:https://www.jianshu.com/p/ac63c57d2555
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

好了这里Manifest文件和MainActivity就定型了,下一步。
- 5、设置layout
这里我就不细说了,我就放了一个ImageButton上去,布局大小设置为了300*300,贴了个自己的背景。

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

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@android:drawable/btn_star_big_on"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

</android.support.constraint.ConstraintLayout>
  • 6、最后一个重点来了。这里我们要开始码我们的Service

添加以下的全局变量,以便之后的赋值

“`
//Log用的TAG
private static final String TAG = “MainService”;

//要引用的布局文件.
ConstraintLayout toucherLayout;
//布局参数.
WindowManager.LayoutParams params;
//实例化的WindowManager.
WindowManager windowManager;

ImageButton imageButton1;

//状态栏高度.(接下来会用到)
int statusBarHeight = -1;


在onCreate函数中生成悬浮窗

@Override
public void onCreate()
{
super.onCreate();
Log.i(TAG,”MainService Created”);
//OnCreate中来生成悬浮窗.
createToucher();
}


下面的代码分解开实在是太耗费排版,故我在代码中已经做了简要的注释说明。更多的小细节附在代码后。

private void createToucher()
{
//赋值WindowManager&LayoutParam.
params = new WindowManager.LayoutParams();
windowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
//设置type.系统提示型窗口,一般都在应用程序窗口之上.
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//设置效果为背景透明.
params.format = PixelFormat.RGBA_8888;
//设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控.
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

    //设置窗口初始停靠位置.
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.x = 0;
    params.y = 0;

    //设置悬浮窗口长宽数据.
    //注意,这里的width和height均使用px而非dp.这里我偷了个懒
    //如果你想完全对应布局设置,需要先获取到机器的dpi
    //px与dp的换算为px = dp * (dpi / 160).
    params.width = 300;
    params.height = 300;

    LayoutInflater inflater = LayoutInflater.from(getApplication());
    //获取浮动窗口视图所在布局.
    toucherLayout = (ConstraintLayout) inflater.inflate(R.layout.layout,null);
    //添加toucherlayout

    if (Build.VERSION.SDK_INT > 25) {
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    }
    windowManager.addView(toucherLayout,params);

    Log.i(TAG,"toucherlayout-->left:" + toucherLayout.getLeft());
    Log.i(TAG,"toucherlayout-->right:" + toucherLayout.getRight());
    Log.i(TAG,"toucherlayout-->top:" + toucherLayout.getTop());
    Log.i(TAG,"toucherlayout-->bottom:" + toucherLayout.getBottom());

    //主动计算出当前View的宽高信息.
    toucherLayout.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);

    //用于检测状态栏高度.
    int resourceId = getResources().getIdentifier("status_bar_height","dimen","android");
    if (resourceId > 0)
    {
        statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    }
    Log.i(TAG,"状态栏高度为:" + statusBarHeight);

    //浮动窗口按钮.
    imageButton1 = (ImageButton) toucherLayout.findViewById(R.id.imageButton1);
    imageButton1.setOnClickListener(new View.OnClickListener() {
        long[] hints = new long[2];
        @Override
        public void onClick(View v) {
            Log.i(TAG,"点击了");
            System.arraycopy(hints,1,hints,0,hints.length -1);
            hints[hints.length -1] = SystemClock.uptimeMillis();
            if (SystemClock.uptimeMillis() - hints[0] >= 700)
            {
                Log.i(TAG,"要执行");
                Toast.makeText(MainService.this,"连续点击两次以退出",Toast.LENGTH_SHORT).show();
            }else
            {
                Log.i(TAG,"即将关闭");
                stopSelf();
            }
        }
    });
    imageButton1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //ImageButton我放在了布局中心,布局一共300dp
            params.x = (int) event.getRawX() - 150;
            //这就是状态栏偏移量用的地方
            params.y = (int) event.getRawY() - 150 - statusBarHeight;
            windowManager.updateViewLayout(toucherLayout,params);
            return false;
        }
    });

    //其他代码...
}

“`
简要说明

1.Params的各种设置是在做什么:
大家大略看完上面的代码后应该会明白,我们设置了params的各种值,再让它成为我们要生成的悬浮窗的参数集。
这里我简要说明就好了,细节请看这里
http://www.jianshu.com/p/95ceb0a2ed27

  • type 它用于表示悬浮窗的类型。类型太多太多,这里我们可以使用SYSTEM_ALERT,它覆盖在所有应用的最上方,符合我们的需求。附上源码。其实我试过TYPE_PHONE也是可以的。

  • format 用于设置显示的格式。RGBA_8888是透明型,也是我们最常用到的了。

  • flags 这是很重要的一个设置。FLAG_NOT_FOCUSABLE设置了不可聚焦,代码中注释好了的。同时我们经常用的还有FLAG_WATCH_OUTSIDE_TOUCH,这个设置可以让悬浮窗接收到外部点击事件,如果你想在之后做小悬浮窗点击变大,再点击悬浮窗之外又变回小悬浮窗。这个可以用到。多个FLAG的话可以用|来连接,如
    params.flags = FLAG_NOT_FOCUSABLE | FLAG_WATCH_OUTSIDE_TOUCH
  • gravity 用于设置窗口的初始停靠位置。我们设置的是让它初始在最左&最上方生成,后面两句是定义这里的xy值都为0。
  • width&height用于设置悬浮窗口的大小,建议设置成和布局一样大最好。小于布局会把里面的组件进行挤压。

2.为什么要测试状态栏高度:
这位作者告诉了我们。
http://blog.csdn.net/a_running_wolf/article/details/50477965
简而言之就是应用区域!=屏幕区域。这里得到状态栏宽度用于在之后更新悬浮窗时计算偏移量。

第二步 图片按钮设置了连续点击两下退出悬浮窗。关闭Service使用stopSelf()方法。

imageButton1.setOnClickListener(new View.OnClickListener() {
            long[] hints = new long[2];
            @Override
            public void onClick(View v) {
                Log.i(TAG,"点击了");
                System.arraycopy(hints,1,hints,0,hints.length -1);
                hints[hints.length -1] = SystemClock.uptimeMillis();
                if (SystemClock.uptimeMillis() - hints[0] >= 700)
                {
                    Log.i(TAG,"要执行");
                    Toast.makeText(MainService.this,"连续点击两次以退出",Toast.LENGTH_SHORT).show();
                }else
                {
                    Log.i(TAG,"即将关闭");
                    stopSelf();
                }
            }
        });

第三步 给图片按钮设置OnTouchListener,让悬浮窗可以拖动。

imageButton1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //ImageButton我放在了布局中心,布局一共300dp
                params.x = (int) event.getRawX() - 150;
                //这就是状态栏偏移量用的地方
                params.y = (int) event.getRawY() - 150 - statusBarHeight;
                windowManager.updateViewLayout(toucherLayout,params);
                return false;
            }
        });

第四步 OnDestroy时销毁WindowManager

@Override
    public void onDestroy()
    {
        //用imageButton检查悬浮窗还在不在,这里可以不要。优化悬浮窗时要用到。
        if (imageButton1 != null)
        {
            windowManager.removeView(toucherLayout);
        }
        super.onDestroy();
    }

结语
这里的悬浮窗Demo比较简单,适合新手。剩下的美化什么的就由各位自己去发掘了。
另外这里有个小问题,在华为手机上Toast一直没法显示出来,其他机型都可以。奇特的华为。如果有解决方案欢迎评论在下方。

源码在GitHub,地址https://github.com/LightingContour/Toucher

作者:LightingContour
链接:https://www.jianshu.com/p/ac63c57d2555
來源:简书

猜你喜欢

转载自blog.csdn.net/m0_37909265/article/details/82190966