final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rel);
final ImageView circle = (ImageView) findViewById(R.id.circle);
final ViewGroup.MarginLayoutParams screen = (ViewGroup.MarginLayoutParams) rl.getLayoutParams();
和..爲什麼bottomMargin/topMargin/rightMargin表現得像leftMargin?
View v;
v = arrayofImages[i];
v.setOnTouchListener(new View.OnTouchListener() {
PointF DownPT = new PointF();
PointF StartPT = new PointF();
@Override
public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:
PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
v.setX((StartPT.x + mv.x));
v.setY((StartPT.y + mv.y));
StartPT = new PointF(v.getX(), v.getY());
if (circle.getRight() > v.getX() && circle.getTop() <= v.getY()
&& circle.getBottom() > v.getY() && circle.getLeft() <= v.getX()) {
v.setVisibility(View.INVISIBLE);
}
if (v.getX() > screen.bottomMargin) {
v.setX(screen.bottomMargin);
}
break;
case MotionEvent.ACTION_DOWN:
DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(v.getX(), v.getY());
break;
case MotionEvent.ACTION_UP:
// Nothing have to do
break;
default:
break;
}
return true;
}
});
所以基本上我想不會允許的ImageView去ofscreen。如果視圖v太靠左,則將其放置在左側。
if (v.getX() > screen.leftMargin) {
v.setX(screen.leftMargin);
}
這應該這樣做,但它只能用於左邊距。如果我嘗試這樣的事情
if (v.getX() > screen.rightMargin) {
v.setX(screen.rightMargin);
}
它就像它的LEFTMARGIN(它移動我的形象,當我觸摸它的左側。)
我不知道我做錯了。 ..請幫助..如果你需要更多的代碼,我會粘貼,但這是我認爲的一切。