0
我有多個視圖被拖動,每個視圖都表示一種情感。這個想法是讓每個視圖向一個數據庫發送一個條目,該條目根據拖動的視圖而變化。拖動時將數據附加到不同視圖
的意見在這裏創造:
_root = (ViewGroup)findViewById(R.id.root);
_view = new ImageView(this);
_view.setImageResource(R.drawable.smile);
_view2 = new ImageView(this);
_view2.setImageResource(R.drawable.frown);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50);
layoutParams.leftMargin = 250;
layoutParams.topMargin = 250;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
_view.setLayoutParams(layoutParams);
_view.setOnTouchListener(this);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(150, 50);
layoutParams2.leftMargin = 250;
layoutParams2.topMargin = 500;
layoutParams2.bottomMargin = -250;
layoutParams2.rightMargin = -250;
_view2.setLayoutParams(layoutParams2);
_view2.setOnTouchListener(this);
_root.addView(_view);
_root.addView(_view2);
}
而且運動和阻力在這裏定義:
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
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:
Intent intent = new Intent(this, Exit_Activity.class);
startActivity(intent);
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;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}
}
正如你所看到的,ACTION_UP目前向用戶發送到下一個活動,不管哪個視圖被拖動。讓應用程序區分視圖的最佳方式是什麼?
非常感謝,只是想知道我是否應該使用if語句根據檢索到的ID,以確定所採取的行動,或者如果這樣做的話更有說服力的話。再次感謝。 – user1398478