Android ViewPager Lag

前端 未结 11 1874
迷失自我
迷失自我 2020-12-28 19:55

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 (

相关标签:
11条回答
  • 2020-12-28 20:18

    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/drawablesfolder. 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

    0 讨论(0)
  • 2020-12-28 20:18

    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;
    
    0 讨论(0)
  • 2020-12-28 20:18

    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

    0 讨论(0)
  • 2020-12-28 20:19

    Instead of moving to res/drawable-hdpi I moved the images to res/drawable-xxxhdpi. This instantly solved the problem.

    0 讨论(0)
  • 2020-12-28 20:27

    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.

    0 讨论(0)
  • 2020-12-28 20:34

    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.

    0 讨论(0)
提交回复
热议问题