标签:
一、知识储备
1、在桌面显示一个View
WindowManager mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.format = PixelFormat.TRANSLUCENT; params.type = WindowManager.LayoutParams.TYPE_PHONE;//电话窗口,它置于所有窗口 params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; params.gravity=Gravity.LEFT + Gravity.TOP;//将重心设置为左上方,而不是默认的中心 params.setTitle("Toast"); View view = View.inflate(this, R.layout.rocket, null); mWM.addView(view, params);
注意:
1、如果上次离开时,图标在边缘(超出屏幕),这时系统为了显示图标为缩小图标
防止图标在拖放中超出屏幕
更新View方法1
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { // 获取起始坐标 startX = (int) event.getRawX(); startY = (int) event.getRawY(); break; } case MotionEvent.ACTION_MOVE: { endX = (int) event.getRawX(); endY = (int) event.getRawY(); // 计算偏移量 int dx = endX - startX; int dy = endY - startY; // 更新浮窗位置 params.x += dx; params.y += dy; // 防止坐标偏离屏幕 if (params.x < 0) { params.x = 0; } if (params.y < 0) { params.y = 0; } if (params.x > winWidth - view.getWidth()) { params.x = winWidth - view.getWidth(); } if (params.y > winWidth - view.getHeight()) { params.y = winWidth - view.getHeight(); } mWM.updateViewLayout(view, params);//更新视图 // 更新起点坐标 startX = endX; startY = endY; break; } case MotionEvent.ACTION_UP: //记录坐标点 Editor editor=mPref.edit(); editor.putInt("lastX", params.x); editor.putInt("lastY", params.y); editor.commit(); sendRocket(); break; default: break; } return true; }
更新View方法2
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { // 获取起始坐标 startX = (int) event.getRawX(); startY = (int) event.getRawY(); break; } case MotionEvent.ACTION_MOVE: { endX = (int) event.getRawX(); endY = (int) event.getRawY(); // 计算偏移量 int dx = endX - startX; int dy = endY - startY; int left = view.getLeft() + dx; int top = view.getTop() + dy; int right = view.getRight() + dx; int bottom = view.getBottom() + dy; // 防止坐标偏离屏幕(注意状态栏高度) if(left < 0 || right>winWidth || top<0 || bottom>winHeight-20 ) { break; } view.layout(left,top,right,bottom); // 更新起点坐标 startX = endX; startY = endY; break; } case MotionEvent.ACTION_UP: //记录坐标点 Editor editor=mPref.edit(); editor.putInt("lastX", params.x); editor.putInt("lastY", params.y); editor.commit(); sendRocket(); break; default: break; } return true; }
2、安卓底层在画图标时分三步onMeasure(测量View),onLayout(安放位置),onDraw(绘制)
不能在Activity的onCreate(还没测量完)中使用view.layout(left,top,right,bottom);
解决办法:
//保存最后离开时的位置 private SharedPreferences mPref; mPref = getSharedPreferences("config", MODE_PRIVATE); mPref = getSharedPreferences("config", MODE_PRIVATE); Editor editor=mPref.edit(); editor.putInt("lastX", params.x); editor.putInt("lastY", params.y); editor.commit() //还原上次离开时的位置 int lastX=mPref.getInt("lastX", 10); int lastY=mPref.getInt("lastY", 10); LayoutParams layoutParams=(LayoutParams) view.getLayoutParams(); layoutParams.leftMargin=lastX;//设置左边距 layoutParams.topMargin=lastY;//设置右边距 view.setLayoutParams(layoutParams);
标签:
原文地址:http://www.cnblogs.com/324sige/p/5744918.html