Android ViewPager Lag












10















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 (http://developer.android.com/training/displaying-bitmaps/display-bitmap.html) so I am already caching and setting the image drawable in a background thread. But I still get some lag when swithcing pages of the pager. On Each fragment, I only inflate the view on the onCreateView() method, so I have no Idea what may be causing this lag. What can I do to remove this lag/choppy effect?










share|improve this question



























    10















    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 (http://developer.android.com/training/displaying-bitmaps/display-bitmap.html) so I am already caching and setting the image drawable in a background thread. But I still get some lag when swithcing pages of the pager. On Each fragment, I only inflate the view on the onCreateView() method, so I have no Idea what may be causing this lag. What can I do to remove this lag/choppy effect?










    share|improve this question

























      10












      10








      10


      5






      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 (http://developer.android.com/training/displaying-bitmaps/display-bitmap.html) so I am already caching and setting the image drawable in a background thread. But I still get some lag when swithcing pages of the pager. On Each fragment, I only inflate the view on the onCreateView() method, so I have no Idea what may be causing this lag. What can I do to remove this lag/choppy effect?










      share|improve this question














      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 (http://developer.android.com/training/displaying-bitmaps/display-bitmap.html) so I am already caching and setting the image drawable in a background thread. But I still get some lag when swithcing pages of the pager. On Each fragment, I only inflate the view on the onCreateView() method, so I have no Idea what may be causing this lag. What can I do to remove this lag/choppy effect?







      android android-viewpager






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 2 '13 at 11:52









      user2235899user2235899

      5315




      5315
























          10 Answers
          10






          active

          oldest

          votes


















          48














          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






          share|improve this answer
























          • This was exactly my problem and your answer resolved it for me. I put all the full size background images in the xhdpi folder and now its smooth as butter. :)

            – Amyth
            Jan 11 '16 at 20:27






          • 1





            how to hell did you even figure this out. I almost started to think "oh it's probably taking too much ram bcz of huge image" then I just did what you said. And it just worked. I put mine in xhdpi. Is that mean phone with hdpi/mdpi/xxhdpi/xxxhdpi would also show my background.

            – Alex
            Jul 28 '16 at 18:55











          • Wow, thanks man! Why does it work?

            – Simon
            Jan 20 at 19:30



















          15














          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.






          share|improve this answer
























          • Just a small correction, it is viewPager.setOffscreenLimit(size). This worked for me though, thanks!

            – Andrew Steinmetz
            Feb 16 '18 at 8:07





















          4














          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.






          share|improve this answer































            1














            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) {
            }
            });





            share|improve this answer
























            • Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.

              – peterh
              Jun 20 '15 at 21:42



















            1














            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;





            share|improve this answer































              1














              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.






              share|improve this answer































                1














                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.






                share|improve this answer































                  1














                  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.






                  share|improve this answer































                    0














                    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






                    share|improve this answer































                      0














                      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.






                      share|improve this answer

























                        Your Answer






                        StackExchange.ifUsing("editor", function () {
                        StackExchange.using("externalEditor", function () {
                        StackExchange.using("snippets", function () {
                        StackExchange.snippets.init();
                        });
                        });
                        }, "code-snippets");

                        StackExchange.ready(function() {
                        var channelOptions = {
                        tags: "".split(" "),
                        id: "1"
                        };
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function() {
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled) {
                        StackExchange.using("snippets", function() {
                        createEditor();
                        });
                        }
                        else {
                        createEditor();
                        }
                        });

                        function createEditor() {
                        StackExchange.prepareEditor({
                        heartbeatType: 'answer',
                        autoActivateHeartbeat: false,
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader: {
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        },
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        });


                        }
                        });














                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15763407%2fandroid-viewpager-lag%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown

























                        10 Answers
                        10






                        active

                        oldest

                        votes








                        10 Answers
                        10






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        48














                        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






                        share|improve this answer
























                        • This was exactly my problem and your answer resolved it for me. I put all the full size background images in the xhdpi folder and now its smooth as butter. :)

                          – Amyth
                          Jan 11 '16 at 20:27






                        • 1





                          how to hell did you even figure this out. I almost started to think "oh it's probably taking too much ram bcz of huge image" then I just did what you said. And it just worked. I put mine in xhdpi. Is that mean phone with hdpi/mdpi/xxhdpi/xxxhdpi would also show my background.

                          – Alex
                          Jul 28 '16 at 18:55











                        • Wow, thanks man! Why does it work?

                          – Simon
                          Jan 20 at 19:30
















                        48














                        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






                        share|improve this answer
























                        • This was exactly my problem and your answer resolved it for me. I put all the full size background images in the xhdpi folder and now its smooth as butter. :)

                          – Amyth
                          Jan 11 '16 at 20:27






                        • 1





                          how to hell did you even figure this out. I almost started to think "oh it's probably taking too much ram bcz of huge image" then I just did what you said. And it just worked. I put mine in xhdpi. Is that mean phone with hdpi/mdpi/xxhdpi/xxxhdpi would also show my background.

                          – Alex
                          Jul 28 '16 at 18:55











                        • Wow, thanks man! Why does it work?

                          – Simon
                          Jan 20 at 19:30














                        48












                        48








                        48







                        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






                        share|improve this answer













                        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







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Sep 15 '13 at 3:27









                        coderekcoderek

                        1,4881017




                        1,4881017













                        • This was exactly my problem and your answer resolved it for me. I put all the full size background images in the xhdpi folder and now its smooth as butter. :)

                          – Amyth
                          Jan 11 '16 at 20:27






                        • 1





                          how to hell did you even figure this out. I almost started to think "oh it's probably taking too much ram bcz of huge image" then I just did what you said. And it just worked. I put mine in xhdpi. Is that mean phone with hdpi/mdpi/xxhdpi/xxxhdpi would also show my background.

                          – Alex
                          Jul 28 '16 at 18:55











                        • Wow, thanks man! Why does it work?

                          – Simon
                          Jan 20 at 19:30



















                        • This was exactly my problem and your answer resolved it for me. I put all the full size background images in the xhdpi folder and now its smooth as butter. :)

                          – Amyth
                          Jan 11 '16 at 20:27






                        • 1





                          how to hell did you even figure this out. I almost started to think "oh it's probably taking too much ram bcz of huge image" then I just did what you said. And it just worked. I put mine in xhdpi. Is that mean phone with hdpi/mdpi/xxhdpi/xxxhdpi would also show my background.

                          – Alex
                          Jul 28 '16 at 18:55











                        • Wow, thanks man! Why does it work?

                          – Simon
                          Jan 20 at 19:30

















                        This was exactly my problem and your answer resolved it for me. I put all the full size background images in the xhdpi folder and now its smooth as butter. :)

                        – Amyth
                        Jan 11 '16 at 20:27





                        This was exactly my problem and your answer resolved it for me. I put all the full size background images in the xhdpi folder and now its smooth as butter. :)

                        – Amyth
                        Jan 11 '16 at 20:27




                        1




                        1





                        how to hell did you even figure this out. I almost started to think "oh it's probably taking too much ram bcz of huge image" then I just did what you said. And it just worked. I put mine in xhdpi. Is that mean phone with hdpi/mdpi/xxhdpi/xxxhdpi would also show my background.

                        – Alex
                        Jul 28 '16 at 18:55





                        how to hell did you even figure this out. I almost started to think "oh it's probably taking too much ram bcz of huge image" then I just did what you said. And it just worked. I put mine in xhdpi. Is that mean phone with hdpi/mdpi/xxhdpi/xxxhdpi would also show my background.

                        – Alex
                        Jul 28 '16 at 18:55













                        Wow, thanks man! Why does it work?

                        – Simon
                        Jan 20 at 19:30





                        Wow, thanks man! Why does it work?

                        – Simon
                        Jan 20 at 19:30













                        15














                        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.






                        share|improve this answer
























                        • Just a small correction, it is viewPager.setOffscreenLimit(size). This worked for me though, thanks!

                          – Andrew Steinmetz
                          Feb 16 '18 at 8:07


















                        15














                        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.






                        share|improve this answer
























                        • Just a small correction, it is viewPager.setOffscreenLimit(size). This worked for me though, thanks!

                          – Andrew Steinmetz
                          Feb 16 '18 at 8:07
















                        15












                        15








                        15







                        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.






                        share|improve this answer













                        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.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered May 27 '13 at 23:35









                        PatrickPatrick

                        7,6381665128




                        7,6381665128













                        • Just a small correction, it is viewPager.setOffscreenLimit(size). This worked for me though, thanks!

                          – Andrew Steinmetz
                          Feb 16 '18 at 8:07





















                        • Just a small correction, it is viewPager.setOffscreenLimit(size). This worked for me though, thanks!

                          – Andrew Steinmetz
                          Feb 16 '18 at 8:07



















                        Just a small correction, it is viewPager.setOffscreenLimit(size). This worked for me though, thanks!

                        – Andrew Steinmetz
                        Feb 16 '18 at 8:07







                        Just a small correction, it is viewPager.setOffscreenLimit(size). This worked for me though, thanks!

                        – Andrew Steinmetz
                        Feb 16 '18 at 8:07













                        4














                        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.






                        share|improve this answer




























                          4














                          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.






                          share|improve this answer


























                            4












                            4








                            4







                            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.






                            share|improve this answer













                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 12 '16 at 4:25









                            Rahil AliRahil Ali

                            638720




                            638720























                                1














                                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) {
                                }
                                });





                                share|improve this answer
























                                • Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.

                                  – peterh
                                  Jun 20 '15 at 21:42
















                                1














                                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) {
                                }
                                });





                                share|improve this answer
























                                • Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.

                                  – peterh
                                  Jun 20 '15 at 21:42














                                1












                                1








                                1







                                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) {
                                }
                                });





                                share|improve this answer













                                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) {
                                }
                                });






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jun 20 '15 at 21:23









                                Danilo RodriguezDanilo Rodriguez

                                111




                                111













                                • Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.

                                  – peterh
                                  Jun 20 '15 at 21:42



















                                • Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.

                                  – peterh
                                  Jun 20 '15 at 21:42

















                                Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.

                                – peterh
                                Jun 20 '15 at 21:42





                                Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long, but it is expected.

                                – peterh
                                Jun 20 '15 at 21:42











                                1














                                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;





                                share|improve this answer




























                                  1














                                  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;





                                  share|improve this answer


























                                    1












                                    1








                                    1







                                    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;





                                    share|improve this answer













                                    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;






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jul 31 '15 at 18:31









                                    SimasSimas

                                    36.8k870102




                                    36.8k870102























                                        1














                                        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.






                                        share|improve this answer




























                                          1














                                          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.






                                          share|improve this answer


























                                            1












                                            1








                                            1







                                            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.






                                            share|improve this answer













                                            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.







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Jun 6 '18 at 20:51









                                            Andrew HolbrookAndrew Holbrook

                                            111




                                            111























                                                1














                                                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.






                                                share|improve this answer




























                                                  1














                                                  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.






                                                  share|improve this answer


























                                                    1












                                                    1








                                                    1







                                                    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.






                                                    share|improve this answer













                                                    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.







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Oct 18 '18 at 19:40









                                                    SamSam

                                                    3,22011224




                                                    3,22011224























                                                        1














                                                        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.






                                                        share|improve this answer




























                                                          1














                                                          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.






                                                          share|improve this answer


























                                                            1












                                                            1








                                                            1







                                                            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.






                                                            share|improve this answer













                                                            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.







                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Nov 15 '18 at 12:34









                                                            Markus RiedlMarkus Riedl

                                                            111




                                                            111























                                                                0














                                                                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






                                                                share|improve this answer




























                                                                  0














                                                                  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






                                                                  share|improve this answer


























                                                                    0












                                                                    0








                                                                    0







                                                                    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






                                                                    share|improve this answer













                                                                    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







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered May 2 '14 at 13:45









                                                                    barca_dbarca_d

                                                                    66121030




                                                                    66121030























                                                                        0














                                                                        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.






                                                                        share|improve this answer






























                                                                          0














                                                                          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.






                                                                          share|improve this answer




























                                                                            0












                                                                            0








                                                                            0







                                                                            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.






                                                                            share|improve this answer















                                                                            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.







                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited May 16 '18 at 14:10









                                                                            JensV

                                                                            623921




                                                                            623921










                                                                            answered Jan 27 '18 at 13:31









                                                                            Martin SchulzMartin Schulz

                                                                            1




                                                                            1






























                                                                                draft saved

                                                                                draft discarded




















































                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                But avoid



                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                To learn more, see our tips on writing great answers.




                                                                                draft saved


                                                                                draft discarded














                                                                                StackExchange.ready(
                                                                                function () {
                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15763407%2fandroid-viewpager-lag%23new-answer', 'question_page');
                                                                                }
                                                                                );

                                                                                Post as a guest















                                                                                Required, but never shown





















































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown

































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown







                                                                                Popular posts from this blog

                                                                                Xamarin.iOS Cant Deploy on Iphone

                                                                                Glorious Revolution

                                                                                Dulmage-Mendelsohn matrix decomposition in Python