我有一个关于动画拖放的问题
i have one problem with animation drag and drop
我正在实现在屏幕上移动视图的方法,但我需要知道如何验证它不会离开屏幕边缘。
image
image 2
我不想发生这种事
image 3
小米代码activity
public class MainActivity extends AppCompatActivity {
private ImageView img;
private RelativeLayout relativeLayout;
private View rootLayout;
private int _xDelta;
private int _yDelta;
private TextView textView;
private int modificarX = 350;
private int modificarY = 350;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.get);
rootLayout = (View) findViewById(R.id.view_root);
img = (ImageView) rootLayout.findViewById(R.id.imageView);
final RelativeLayout.LayoutParams[] layoutParams = {new RelativeLayout.LayoutParams(600, 600)};
img.setLayoutParams(layoutParams[0]);
img.setOnTouchListener(new ChoiceTouchListener());
}
private final class ChoiceTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
textView.setText("X: " + X + " Y: " + Y);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta - 10;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
Toast.makeText(MainActivity.this, "X: " + X + " Y: " + Y, Toast.LENGTH_LONG).show();
rootLayout.getRight();
break;
}
rootLayout.invalidate();
return true;
}
}
}
有人帮我解决这个问题,我需要这个,谢谢
首先你必须得到屏幕的宽度和高度
int w = getResources().getDisplayMetrics().widthPixels
int h = getResources().getDisplayMetrics().heightPixels
或 rootLayout 的(在你的 rootLayout 已经布局时执行,否则你将得到 0):
int w = rootLayout.getWidth();
int h = rootLayout.getHeight();
// or if view haven't been laid out yet:
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rootLayout.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
int w = rootLayout.getWidth();
int h = rootLayout.getHeight();
}
});
然后您必须验证视图的边界,以便 left > 0
、right < w
、bottom < h
和 top > 0
.
layoutParams.leftMargin = Math.max(0, Math.min(rootLayout.getWidth() - view.getWidth(), X - _xDelta - 10))
layoutParams.topMargin = Math.max(0, Math.min(rootLayout.getHeight() - view.getHeight(), Y - _yDelta))
为了解释此处发生的情况,让我们使用 500x800 像素的屏幕和 100x100 像素的视图。我们有3个案例,其中2个在边界外,1个在边界内。
// TEST 1
int out1X = -50; //x coordinate of the view
int out1Y = -50; //y coordinate of the view
layoutParams.leftMargin = Math.max(0, Math.min(w - 100/*view width*/, out1X))
layoutParams.topMargin = Math.max(0, Math.min(h - 100/*view height*/, out1Y))
// here we have for leftMargin max(0, min(400, -50)) => max(0, -50) => 0 your view won't go further than 0
// for topMargin: max(0, min(700, -50)) => max(0, -50) => 0
// TEST 2
int out2X = 650; //x coordinate of the view
int out2Y = 950; //y coordinate of the view
layoutParams.leftMargin = Math.max(0, Math.min(w - 100, out2X))
layoutParams.topMargin = Math.max(0, Math.min(h - 100, out2Y))
// leftMargin: max(0, min(400, 650)) => max(0, 400) => 400
// topMargin: max(0, min(700, 950)) => max(0, 700) => 700
// TEST 3
int inX = 50; //x coordinate of the view
int inY = 50; //y coordinate of the view
layoutParams.leftMargin = Math.max(0, Math.min(w - 100, inX))
layoutParams.topMargin = Math.max(0, Math.min(h - 100, inY))
// leftMargin: max(0, min(400, 50)) => max(0, 50) => 50
// topMargin: max(0, min(700, 50)) => max(0, 50) => 50
// in this case we get our original coordinates
我正在实现在屏幕上移动视图的方法,但我需要知道如何验证它不会离开屏幕边缘。
image
image 2
我不想发生这种事
image 3
小米代码activity
public class MainActivity extends AppCompatActivity {
private ImageView img;
private RelativeLayout relativeLayout;
private View rootLayout;
private int _xDelta;
private int _yDelta;
private TextView textView;
private int modificarX = 350;
private int modificarY = 350;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.get);
rootLayout = (View) findViewById(R.id.view_root);
img = (ImageView) rootLayout.findViewById(R.id.imageView);
final RelativeLayout.LayoutParams[] layoutParams = {new RelativeLayout.LayoutParams(600, 600)};
img.setLayoutParams(layoutParams[0]);
img.setOnTouchListener(new ChoiceTouchListener());
}
private final class ChoiceTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
textView.setText("X: " + X + " Y: " + Y);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta - 10;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
Toast.makeText(MainActivity.this, "X: " + X + " Y: " + Y, Toast.LENGTH_LONG).show();
rootLayout.getRight();
break;
}
rootLayout.invalidate();
return true;
}
}
}
有人帮我解决这个问题,我需要这个,谢谢
首先你必须得到屏幕的宽度和高度
int w = getResources().getDisplayMetrics().widthPixels
int h = getResources().getDisplayMetrics().heightPixels
或 rootLayout 的(在你的 rootLayout 已经布局时执行,否则你将得到 0):
int w = rootLayout.getWidth();
int h = rootLayout.getHeight();
// or if view haven't been laid out yet:
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rootLayout.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
int w = rootLayout.getWidth();
int h = rootLayout.getHeight();
}
});
然后您必须验证视图的边界,以便 left > 0
、right < w
、bottom < h
和 top > 0
.
layoutParams.leftMargin = Math.max(0, Math.min(rootLayout.getWidth() - view.getWidth(), X - _xDelta - 10))
layoutParams.topMargin = Math.max(0, Math.min(rootLayout.getHeight() - view.getHeight(), Y - _yDelta))
为了解释此处发生的情况,让我们使用 500x800 像素的屏幕和 100x100 像素的视图。我们有3个案例,其中2个在边界外,1个在边界内。
// TEST 1
int out1X = -50; //x coordinate of the view
int out1Y = -50; //y coordinate of the view
layoutParams.leftMargin = Math.max(0, Math.min(w - 100/*view width*/, out1X))
layoutParams.topMargin = Math.max(0, Math.min(h - 100/*view height*/, out1Y))
// here we have for leftMargin max(0, min(400, -50)) => max(0, -50) => 0 your view won't go further than 0
// for topMargin: max(0, min(700, -50)) => max(0, -50) => 0
// TEST 2
int out2X = 650; //x coordinate of the view
int out2Y = 950; //y coordinate of the view
layoutParams.leftMargin = Math.max(0, Math.min(w - 100, out2X))
layoutParams.topMargin = Math.max(0, Math.min(h - 100, out2Y))
// leftMargin: max(0, min(400, 650)) => max(0, 400) => 400
// topMargin: max(0, min(700, 950)) => max(0, 700) => 700
// TEST 3
int inX = 50; //x coordinate of the view
int inY = 50; //y coordinate of the view
layoutParams.leftMargin = Math.max(0, Math.min(w - 100, inX))
layoutParams.topMargin = Math.max(0, Math.min(h - 100, inY))
// leftMargin: max(0, min(400, 50)) => max(0, 50) => 50
// topMargin: max(0, min(700, 50)) => max(0, 50) => 50
// in this case we get our original coordinates