80,351
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags=""
android:canRetrieveWindowContent="true"
android:description="@string/accessibility_service_description"
android:notificationTimeout="100" />
mWindowManager = (WindowManager) getApplicationContext().getSystemService("window");
mLayout = new WindowManager.LayoutParams();
mLayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mLayout.format = PixelFormat.RGBA_8888;
mLayout.gravity = Gravity.TOP | Gravity.LEFT;
mLayout.width = WindowManager.LayoutParams.MATCH_PARENT;
mLayout.height = WindowManager.LayoutParams.WRAP_CONTENT;
private void createFloatView() {
wmParams = new WindowManager.LayoutParams();
mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
wmParams.x = 0;
wmParams.y = 0;
wmParams.width = 150;
wmParams.height = 150;
LayoutInflater inflater = LayoutInflater.from(getApplication());
mFloatLayout = (LinearLayout) inflater.inflate(R.layout.floatservice, null);
mWindowManager.addView(mFloatLayout, wmParams);
mFloatView = (ImageView) mFloatLayout.findViewById(R.id.float_iv);
mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
mFloatView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2;
Log.i(TAG, "RawX" + event.getRawX());
Log.i(TAG, "X" + event.getX());
//减25为状态栏的高度
wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25;
Log.i(TAG, "RawY" + event.getRawY());
Log.i(TAG, "Y" + event.getY());
//刷新
mWindowManager.updateViewLayout(mFloatLayout, wmParams);
return false; //此处必须返回false,否则OnClickListener获取不到监听
}
});
mFloatView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (popupWindow != null && popupWindow.isShowing())
popupWindow.dismiss();
else
initPopWindow();
}
});
}
protected void initPopWindow() {
// List<Msg> msgList=new ArrayList<Msg>();
// MessageAdapter adapter;
//
// initData(msgList);
// adapter = new MessageAdapter(getApplicationContext(),R.layout.msg_item, msgList);
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View view = inflater.inflate(R.layout.floatwindowservice, null);
// ListView listview = (ListView) view.findViewById(R.id.listview);
popupWindow = new PopupWindow(view,500,400);
popupWindow.setContentView(view);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
Button close = (Button) view.findViewById(R.id.close);
Button send = (Button) view.findViewById(R.id.send);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.setFocusable(false);
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(mFloatView,20,20);