我有两个ImageButton
s,我想为它们设置相同的OnTouchListener
,但问题是 - 当我触摸一个Image时,另一个相邻的Image也在移动。 我也觉得,当我触摸图片2时,它正在移出屏幕并隐形。我希望它保持在屏幕上,并希望Touch事件应该为他们单独工作。On Touch Listener无法正常工作
这是我到现在为止所做的。
//this is the snippet from main activity where the onTouch is implemented and layout is created
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
imgMaster.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, imgMaster.getId());
imgMaster.setOnTouchListener(new ChoiceTouchListener());
imgMood.setOnTouchListener(new ChoiceTouchListener());
imgMood.setLayoutParams(params);
imgMaster.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Clicked Master", Toast.LENGTH_SHORT).show();
}
});
imgMood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Clicked Mood", Toast.LENGTH_SHORT).show();
}
});
}
private final class ChoiceTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent event) {
//ImageButton views = (ImageButton) view;
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (view.getId()) {
case R.id.master:
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;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
case R.id.mood:
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;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
break;
}
return false;
}
}
//这是我提前布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_root"
android:background="@drawable/transparent_background"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mood"
android:src="@drawable/mood"
android:background="@null"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/master"
android:src="@drawable/master"
android:background="@null" /></RelativeLayout>
感谢。
这些图像是否重叠在一起?我问这是因为你使用了相对布局,并没有提到每个图像相对于彼此的相对位置 – Dibzmania
@Dibzmania但添加规则已经定义在代码中的位置 –
@Dibzmania可能是它的重叠...但如果我设置类似于在布局android:layout_alignParentBottom =“true” android:layout_alignParentRight =“true” android:layout_alignParentEnd =“true”图像它没有做任何改变.... –