该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这是一个点击触摸移动的代码
toast_view = View.inflate(getApplicationContext(), R.layout.toast_phone, null);
params = new WindowManager.LayoutParams();
toast_view.setOnTouchListener(new OnTouchListener() {// 归属地吐司按住拖动
private int startX;
private int startY;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) motionEvent.getRawX();
startY = (int) motionEvent.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int disX = (int) motionEvent.getRawX() - startX;
int disY = (int) motionEvent.getRawY() - startY;
params.x = params.x +disX;
params.y = params.y + disY;
if (params.x< 0) {// 左边<0
params.x= 0;
}
if (params.y <0 ) {
params.y =0 ;
}
if (params.x >wm.getDefaultDisplay().getWidth() - toast_view.getWidth() ) {
params.x = wm.getDefaultDisplay().getWidth() - toast_view.getWidth();
}
if (params.y > wm.getDefaultDisplay().getHeight() - toast_view.getHeight() - 22) {// 大于屏幕宽度
params.y = wm.getDefaultDisplay().getHeight() - toast_view.getHeight() - 22;
}
startX = (int) motionEvent.getRawX();
startY = (int) motionEvent.getRawY();
wm.updateViewLayout(toast_view, params);
break;
case MotionEvent.ACTION_UP:
SharepreferUtils.putInt(getApplicationContext(), ConstantValue.TOAST_LOCATIONX, params.x);
SharepreferUtils.putInt(getApplicationContext(), ConstantValue.TOAST_LOCATIONY, params.y);
break;
}
//只有ontouch方法 没有onclcik 所以return true
return true;
}
});
}