我有一个可移动的视图。它也附加到windowmanager。我如何设法制作附加我的视图的窗口复制我视图的每个移动,以便它始终以我的视角移动?
我用它来移动我的观点:
private class MyTouchListener implements View.OnTouchListener{
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
dX = view.getX() - motionEvent.getRawX();
dY = view.getY() - motionEvent.getRawY();
break;
case MotionEvent.ACTION_MOVE:
view.animate()
.x(motionEvent.getRawX() + dX)
.y(motionEvent.getRawY() + dY)
.setDuration(0)
.start();
break;
case MotionEvent.ACTION_UP:
if(view.getX()>(windowManager.getDefaultDisplay().getWidth()/2)){
view.animate().x(windowManager.getDefaultDisplay().getWidth() - view.getWidth())
.y(view.getY())
.setDuration(0)
.start();
}else{
view.animate().x(0)
.y(view.getY())
.setDuration(0)
.start();
}
break;
}
return true;
}
}
我将代码添加到窗口的代码:
Button b = new Button(this);
b.setBackground(getResources().getDrawable(R.drawable.round_button));
b.setOnTouchListener(new MyTouchListener());
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
windowManager.addView(b, params);