lisovaccaro
lisovaccaro

Reputation: 33956

Android how to do image screen slide?

I'm building a very basic gallery on android, it shows the last image on the camera folder and the user can slide left to see the previous one.

I finished an implementation using a viewpager, and a pagerAdapter using sampleSize to scale images. My problem is that the implementation is nowhere as efficient as the default gallery app, every time you slide an image you have to wait around 200ms for the next one to load. So basically my idea on how to implement it is not working out.

How can I do this efficiently, and if possible with an implementation that allows zooming in and out later on?


This looks promising but I don't know how to implement it with files instead of drawables http://www.androidviews.net/2012/11/photoview/


EDIT:

I've managed to improve speed a bit using photoview (a hacked viewpager), however it takes too much to convert the filepaths to bitmaps or drawables.

I have tried these two methods suggested by dilix and iDroid Explorer:

// Method A
Drawable d = Drawable.createFromPath(imagePath);
imageView.setImageDrawable(d);

// Method B
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(myBitmap);

But both produce error Bitmap too large to be uploaded into a texture. I need to get the image to the imageView as fast as possible and somehow bypass this error and maybe I'll get a decent speed. Right now I'm scaling the images and converting them to a drawable but it's not very efficient.

Upvotes: 15

Views: 5709

Answers (4)

dilix
dilix

Reputation: 3893

1)

This looks promising but I don't know how to implement it with files instead of drawables http://www.androidviews.net/2012/11/photoview/

You have to store not list of drawables in you adapter, but links to files on your sd card and then load it.

2) But i can imagine that file instead of drawables won't be so efficient because to load from files you have to parse it from sd card.

I think you can save time by loading several images in your memory simultaneously (compressed) and when you swype to your othe image you'll already have the image and memory and while swyping you can start loading other image in memory.

When fling over the gallery you may not load images and load it after almost stopping.

It's the main problem: memory consuming vs performance.

UPDATE

I think better than exploring this by yourself will be exploring existing code of google gallery: https://github.com/CyanogenMod/android_packages_apps_Gallery

As i checked - they store cache (almost as i've said about loading multiple bitmaps) and store only resized images - seems to be efficient.

Upvotes: 1

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

I am not sure whether you got correct solution or not. But if you want to continue with your own implementation with your given link then:

http://www.androidviews.net/2012/11/photoview/   //this link is given in your question

And you can see this SO for how to convert file's path in to the drawable. Now, you get drawable of your specified file and implement on the link you have given.

I think that link will works nice.

Feel free to comments.

Upvotes: 1

ndw
ndw

Reputation: 513

See the official documentation for a great example that will walk you through using bitmaps with a viewpager efficiently. You should use a cache of the images that have already been loaded, and the images should be loaded outside of the UI thread. You also want to scale the image to the minimum size needed for your display (usually powers of two - note that the image won't always scale to what you ask it to!) ViewPager also loads images in the background to prepare for the next slide, which is set with the setOffScreenPageLimit method (the default is usually good, I think it's 2.)

https://developer.android.com/training/displaying-bitmaps/index.html

Upvotes: 1

Naveen
Naveen

Reputation: 1703

Check this awesome website : http://www.androidviews.net/ and maybe this is what you want: http://www.androidviews.net/2012/11/photoview/

It has page scroll, zooming and panning as you need I have used it in my project with some modifications and lazy image loading. It is working exactly as you need.

Upvotes: 3

Related Questions