我正在实现在屏幕上移动视图的方法,但是我需要知道如何验证它不会离开屏幕的边缘。
我不想发生这种情况
mi活动代码
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;
}
}
}
请有人帮我,我需要这个,谢谢
答案 0 :(得分:2)
首先,您必须获取屏幕的宽度和高度
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