ImageView Zoom and Scroll

Update: the source code has moved to github, so it’s easier for anyone to fork it!

As long as Android doesn’t have a built-in ImageView widget with zoom and scroll capabilities I tries to create one by myself starting from the google repository.

The result is pretty nice so I’m posting here the source code, if anyone is interested, or simply doesn’t want to waste the time creating a new one.

Here’s a sample code on how to use it in an Activity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package it.sephiroth.android.demo;

import it.sephiroth.android.library.imagezoom.ImageViewTouch;

import java.io.IOException;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class ImageZoomActivity extends Activity {
   
   private ImageViewTouch  mImageView;
   
   @Override
   protected void onCreate( Bundle savedInstanceState )
   {
      super.onCreate( savedInstanceState );
     
      requestWindowFeature( Window.FEATURE_NO_TITLE );
      setContentView( R.layout.main );
      getWindow().addFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN );
      selectRandomImage();
   }
   
   @Override
   public void onContentChanged()
   {
      super.onContentChanged();
      mImageView = (ImageViewTouch)findViewById( R.id.imageView1 );
   }
   
   /**
    * pick a random image from your library
    * and display it
    */

   public void selectRandomImage()
   {
      Cursor c = getContentResolver().query( Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null );
      if ( c != null ) {
         int count = c.getCount();
         int position = (int)( Math.random() * count );
         if ( c.moveToPosition( position ) ) {
            long id = c.getLong( c.getColumnIndex( Images.Media._ID ) );
            int orientation = c.getInt( c.getColumnIndex( Images.Media.ORIENTATION ) );
           
            Uri imageUri = Uri.parse( Images.Media.EXTERNAL_CONTENT_URI + "/" + id );
            Bitmap bitmap;
            try {
               bitmap = ImageLoader.loadFromUri( this, imageUri.toString(), 1024, 1024 );
               mImageView.setImageBitmapReset( bitmap, orientation, true );
            }
            catch ( IOException e ) {
               Toast.makeText( this, e.toString(), Toast.LENGTH_LONG ).show();
            }
         }
         c.close();
         c = null;
         return;
      }
   } 
}

 

 

Anyway, if you want to download the source, here is the eclipse source project:
http://blog.sephiroth.it/wp-…/ImageZoom.zip
https://github.com/sephiroth74/ImageViewZoom

  • Nishant Tiwari

    Hi.,

    I am using ImageViewTouch to implement a zoom in functionality in my app.

    What I am doing is following;

    I am overlaying the ImageViewTouch on the top of the Camera Surface.

    And when I double tap, I want to zoom in exactly 3x on my camera view and 3x equivalent on my ImageViewTouch.

    Looking at this code of yours;

    protected float onDoubleTapPost( float scale, float maxZoom ) {
    if ( mDoubleTapDirection == 1 ) {
    if ( ( scale + ( mScaleFactor * 2 ) ) <= maxZoom ) {
    mDoubleTapDirection = -1;
    float scaledata = scale + mScaleFactor;
    return 2.5f;//scale + mScaleFactor;
    } else {
    return maxZoom;
    }
    } else {
    mDoubleTapDirection = 1;
    return 1f;
    }
    }

    Can you suggest me if I have to zoom in exactly 3x how much should I return from here.

    Or should I look at different place for solving tis problem

  • Pingback: How to implement zoom effect for image view in android? | Developers Q & A()

  • Vijaya Srikara Das

    Where is easing and Cubic class..?? I am not able to compile..Please help me

  • Muhammad Omer

    Much appreciated work..
    Well done..
    Got my fix

  • Pingback: Trying to get a handle on displaying an image with zoom and pan | Stackforum.com()

  • Nabil Ait Brahim

    Where can I find the .jar ?

    • mobiledevstories

      import it using gradle if you are using Android Studio.

      If using Eclipse, import the library project as it’s indicated in the GitHub page

  • James Sijera

    sir where is the .jar file?

  • Pingback: Study and implement: ImageViewZoom(1) | +.゚(*-ω-)人(-ω-*)+.゚()

  • mobiledevstories

    Hi alessandro,

    first of all, thank you for the excellent lib. Am using it in two Apps with total success. Quite an excellent job :-)

    I’ve moved to Android Studio, and downloaded the gradle dependency (in the past, I used the “libproject” version on Eclipse), and looks like there is a lacking constructor:

    ImageViewTouch ivt = new ImageViewTouch(mContext); // where mContext is a Context (trivial)

    changing to

    ImageViewTouch ivt = new ImageView(mContext, null);

    compiles, but does not display images properly in a ViewPager.
    I see that in all your examples you retrieve the ImageViewTouch using findViewById + a static cast.

    Is it possible/recommended to use ImageViewTouch the way i propose?

    Thanks in advance

  • Achiles

    It works perfectly! Thanks so much!!