Android ViewPager Lag

前端 未结 11 1875
迷失自我
迷失自我 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:34

    None of above solutions worked in my case!

    I found a library which works around the issue, and additionally incorporates few more interesting features. Take a look at it here!

    Try this:

    In gradle

    compile 'com.ToxicBakery.viewpager.transforms:view-pager-transforms:1.2.32@aar'
    

    onCreate

    viewPager.setPageTransformer(true, new DefaultTransformer());
    

    You can get some non standard efects by changing second argument, though not all of them work properly.

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

    My solution to avoid this lag when switching pages was: preload images

    final Drawable[] images = new Drawable[3];
    for(int i=0; i<3; i++){
        int position = i+1;
        images[i] = getResources().getDrawable(getResources().getIdentifier("image"+position, "drawable", getPackageName()));
     }
    

    and then:

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
            }
    
            @Override
            public void onPageSelected(int position) {
                imageSwitcher.setImageDrawable(images[position]);
            }
    
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    
    0 讨论(0)
  • 2020-12-28 20:35

    I know this is an old question, but I have run into it recently and discovered a different issue. So just sharing in case anyone comes across this post.

    My fragments were all just big Imageview holders basically and scrolling through Images that the UX/UI team gave me. However, anytime the next or previous image was a different size i.e. (1200 x 1200) transitioning to (1200 x 1300) for example, that swipe would lag, but the rest would be fine.

    Resolutions were in the correct folders. I experimented and found that if I cropped all images to the same size, I had no lag issues. However, that didn't fit my need as I wanted different size images.

    So I switched to using Glide to load the image into place instead of setting the drawable directly and that cleaned it up very nicely. So if anyone else hits this issue, you may just need to switch to loading with glide asynchronously instead of setting directly the images.

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

    I solved this by moving the images into drawable-nodpi, as this disables the scaling for all DPIs. Moving into hdpi/xhdpi will only disable scaling if the user is on a device with that screen density.

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

    For removal of lag:

    1)Put the images in res/drawable-hdpi if images are static.If it is downloaded from URL then use background thread for loading of images.

    2) set page off screen limit by this method viewPager.setOffScreenLimit(size) . With the help of that view pager cache the minimum number of screen which you set in this method by default its value is 1.

    3)You can use FragmentPagerAdapter and FragmentStatePagerAdapter for better performance.

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