开发者

How to Get ImageView Bring to front on touch event of ImageView?

I have two ImageView in two different layouts, the One ImageView is on another ImageView and I am using RelativeLayout & the Both ImageView size is Wrap Content But the problem is if I click on ImageView2 that time the Imageview2 is displayed on the Imageview1 and if I click on ImageView1 that time the Imageview1 is displayed on the Imageview2 I am using bring-to-front method but this is not working. Sorry for bad English Communication. Please Help Me.

Thanks in Advance

Following is My Code.

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="开发者_JAVA技巧matrix" android:layout_height="wrap_content"
            android:id="@+id/mImageView2" android:src="@drawable/icon" />
    </RelativeLayout>
</RelativeLayout>

Java File:-

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;
            }
        });
    }
}


You can bring the background view above the clicked view. Bring mImageView2 to front when you get a click on mImageView1 and vice versa.

 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;
        }
    });

Also change the layout. Put the imageviews into a single relativelayout. Otherwise bringToFront will not work.

<?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>


Use FrameLayout Instead of RelativeLayout. And Set widht and height of imageview to match_parent. I'm Sure it will work Fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜