Saturday, 6 April 2013

Update Pinch to zoom example and tutorial for android ImageView with zoom limit

I wrote on the same topic Pinch to zoom in android ImageView while i just started blogging. And that still is my one of the popular article, despite that it has many complication and limitation with it. So i am working to improve it. After reading most comment, I feel some problems while implementing pinch to zoom with android ImageView using previous post
  • How to implement that pinch to zoom in their project? 
  • Determining Zoom limit in pinch to zoom ImageView! 
  • Does not work properly as i told in post !! 

So now i come up with a solution in which every drawback avoided and pinch to zoom image in android seems quite simple and work perfect
Follows thes easy steps

1) Create a new project and Download this library , add this Jar to libs folder of your project 

2) Create one layout with one ImageView (Which need to be zoom)

3) Create one Activity and change to following code. This will handle all attributes required for pinch to zoom ImageView


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.imagezoom.ImageAttacher;
import com.imagezoom.ImageAttacher.OnMatrixChangedListener;
import com.imagezoom.ImageAttacher.OnPhotoTapListener;

public class SampleZoom extends Activity {
    ImageView mImaView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pinch_sample);
        mImaView = (ImageView) findViewById(R.id.simple);

        Bitmap bimtBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.a);
        mImaView.setImageBitmap(bimtBitmap);

        /**
         * Use Simple ImageView
         */
        usingSimpleImage(mImaView);

    }

    public void usingSimpleImage(ImageView imageView) {
        ImageAttacher mAttacher = new ImageAttacher(imageView);
        ImageAttacher.MAX_ZOOM = 2.0f; // Double the current Size
        ImageAttacher.MIN_ZOOM = 0.5f; // Half the current Size
        MatrixChangeListener mMaListener = new MatrixChangeListener();
        mAttacher.setOnMatrixChangeListener(mMaListener);
        PhotoTapListener mPhotoTap = new PhotoTapListener();
        mAttacher.setOnPhotoTapListener(mPhotoTap);
    }

    private class PhotoTapListener implements OnPhotoTapListener {

        @Override
        public void onPhotoTap(View view, float x, float y) {
        }
    }

    private class MatrixChangeListener implements OnMatrixChangedListener {

        @Override
        public void onMatrixChanged(RectF rect) {

        }
    }
}

Implementing Zoom Limit. 


See inside function usingSimpleImage(), below two line decide zoom limit ImageAttacher.MAX_ZOOM = 2.0f;  // Double the current Size ImageAttacher.MIN_ZOOM = 0.5f; // Half the current Size

Note : Work perfect for the image with fit width (either in Landscape or portrait)
Android News and source code