ImageView Zoom and Scroll

cheap essay writing online

http://www.fiascoultimate.com/mechanical-engineering-phd-thesis/ mechanical engineering phd thesis chemistry aleks help Update: the source code has moved to github, so it’s easier for anyone to fork it!

womenra le viagra pour femme 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.

watson-glaser critical thinking appraisal short form answers 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.

http://www.wonderlandparty.it/online-essay-revision/ online essay revision 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;
      }
   } 
}

http://stjohns.lambdaphiepsilon.com/autodesk-navisworks-review-2009/ autodesk navisworks review 2009  

http://aaequipment.net/?custom-resume-writing-usa custom resume writing usa  

http://www.surgeskateboard.com/phd-thesis-eu-law/ phd thesis eu law 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

  • Hi!

    best viagra prices 100mg great library! Thanks a lot!

    who selling viagra at a discount I noticed this library wasn’t working on Android 2.1, because of the API level (8) for the gesture recognizers.

    ghostwriter xp I ported the classes by downloading the source code from http://grepcode.com/search?query=ScaleGestureDetector

    essay writing service wiki The following classed need to be added to your library:
    – ScaleGestureDetector
    – ViewConfiguration
    – GestureDetector

    customessaystation com By adjusting some constants (like MotionEvent.ACTION_POINTER_INDEX_SHIFT => MotionEvent.ACTION_POINTER_ID_SHIFT,…) everything works like a charm!

    here Grtz
    Cliff

  • Eyal

    view How can I avoid the automaticly scale down of the image?

  • Eyal

    http://colostrumnutrient.com/?p=write-my-college-essay-me How can I use “wrap_content” for hight and width of ImageViewTouch?

  • Vineet Billorey
  • http://vcc.org.nz/dissertation-editing-help-uk/ Hey thanks a lot…Very helpful tutorial…

  • Ragummrsa

    next Very very good job. its working fine but one problem that is I have some images in custom gallery view it display when i use ur custom image view to adapter class the gallery view scrolling not working. only one image was displayed. how to implement to scroll the gallery view. please share ur ideas with me. I’m new one for android and mobile technologies.

  • Bruno

    http://www.aftlv.com/write-my-homework-chper/ write my homework chper Hy, I’m using Android 3.0 and trying to use your library, but as soon as I tap the image with two fingers, it simply disappears… any idea on what is going on?

  • Dhaiwat Bhavsar

    http://bsqmgroup.com/?p=examples-of-dissertations hi can you provide me direction how to run on 2.1 plz

    • Ragummrsa

      sorry i think i cant run 2.1 it should only run from 2.2 and above. because ScaleGestureDetector.SimpleOnScaleGestureListener support only 2.2 and above..

  • Lee LeOn

    wow, that is great!!

  • Lee LeOn

    I modify the ImageViewTouch.java code for android 2.1 and success work it.

    package it.sephiroth.android.library.imagezoom;

    import android.content.Context;
    import android.graphics.PointF;
    import android.util.AttributeSet;
    import android.util.FloatMath;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.view.ViewConfiguration;

    public class ImageViewTouch extends ImageViewTouchBase{

    protected static final float MIN_DIST = 10F;

    static final float MIN_ZOOM = 0.9f;
    protected GestureDetector mGestureDetector;
    protected int mTouchSlop;
    protected float mCurrentScaleFactor;
    protected float mScaleFactor;
    protected int mDoubleTapDirection;
    protected GestureListener mGestureListener;

    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist;

    private final static int NONE = 0;
    private final static int DRAG = 1;
    private final static int ZOOM = 2;

    private int touchState = NONE;

    public ImageViewTouch( Context context, AttributeSet attrs )
    {
    super( context, attrs );
    }

    @Override
    protected void init()
    {
    super.init();
    mTouchSlop = ViewConfiguration.getTouchSlop();
    mGestureListener = new GestureListener();

    mGestureDetector = new GestureDetector(getContext(), mGestureListener);
    mCurrentScaleFactor = 1f;
    mDoubleTapDirection = 1;
    }

    @Override
    public void setImageRotateBitmapReset( RotateBitmap bitmap, boolean reset )
    {
    super.setImageRotateBitmapReset( bitmap, reset );
    mScaleFactor = getMaxZoom() / 3;
    }

    @Override
    public boolean onTouchEvent( MotionEvent event )
    {
    if (touchState != ZOOM) mGestureDetector.onTouchEvent( event );
    int action = event.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
    start.set(event.getX(), event.getY());
    touchState = DRAG;
    break;
    case MotionEvent.ACTION_UP:
    touchState = NONE;
    if ( getScale() MIN_DIST) {
    touchState = ZOOM;
    midPoint(mid, event);
    }
    break;
    case MotionEvent.ACTION_MOVE:
    if (touchState == ZOOM) {
    onScale (event);
    }
    break;
    }
    return true;
    }

    // calculate the distance
    private float spacing(MotionEvent event) {
    float x = event.getX(0) – event.getX(1);
    float y = event.getY(0) – event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
    }

    // calculate the center point
    private void midPoint(PointF point, MotionEvent event) {
    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    point.set(x / 2, y / 2);
    }

    public boolean onScale( MotionEvent event )
    {
    float span = spacing(event);
    float targetScale = span / oldDist;
    if (span > MIN_DIST) {
    targetScale = Math.min( getMaxZoom(), Math.max( targetScale, MIN_ZOOM ) );
    zoomTo( targetScale, mid.x, mid.y);
    mCurrentScaleFactor = Math.min( getMaxZoom(), Math.max( targetScale, MIN_ZOOM ) );
    mDoubleTapDirection = 1;
    invalidate();
    return true;
    }
    return false;
    }

    @Override
    protected void onZoom( float scale )
    {
    super.onZoom( scale );
    mCurrentScaleFactor = scale;
    }

    protected float onDoubleTapPost( float scale, float maxZoom )
    {
    if ( mDoubleTapDirection == 1 ) {
    if ( ( scale + ( mScaleFactor * 2 ) ) 800 || Math.abs( velocityY ) > 800 ) {
    scrollBy( diffX / 2, diffY / 2, 300 );
    invalidate();
    }
    return super.onFling( e1, e2, velocityX, velocityY );
    }
    }
    }

    • hey can you explain me.i am using viewfliper and i want zoom and fling both option?so is it possible in android?

      • Robby Ali Darmawan

        there is the answer? I have same issue for your question. please answer.. thx

  • Lee LeOn

    I found a more simple method to solve the problem in android2.1 . We can copy the ScaleGestureDetector.java, we can found it in android 2.2 open source($ANDROID_SRE/frameworks/base/core/java/android/view/ScaleGestureDetector.java), to our project.And modify some attributes.~~ ^^

  • Dynde

    I have the same problem as bruno with android 3.x. Image displays fine, but as soon as I double touch it, the image disappears?

    • Dynde

      I figured it out – you need to use the setImageBitmapReset methods, so it sets all the variables (like maxzoom etc.) – I wonder why the regular imageview method “setImageBitmap” wasn’t overriden, to make sure it always goes through the reset?

  • How can I modify this to load image from URL of the website??

    • Never mind, I’ve figured it out.

      • M040980

        You got any sample code how to load the image from url? I can’t figure it out.

      • rin

        how? i don’t have idea how to modify this to load image from url of the website.
        please, share with us

  • Sisthein

    really cool library. but now i want to draw a circle or rectangle or what ever on a double tap. so i put a canvas around the member variable mBitmap inside the RotateBitmap class. i added a getter for the canvas and tried to draw a circle on the canvas in the method onDoubleTapPost() in the class ImageViewTouch. but the circle isnt visible ๐Ÿ™

    any idea what is wrong?

  • Pingback: » ZoomGallery for Android ยป El blog de Munix()

  • Rolf

    How can I use this library in a TabHost? I liked to put in each tab another picture

  • Arifandalusia

    Hello .how do I change the select random image to the image that i want. please give some snippet code. tq

  • Anonymous

    Good article!

  • Tek Yin Kwee

    Hi, how to make the bitmap automatically fitwidth at first load?

  • Haddellp

    This is a great example and works perfectly. I was just wondering how you change it from a random picture to a pre-determined picture in the drawable folder?

    Many Thnaks

  • IC92

    hai..
    thanks for your code..
    may i use this on my project?
    my project is such a maps application, which based on a picture file..
    so i need that picture zoom in and out

    • Yes indeed

      • ic92

        hai..
        thanks for the code,, it is work good!!
        however, i got problem when loading image, i change your code arround so i can load the image that i want based on location that i give. the picture is in 300 dpi, png format. it is always succes for the first load. however, when i try to point to another file, it is crashed and say “java.lang.OutOfMemoryError: bitmap size exceeds VM budget.”
        here is my code to load the image:

        private Bitmap loadFile(String region, String name) {
        FileInputStream fis;
        BufferedInputStream Stream;
        Bitmap bmp = null;

        String file = Environment.getExternalStorageDirectory()+”/region+”/”+name;
        try {
        fis = new FileInputStream(file);
        Stream = new BufferedInputStream(fis,16384);
        bmp = BitmapFactory.decodeStream(Stream);
        }
        catch (FileNotFoundException e) {
        e.printStackTrace();
        }
        return bmp;

        }

        do you have any suggestion?

  • Anonymous

    hai……….
    i am getting just blank screen and how i need to import my drawable image.

    • Zhiqiao Lin

      The demo imports from your gallery and if your gallery is empty it just shows a blank screen

  • thanks a lot. it is useful

  • Janu Manu Hu 11

    Hi……….
    i am getting just blank screen… ๐Ÿ™

  • rahu

    Good Job Man

  • Thanks you very much, your code help me a lot
    Take a look what app i have done with this amazing ‘it.sephiroth.android.library.imagezoom.ImageViewTouch’
    http://www.miiroad.com/?a=184 ^^

  • Tofeeq Ahmed

    Nice example..But can you tell me if i zoom a image then want to scroll it.So how can i know that image end comes now?

    I have also a zoom article on my blog see this if it help some one.
    http://androidtrainningcenter.blogspot.in/2012/04/pinching-zoom-in-android-image-view-or.html

  • Fock

    Hi.
    I’ve created an ImageViewTouch in my main.xml with the attributes to load my image and it seems to work.
    I’ve got a “NullPointerException” at it.sephiroth.android.library.imagezoom.ImageViewTouchBase.updateRect(ImageViewTouchBase.java:327)
    after 5 sec, when I try to move or zoom the image.

    What’s the problem ?

  • Rajg Naga

    Please explain me more on the following.
    I have 3 images.
    1.on double zoom the image.
    2. Provide scroll in the zoomed image
    3.move to the next image once it crosses boundary
    4.set thumnail accordingly.

    I am very new to programming in Android. I saw all your posts but not getting working project.

    And more over it shows the balnk screen for the project added above on running.

    Please save me.

    Thanks in advance.

  • Nice tutorial man , It is working fine

  • Maycon Fernando Silva Brito

    Guy, thank you very, very, very much. You really help me .

  • Bhavani Vishnu

    great library! Thanks a lot!

    Is there any Licensing associated with this library, because we want to use in one of our projects.

  • Bugm5

    How we could combine this with ViewPager … I tried, but then when I try to pan/scroll a zoomed in image, viewPager understands this as a swipe and moves to next image?

  • Pramod Kumar

    how to implement this zooming code in image gallery or slideshow of images in a project

  • Pramod Kumar

    It takes the images which are in the Gallery .i want to give some images in drawable folder and that images has to be displayed as slide show with zoom plz help me

  • Tsukiomiya

    can u give me the source code (full)
    because i can’t fix my problem
    i need this code
    thanks

  • Nitin

    Thanks alot. It really helped.

  • Renaud

    Hello !
    is it possible to add POI on the image ? I need something alike to do some kind of map.
    thx !

  • Renaud

    Hello !
    is it possible to add POI on the image ? I need something alike to do some kind of map.
    thx !

  • Pingback: how to zoom in and zoom out the images in gallery | Android Development tutorial | Android Development tutorial()

  • Pingback: Step-by-step of how to add pinch and double-tap zoom to ImageView « Darren's Blog()

  • sujit

    Hi …great work..but i have one problem. I am not able to increase the zoom factor. It looks cool in small devices but in big screen it the image does accept pinch zoom. can you please tell how to increase the default zoom factor.

  • M. Konermann

    Hello,

    can i use that library in my project? Is there any licence for this source code?

  • Developer

    You the Man !

  • pratik gavas

    how to change the name of image file which is currently capture and store in php server

  • Muhammad Irfan

    Hi every one. i want to add this image view programatically.. kindly help me

  • Is your lib support multi touch ????? zoom image with 2 finger???

  • Pingback: How to zoom image on gesture and double tap in android - How-To Video()

  • Shilpa

    I changed the direction in mainx.xml to ‘leftToRight’ and it did not work. Did anyone else face the same issue?

    • Shilpa

      Fixed the issue:
      In onTouchEvent (), the calculation of c4 if (mInvert = true i.e. ORIENTATION_TTB or ORIENTATION_LTR)

      changed from:
      c4 = ( !mExpanded && handleLeft > mTopOffset + mHandleWidth + mTapThreshold );

      to:

      c4 = ( !mExpanded && handleLeft < mTopOffset + mHandleWidth + mTapThreshold );

  • aij

    would it work if i use eclipse??