该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package com.mycompany.myapp;
import android.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import java.util.zip.*;
import android.R;
public class MainActivity extends Activity
{
private static WindowManager wm;
private static WindowManager.LayoutParams params;
private View m;
String TAG ="FxService";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/
createFloatView();
Log.i(TAG, "oncreat");
}
@Override
public IBinder onBind(Intent p1)
{
// TODO: Implement this method
return null;
}
private void createFloatView() {
m=LayoutInflater.from(this).inflate(R.layout.browser_link_context_header,null);
//btn_floatView = new Button(getApplicationContext());
//btn_floatView.setText("悬浮窗");
wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams();
// 设置window type
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
/*
* 如果设置为params.type = WindowManager.LayoutParams.TYPE_PHONE;
* 那么优先级会降低一些, 即拉下通知栏不可见
*/
//params.format=PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
// 设置Window flag
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
/*
* 下面的flags属性的效果形同“锁定”。
* 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。
wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE
| LayoutParams.FLAG_NOT_TOUCHABLE;
*/
// 设置悬浮窗的长得宽
params.width = 100;
params.height = 100;
// 设置悬浮窗的Touch监听
m.setOnTouchListener(new OnTouchListener() {
int lastX, lastY;
int paramX, paramY;
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
paramX = params.x;
paramY = params.y;
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
params.x = paramX + dx;
params.y = paramY + dy;
// 更新悬浮窗位置
wm.updateViewLayout(m, params);
break;
}
return true;
}
});
wm.addView(m, params);
//isAdded = true;
}
}