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

时间:2011-08-31 05:59:34

标签: android android-imageview

我在两个不同的布局中有两个ImageView,一个ImageView位于另一个ImageView上,而我正在使用RelativeLayout&amp;两个ImageView大小都是自动换行内容但问题是如果我点击ImageView2 Imageview2 Imageview1显示在ImageView1上,如果我点击Imageview1 Imageview2 <?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> 上显示的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; } }); } } 时间我使用的是前置方法,但这不起作用。对不起英语沟通不好。请帮助我。

先谢谢

以下是我的代码。

main.xml中: -

{{1}}

Java文件: -

{{1}}

2 个答案:

答案 0 :(得分:4)

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

 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>

答案 1 :(得分:1)

使用FrameLayout而不是RelativeLayout。并设置imageview的宽度和高度match_parent。在#39; m当然可以正常工作。