9

I've got an Activity made by XML, I've set it's background to a background image I have in my drawable folder.

In the same activity I've created a ViewPager to allow swiping back and forth between views. All the views inside the ViewPager contain one image.

Whenever I swipe left or right, the transition is very slow.

I tried removing the background image by setting it to white (#fff) and all the lag was gone. It worked perfect! The problem is that I do need that background for the application.

Is there any way to optimize a background image or something so swiping will go smoothly again? Currently it's too frustrating to use because of the lag.

I also tried cropping the image to a small size and then just stretched it over the screen, but I didn't notice any performance improvements. Also it's not the images fault that are located in the ViewPager, when I tested it with TextViews instead there was the same lag.

Pieter888
  • 4,882
  • 13
  • 53
  • 74

3 Answers3

16

I figured it out myself after some searching, I will leave the answer here for anyone else who encounters this problem.

Apparently android has a lot of trouble rendering a background when the image is stretched. At least on the Galaxy Tab 10.1 running Android 3.2 it does.

The background image I was using was a picture of a wooden floor, because it's a pattern of the same planks repeating itself I managed to crop the image down from 1440 x 1050 to about 350x500. When using this image as background while stretched out over the entire screen there was a huge performance drop. When displaying as a single image in the top left corner however, it worked just fine.

Using the method described here I managed to repeat the image over the entire screen instead of stretching it. Surprisingly enough, I noticed absolutely no extra stress and the transitions went very smooth.

So here it is, hope this will help others out there with the same performance issues!

Community
  • 1
  • 1
Pieter888
  • 4,882
  • 13
  • 53
  • 74
15

Another solution in the later years: You have to put your drawables into appropriate folders. I had my background.png in drawable folder and it lagged because it struggles with the streching. I could not use repeatable because I dont have that kind of pattern on my phone.

But when i put my drawable background in drawable-xhdpi, worked without any lag at all. So for future reference its also possbile that this might solve your problem. Ref this question which solves itself: ViewPager really slow on Samsung S4

Community
  • 1
  • 1
vonGohren
  • 929
  • 8
  • 22
  • This is awesome, I had problems with my Moto X, and just by coping the drawables to drawable-xhdpi solved the problem! – marcwjj Apr 25 '15 at 18:01
2

This trick helps me a lot when swiping with pager. It is based on hardware acceleration.

       viewPager.setOnPageChangeListener(new OnPageChangeListener() {
                @Override
                public void onPageScrollStateChanged(int scrollState) {
                    if (scrollState != ViewPager.SCROLL_STATE_IDLE) {
                        final int childCount = viewPager.getChildCount();
                        for (int i = 0; i < childCount; i++)
                            viewPager.getChildAt(i).setLayerType(View.LAYER_TYPE_NONE, null);
                    }
                }

          ....
});

Hope it will help.

Zhar
  • 3,330
  • 2
  • 24
  • 25