先看效果
未扩展
扩展后
代码说明
AndroidManifest..xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.hpc.assistant"
android:versionCode="1"
android:versionName="1.0" >
<!--需要下面的权限-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.GET_TASKS" />
<application
android:name=".FloatApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.hpc.assistant.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
App 类
[java]
package cn.hpc.assistant;
import android.app.Application;
import android.view.WindowManager;
public class FloatApplication extends Application {
private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();
public WindowManager.LayoutParams getWmParams(){
return wmParams;
}
}
MainActivity 类
[java]
package cn.hpc.assistant;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
public class MainActivity extends Activity {
private WindowManager wm = null;
private WindowManager.LayoutParams wmParams = null;
private FloatFrame myFV = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建悬浮窗口
createFloatView();
this.finish(); // 显示悬浮窗口后,Activity自动退出
}
private void createFloatView() {
Point windowSize = new Point();
this.getWindowManager().getDefaultDisplay().getSize(windowSize);
myFV = new FloatFrame(getApplicationContext());
// 获取WindowManager
wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
// 设置LayoutParams(全局变量)相关参数
wmParams = ((FloatApplication) getApplication()).getWmParams();
/**
* 以下都是WindowManager.LayoutParams的相关属性 具体用途可参考SDK文档
*/
wmParams.type = 2003;//LayoutParams.TYPE_PHONE; // 设置window type
wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
// 设置Window flag
wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
/*
* 下面的flags属性的效果形同“锁定”。 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。
* wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL |
* LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;
*/
wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角
// wmParams.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT; // 调整悬浮窗口至左上角
//设置默认显示位置
// wmParams.x = 0;<span style="font-family: Arial, Helvetica, sans-serif;">// 以屏幕左上角为原点,设置x、y初始值</span>
// wmParams.y = 0;
wmParams.x = windowSize.x;<span style="font-family: Arial, Helvetica, sans-serif;">// 以屏幕右边, 距中</span>
wmParams.y = windowSize.y / 2;
// 设置悬浮窗口长宽数据