问题描述
- 关于ontouchlistener的监听滑动问题
-
relativeLayout.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: x=event.getX(); y=event.getY(); break; case MotionEvent.ACTION_MOVE: mx=event.getX(); my=event.getY(); rx=mx-x; ry=my-y; mParams.x =(int) rx; mParams.y =(int)ry; mWM.updateViewLayout(ivCursor, mParams); break; case MotionEvent.ACTION_UP: break; } return true; } }); 浮动框的代码 mWM = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE); ivCursor = new ImageView(getBaseContext()); ivCursor.setImageResource(R.drawable.ic_launcher); mParams = new WindowManager.LayoutParams();// 对ivCursor对象的参数描述对象 mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;// 宽度自适应 mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;// 高度自适应 mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 设置成不能获取焦点 mParams.type = LayoutParams.TYPE_PHONE; // 设置图片格式,效果为背景透明 mParams.format = PixelFormat.RGBA_8888; mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL; mParams.x = 0; mParams.y = 0; mWM.addView(ivCursor, mParams);
当我手指滑动的时候 浮动框是跟着动的 松开后 再去滑动 浮空框就又从原点开始滑动了 有什么办法能让浮动框不从原点开始 就从停止的地方开始滑动啊
解决方案
relativeLayout外层再套一个relativelayout布局。
然后内层relativeLayout就可以拖动更新了
解决方案二:
case MotionEvent.ACTION_DOWN:
x=event.getX();//点击的时候坐标,假如是50
y=event.getY();
break;
case MotionEvent.ACTION_MOVE:
mx=event.getX(); //这里一移动,还是获取的还是50
my=event.getY();
rx=mx-x; //rx就是0或者比0大点
ry=my-y;
mParams.x =(int) rx; //屏幕的上坐标就是0了,
mParams.y =(int)ry;
mWM.updateViewLayout(ivCursor, mParams); //更新后回到屏幕左上角原点
break;
改成类似
int mx0 = 0;
int my0 = 0;
case MotionEvent.ACTION_MOVE:
mx=event.getX();
my=event.getY();
mx0=mx0==0?mx:mx0;
my0=my0==0?my:my0;
rx=mx-mx0+x;
ry=my-my0+y;
my0=my;
mParams.x =(int) rx;
mParams.y =(int)ry;
mWM.updateViewLayout(ivCursor, mParams);
break;
解决方案三:
http://blog.csdn.net/lxk_1993/article/details/50725489
时间: 2024-10-30 12:51:05