Java 触控式侦听器未按预期工作

Java 触控式侦听器未按预期工作,java,android,android-relativelayout,imagebutton,ontouchlistener,Java,Android,Android Relativelayout,Imagebutton,Ontouchlistener,我有两个ImageButtons,我想为这两个按钮设置相同的OnTouchListener,但问题是-当我触摸一个图像时,另一个相邻的图像也在移动。 我还觉得,当我触摸图像2时,它正在移出屏幕,变得不可见。我希望它保持在屏幕上,并希望触摸事件应该为他们单独工作 这是我到现在为止所做的 //这是来自主要活动的片段,其中实现了onTouch并创建了布局 RelativeLayout.LayoutParams params=新的RelativeLayout.LayoutParams(RelativeL

我有两个
ImageButton
s,我想为这两个按钮设置相同的
OnTouchListener
,但问题是-当我触摸一个图像时,另一个相邻的图像也在移动。 我还觉得,当我触摸图像2时,它正在移出屏幕,变得不可见。我希望它保持在屏幕上,并希望触摸事件应该为他们单独工作

这是我到现在为止所做的

//这是来自主要活动的片段,其中实现了onTouch并创建了布局
RelativeLayout.LayoutParams params=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
imgMaster.setLayoutParams(params);
params=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容);
params.addRule(RelativeLayout.RIGHT_OF,imgMaster.getId());
setOnTouchListener(新选项touchlistener());
setOnTouchListener(新选项touchlistener());
imgMood.setLayoutParams(params);
imgMaster.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(getApplicationContext(),“单击主控”,Toast.LENGTH\u SHORT.show();
}
});
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(getApplicationContext(),“单击的情绪”,Toast.LENGTH\u SHORT.show();
}
});
}
私有最终类ChoiceTouchListener实现OnTouchListener{
公共布尔onTouch(视图、运动事件){
//ImageButton视图=(ImageButton)视图;
final int X=(int)event.getRawX();
final int Y=(int)event.getRawY();
开关(view.getId()){
案例R.id.master:
开关(event.getAction()&MotionEvent.ACTION\u掩码){
case MotionEvent.ACTION\u DOWN:
RelativeLayout.LayoutParams lparams=(RelativeLayout.LayoutParams)视图。getLayoutParams();
_xDelta=X-lparams.leftMargin;
_yDelta=Y-lparams.topMargin;
打破
case MotionEvent.ACTION\u UP:
打破
case MotionEvent.ACTION\u指针\u向下:
打破
case MotionEvent.ACTION\u指针\u向上:
打破
case MotionEvent.ACTION\u移动:
RelativeLayout.LayoutParams LayoutParams=(RelativeLayout.LayoutParams)视图。getLayoutParams();
layoutParams.leftMargin=X-xDelta;
layoutParams.topMargin=Y-_yDelta;
layoutParams.rightMargin=-250;
layoutParams.bottomMargin=-250;
view.setLayoutParams(layoutParams);
打破
}
案例R.id.mood:
开关(event.getAction()&MotionEvent.ACTION\u掩码){
case MotionEvent.ACTION\u DOWN:
RelativeLayout.LayoutParams lparams=(RelativeLayout.LayoutParams)视图。getLayoutParams();
_xDelta=X-lparams.leftMargin;
_yDelta=Y-lparams.topMargin;
打破
case MotionEvent.ACTION\u UP:
打破
case MotionEvent.ACTION\u指针\u向下:
打破
case MotionEvent.ACTION\u指针\u向上:
打破
case MotionEvent.ACTION\u移动:
RelativeLayout.LayoutParams LayoutParams=(RelativeLayout.LayoutParams)视图。getLayoutParams();
layoutParams.leftMargin=X-xDelta;
layoutParams.topMargin=Y-_yDelta;
layoutParams.rightMargin=-250;
layoutParams.bottomMargin=-250;
view.setLayoutParams(layoutParams);
打破
}
打破
}
返回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>


提前感谢。

当您要求代码时,代码在这里,但我仍然不知道您希望对象的实际运动是什么

我提供的代码允许您将两个对象拖动到屏幕上任意位置,而无需移动另一个对象。

希望这是你想要的(至少可能更接近)

注意:请删除所有
RelativeLayout.addRule()
等,只需将onTouch侦听器设置为您的图像按钮,下面是onTouch的定义

private final class ChoiceTouchListener implements View.OnTouchListener {
        public boolean onTouch(View view, MotionEvent event) {
            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:
                            break;
                        case MotionEvent.ACTION_UP:
                            break;
                        case MotionEvent.ACTION_POINTER_DOWN:
                            break;
                        case MotionEvent.ACTION_POINTER_UP:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            view.setX(X-view.getWidth()/2);
                            view.setY(Y-view.getHeight());
                            break;
                    }
                case R.id.mood:
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_UP:
                            break;
                        case MotionEvent.ACTION_POINTER_DOWN:
                            break;
                        case MotionEvent.ACTION_POINTER_UP:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            view.setX(X-view.getWidth()/2);
                            view.setY(Y-view.getHeight());
                            break;
                    }
                    break;
            }
                return false;

        }
这应该是XML中的布局

<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/master"
        android:src="@drawable/master"
        android:background="@null"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mood"
        android:src="@drawable/mood"
        android:background="@null"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/master"
        android:layout_toEndOf="@+id/master" />
</RelativeLayout>


这些图像一开始是否相互重叠?我这样问是因为你使用了相对布局,没有提到每个图像相对于每个图像的相对位置other@Dibzmania但已为中的位置定义了添加规则code@Dibzmania可能是重叠…但如果我的设置与布局中的android类似:layout\u alignParentBottom=“true”android:layout\u alignParentRight=“true”android:layout\u alignParentEnd=“true”对于图像,它没有做任何更改……问题可能只存在于添加规则中,因为当我在添加规则()中将位置添加为左侧时,右侧的相邻问题就出现了感谢工作完美您也可以投票支持我的答案,如果你认为我真的付出了一些努力,谢谢:)