gpt4 book ai didi

android - ACTION_UP 不起作用

转载 作者:行者123 更新时间:2023-11-29 14:25:28 25 4
gpt4 key购买 nike

我正在尝试做一个用户可以移动图像的应用程序......当用户将它移动到某个地方时,应用程序应该做其他事情......有点解锁......

但问题是当 ACTION_UP 语句不起作用时...有什么建议吗?

这是我的代码:

public class Main extends Activity {

private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ViewGroup vg = (ViewGroup) findViewById(R.id.lout);
vg.setOnTouchListener(new View.OnTouchListener() {

@Override
@SuppressWarnings("deprecation")
public boolean onTouch(View v, MotionEvent event) {

switch (event.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX() - offset_x;
int y = (int) event.getY() - offset_y;
int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
if (x > w)
x = w;
if (y > h)
y = h;
AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT));
lp.x = x;
lp.y = y;
selected_item.setLayoutParams(lp);
break;
default:
break;
}
return true;
}
});

ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
offset_x = (int) event.getX();
offset_y = (int) event.getY();
selected_item = v;
break;
case MotionEvent.ACTION_UP:
if (offset_x < 150 && offset_x > 120 && offset_y < 230
&& offset_y > 200)
startActivity(new Intent(
"com.yahya.GeneralTraining.UNLOCKED"));
break;
default:
break;
}
return false;

}
});

}
}

最佳答案

我认为您需要在 imageView onTouch() 回调中返回 true。出于某种原因,在 ACTION_DOWN 之后返回 false 会导致您无法通过 ACTION_UP 接到另一个电话。

关于android - ACTION_UP 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9020031/

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