Question

There are a lot of examples how to use ViewPager as an image slider, but I haven't seen any that handle the problem of correctly rotated images:

Now actually I have a solution for this, but the problem is that the image sliding is painfully slow when the image size is over about 3000*1500 pixel because of the extra rotation step:

The ImageView itself doesn't care about the correct orientation when setting an image via setImageURI or setImageBitmap.

This means you first have to find out the correct orientation yourself and then eventually do a

matrix.postRotate(rotation) 

and create a new bitmap using

Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                srcBitmap.getHeight(), matrix, true);

This additional bitmap creation really slows it down.

If the was a method to load an image "rotated on the fly" ? Sth. like BitmapFactory.decodeStream(stream,matrix) ?

I have also tried to increase the ViewPager.setOffscreenPageLimit value to 3, but with the result of "java.lang.OutofMemoryError: bitmap size exceeds VM budget" errors.

Somehow it must be possible because with the default "Gallery" app the sliding is really fast even with very large images...But I guess this not a trivial task ?

Was it helpful?

Solution

Finally I found the solution: to get a responsive, fast & fluid image sliding behaviour two things need to be done:

  1. Do the bitmap loading in the background using AsyncTask https://developer.android.com/training/displaying-bitmaps/process-bitmap.html

  2. Load a downsampled version of the bitmap using BitmapFactory.Options.inSampleSize (calculated using the ratio between ViewPager's and the Bitmap's dimensions)

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top