如何获取ImageView带来ImageView的触摸事件?

问题描述:

我有两个二ImageView不同的布局中,一个ImageView是另一个ImageView,我使用RelativeLayout &两个ImageView大小包裹的内容,但如果我上ImageView2单击时间Imageview2显示在Imageview1和问题如果我点击ImageView1,那么Imageview1会显示在Imageview2上我正在使用引导式方法,但这不起作用。对不起英文交流。请帮帮我。如何获取ImageView带来ImageView的触摸事件?

在此先感谢

以下是我的代码。

的main.xml: -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <RelativeLayout android:layout_width="fill_parent" android:id="@+id/mRlayout1" 
     android:layout_height="fill_parent"> 
     <ImageView android:layout_width="wrap_content" 
      android:scaleType="matrix" android:layout_height="wrap_content" 
      android:id="@+id/mImageView1" android:src="@drawable/icon1" /> 
    </RelativeLayout> 
    <RelativeLayout android:layout_width="fill_parent" android:id="@+id/mRlayout2" 
     android:layout_height="fill_parent" > 
     <ImageView android:layout_width="wrap_content" 
      android:scaleType="matrix" android:layout_height="wrap_content" 
      android:id="@+id/mImageView2" android:src="@drawable/icon" /> 
    </RelativeLayout> 
</RelativeLayout> 

Java文件: -

public class BodyTemp extends Activity { 
    ImageView mImageView1, mImageView2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mImageView1 = (ImageView) findViewById(R.id.mImageView1); 
     mImageView1.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       mImageView1.bringToFront(); 
       return true; 
      } 
     }); 
     mImageView2 = (ImageView) findViewById(R.id.mImageView2); 
     mImageView2.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       mImageView2.bringToFront(); 
       return true; 
      } 
     }); 
    } 
} 
+1

你能详细描述一下mspaint中的图片吗?所以我们可以更好地理解和解决 –

+0

你好,尼克,我编辑了我的问题,并把代码。 –

+0

究竟你想做什么?你还没有清楚你想要什么。 –

您可以把点击视图上方的背景视图。当您点击mImageView1时,将mImageView2置于前面,反之亦然。

mImageView2 = (ImageView) findViewById(R.id.mImageView2); 
mImageView1.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      mImageView2.bringToFront(); 
      return true; 
     } 
    }); 
    mImageView2.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      mImageView1.bringToFront(); 
      return true; 
     } 
    }); 

也改变布局。将图像放入单个相关布局中。否则bringToFront将不起作用。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent"> 

    <ImageView android:layout_width="wrap_content" 
     android:scaleType="matrix" android:layout_height="wrap_content" 
     android:id="@+id/mImageView1" android:src="@drawable/icon1" /> 

    <ImageView android:layout_width="wrap_content" 
     android:scaleType="matrix" android:layout_height="wrap_content" 
     android:id="@+id/mImageView2" android:src="@drawable/icon" /> 
</RelativeLayout> 
+0

我使用了这段代码,但是如果你有任何其他的答案或链接,请告诉我这段代码不工作。 –

+0

我觉得你用这段代码非常接近它。试一试。 – Ronnie

+0

设置onclicklisteners而不是ontouch。 – Ronnie

使用FrameLayout而不是RelativeLayout。并将imageview的widht和height设置为match_parent。我确定它会正常工作。