如何获取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;
}
});
}
}
答
您可以把点击视图上方的背景视图。当您点击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>
答
使用FrameLayout而不是RelativeLayout。并将imageview的widht和height设置为match_parent。我确定它会正常工作。
你能详细描述一下mspaint中的图片吗?所以我们可以更好地理解和解决 –
你好,尼克,我编辑了我的问题,并把代码。 –
究竟你想做什么?你还没有清楚你想要什么。 –