Umair Umair - 4 months ago 47
Android Question

Zooming a layout (Relative Layout) in android

I want to zoom in and out all the images inside the layout. I have seen many questions like here and here and tried to implement them but I couldn't.

Please help me in order to understand this. My confusion is, why would I extend my class from relative layout, as described in the question above, i mean i have a relative layout in my XML and I am setting this XML in content view but how could I grab the specific relative layout by using this methodology. And even if I have extended class from relative layout, how would I use this class in the activity which contains my images inside layout.

My XML setup is like this, can anyone please tell me what approach I have to follow.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cpec.farrukhtanveer.MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="130dp"
android:layout_marginRight="130dp"
android:background="@drawable/imageBackGround">

<ImageView
android:id="@+id/img_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:src="@drawable/imageone"/>

<ImageView
android:id="@+id/img_two"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="imagetwo"/>
</RelativeLayout>
</RelativeLayout>


Please help folks !
Thanks.
Umair

Answer

Since no one came up with solution, I kind of sort it out myself and thought it might be helpful for some one else who is stuck in this problem. The basic guidance came from here, where author implemented the zoom in and out for a single image, we just have to add images and handle other initialization as described in that tutorial.

ImageViewWithZoomClass:

private Drawable imageOne, imageTwo;
imageOne = context.getResources().getDrawable(R.drawable.icon1); 
imageTwo = context.getResources().getDrawable(R.drawable.icon2);
.
.
imageOne.setBounds(0, 0, imageOne.getIntrinsicWidth(), imageOne.getIntrinsicHeight());
imageTwo.setBounds(0, 0, imageTwo.getIntrinsicWidth(), imageTwo.getIntrinsicHeight());
.
.
imageOne.draw(canvas);
imageTwo.draw(canvas);

and since I am using relative layout, I just used onClickListener in my MainActivity like this

relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(new ImageViewWithZoom(getApplicationContext()));
        }
    });

and XML for the relative layout looks like this:

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="130dp"
    android:layout_marginRight="130dp"
    android:background="@drawable/abc"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    >

    <ImageView
        android:id="@+id/img_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/roadandrailways"
        android:visibility="invisible"/>

    <ImageView
        android:id="@+id/img_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/capital"
        android:visibility="invisible"/>
</RelativeLayout>

You can add multiple images in layout. Thats it, hope it helps.

Comments