Hello I have a viewpager with several pages(using a fragment state pager), and some pngs as backgrounds to those pages. I already followed the Displaying Bitmaps in the Ui (
I had similar problem.
I am showing tutorial-like pages. Each page is a full screen jpg.
At first, I put the pictures in res/drawables
folder. The viewpager is very laggy when swiping it.
Then I move those jpgs to
res/drawable-hdpi
folder, the lag is gone.
I think different optimisations are done on the pictures based on folder. So we cannot put everything in res/drawable
folder
Building up on @coderek answer I seem to have solved this by fetching bitmaps without scaling them for specific densities:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resource, opts);
((ImageView) view).setImageBitmap(bitmap);
Using an image from drawable
folder in an xxhdpi
device resulted in up to 8 times more memory being allocated!
P.S.:
You can also down sample the drawable if the resolution is too big:
opts.inSampleSize = 2;
I had a similar problem too and, in my case, the lagging was caused by the background picture of the layouts which was too large. I just resized the picture and the swiping lag was gone
Instead of moving to res/drawable-hdpi I moved the images to res/drawable-xxxhdpi. This instantly solved the problem.
I followed the answer of Coderek and moved it between the different
res/drawable-hdpi folders
drawable-xxxhdpi is what worked best for my project.
You may want to try viewPager.setOffScreenLimit(size) to the number of your pages. This will load all the fragments once and keep from reloading them while swiping.