Ahamed Salik
Ahamed Salik

Reputation: 313

How to zoom an ImageView when click a button in android

I have a requirement where I have an ImageView and two buttons for zoom-in and zoom-out. I want to zoom the image when I click the button. I searched the google and StackOverFlow, but unfortunately unable to found a solution.

Therefore can someone help me? If you can give me a sample code or link that would be very much appriciated.

Here is the code which I am trying.

Main.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/imgCamera"
        android:layout_weight="1"
        android:scaleType="fitXY"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:src="@drawable/matterhorn" />

     <Button
        android:id="@+id/btnZoomIn"
        android:layout_width="65dp"
        android:layout_height="35dp"
        android:layout_gravity="bottom|left"
        android:layout_marginLeft="260dp"
        android:layout_marginBottom="15dp"
        android:layout_weight="1"
        android:text="Zoom In"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/btn_icon_zoom_in"
        >
    </Button>

    <Button
        android:id="@+id/btnZoomOut"
        android:layout_width="65dp"
        android:layout_height="35dp"
        android:layout_gravity="bottom|left"
        android:layout_marginLeft="330dp"
        android:layout_marginBottom="15dp"
        android:layout_weight="1"
        android:text="Zoom Out"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/btn_icon_zoom_out"
        >
    </Button>
</merge>

This is my main Activity "ZoomActivity.java" in which the event will be handle

package com.image.zoomcontrol;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class ZoomActivity extends Activity implements OnClickListener {

    private ImageView imgview;
    private Button btnZoomIn;
    private Button btnZoomOut;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imgview = (ImageView) findViewById(R.id.imgView);

        btnZoomIn = (Button) findViewById(R.id.btnZoomIn);
        btnZoomIn.setOnClickListener(this);

        btnZoomOut = (Button) findViewById(R.id.btnZoomOut);
        btnZoomOut.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnZoomIn:
            // Here I need Zoom in Code
            break;

        case R.id.btnZoomOut:
            // Here I need Zoom out Code
            break;
        }
    }
}

Thanks in advance.

Upvotes: 1

Views: 8581

Answers (3)

bhumika rijiya
bhumika rijiya

Reputation: 440

Try this code for Zoom in and Zoom out of ImageView,

Put this in main.xml :

<ZoomControls
        android:id="@+id/zoomControls1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp" />

In MainActivity.java :

 zoom = (ZoomControls) findViewById(R.id.zoomControls1);
     zoom.setOnZoomInClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                float x = img.getScaleX();
                float y = img.getScaleY();

                img.setScaleX((float) (x+1));
                img.setScaleY((float) (y+1));
            }
        });

            zoom.setOnZoomOutClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                float x = img.getScaleX();
                float y = img.getScaleY();

                img.setScaleX((float) (x-1));
                img.setScaleY((float) (y-1));
            }
        });

I wish it will help you..

Upvotes: 0

r4jiv007
r4jiv007

Reputation: 3114

basic animation tutorial is available on official developer site :- Click Here

Update :-

these links are not exactly for android but worth having a look :-

large_image_scrolling_using_low_level_touch_events

zoom-and-pan-large-images-with-google-map-interface

photo-zoom

Upvotes: 0

WISHY
WISHY

Reputation: 11999

try using zoomcontrol available in the advanced tab of activity xml

Upvotes: 0

Related Questions