我正試圖做一個應用程序,用戶可以移動圖像...而當用戶將其移動到某個地方時,應用程序應該做一些其他的事情...有點解鎖...ACTION_UP不起作用
但問題是,當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上? –
我怎麼知道它? – yahya