Wednesday, September 19, 2012

Android: Swipe Image Viewer with ViewPager

A few months ago I complained that there is no standard widget for a swipe image viewer. I just asked about it on Twitter, and discovered that ViewPager is what I wanted.

I updated my github project with a ViewPager implementation, and it's so much simpler. On top of that, the images slide as your finger swipes across the screen, giving a much more satisfactory feedback.

11 comments:

Inline coding questions will not be answsered. Instead, ask on StackOverflow and put the link in the comment.

  1. hello Chiu-Ki Chan.:)
    your article is very great, it's very useful for me. i am glad, i can find your blog.

    but, there is something that i am still confused it when i see your project.
    i want to make swipe button like your project.
    so, how to make it? or i just change the imageview to image button?

    my project is make navigation for blind people in campus, and then i want to make HCI (Human Computer Interaction) with UI like your project, i want to add TTS (Text To Speech)service in every action. for example is when the image/button is swiped, and when the image/button is pressed.
    can you tell me how to add the TTS service?
    i am very glad if you help me.
    thank you Chiu-Ki Chan. :)

    ReplyDelete
    Replies
    1. No, the ViewPaper isn't suitable if you just want to recognize the swipe gesture on the button. You should implement a SimpleOnGestureListener instead. See the old code in my github for an example: https://github.com/chiuki/android-swipe-image-viewer/tree/ImageSwitcher. In particular, you will be setting the touch listener on your button instead of mImageSwitcher.

      Delete
    2. i am sorry Chiu-Ki Chan. i am too late to reply it again.

      i have solved my problem, and your article so help me to build my project.
      thank you very much Chiu-Ki Chan. :)
      you are so kind.

      may you always successful. GBU.

      Delete
  2. Hello Chiu-Ki Chan,
    I am new for Android development and find your blog by google. Your sample project is great but I have a question: Is it possible to expand it so that the user can zoom the image per 2 Fingers gesture like the modern gallery program in Android phone now?
    my email address: katrina@me3.de
    Thank you in advance!

    With best regards,
    Katrina

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hiii...how r u...
    Thanks for u r post....My requirement is giving the animation to the gallary images.the images are swipe from corner. the next image come back from first image with shadow...
    i hope u give reply
    my email--bramhanandareddym@gmail.com

    ReplyDelete
  5. Hi Chiu-Ki Chan,
    I am new in Android development, your sample is good, but i have a problem, I got many photo to view, when I start my activity, I want to go back to the last photo I had view, so, how can I save the current view position? and apply it like a bookmark. please advice. thanks.

    wongkinkok72@gmail.com

    ReplyDelete
    Replies
    1. You can set a OnPageChangeListener on the view pager and save the current page in onPageSelected(), then restore it with viewPager.setCurrentItem() later.

      Delete
  6. Great Solution! You really saved my day :)

    Thank you.

    ReplyDelete
  7. Hi Chiu-Ki Chan,
    Great help and good piece of code ! Thanks !

    ReplyDelete
  8. hi, just stumble upon your blog and got exactly code that i wanted, thanks :D

    ReplyDelete