gpt4 book ai didi

java - 我有一个关于动画拖放的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:05:19 25 4
gpt4 key购买 nike

我正在实现在屏幕上移动 View 的方法,但我需要知道如何验证它不会离开屏幕边缘。

image

image 2

我不想发生这种事

image 3

mi 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();
}
});

然后您必须验证 View 的边界,以便 left > 0 , right < w , bottom < htop > 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 像素的 View 。我们有 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

关于java - 我有一个关于动画拖放的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58123485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com