Android 为应用增加可移动的悬浮窗口

来源:互联网 发布:雅马哈p115软件 编辑:程序博客网 时间:2024/04/29 12:48

需求描述:
在使用手机客户端浏览内嵌网页的时候,界面出现一个悬浮的刷新按钮,点击网页重新加载。


布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView         android:id="@+id/float_fresh"        android:padding="5dp"        android:layout_width="40dp"        android:layout_height="40dp"        android:src="@drawable/icon_fresh"        android:background="@drawable/bg_floatbutton"/></LinearLayout>





设置窗口类型在所有窗口之上:

这里说一下这个LayoutParams.TYPE_PHONE。
我们看一下官方文档说明:
These are non-application windows providing user interaction with the phone (in particular incoming calls). These windows are normally placed above all applications, but behind the status bar. In multiuser systems shows on all users' windows.
就是说设置了这个属性之后,这个窗口会在所以的界面之上,但是在状态栏的下面。在多用户系统中,所有用户的窗口上都会显示。


定义拖动和点击事件:

        mFloatView.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stub  wmParams.x = sWidth - (int) event.getRawX() - mFloatLayout.getMeasuredWidth() / 2;                  wmParams.y = sHeight - (int) event.getRawY() - mFloatLayout.getMeasuredHeight() / 2;                if(wmParams.y > sHeight - titleHeight){                return true;                }                 //刷新                  mWindowManager.updateViewLayout(mFloatLayout, wmParams);                 return false;}});                mFloatView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubweb.reload();}});

别忘了权限声明:

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


效果图:


0 0