Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ViewPager performance issues when using background

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.

like image 820
Pieter888 Avatar asked Apr 05 '12 08:04

Pieter888


People also ask

When should I use ViewPager?

ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.

Can I use ViewPager with views not with fragments?

yes...you can use View instead of Fragment in viewpager. Here you can Find Whole example that will help you to achieve Viewpager without Fragment. Go through this documentation. Save this answer.

Why ViewPager is used in Android?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.

How do I turn off ViewPager on Android?

You can easily do that by creating a custom class inherits from viewPager and override two methods: “onTouchEvent()” and “onInterceptTouchEvent()” and return true or false to disable and enable the swiping on certain touch events i.e say swiping.


1 Answers

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!

like image 60
Pieter888 Avatar answered Oct 13 '22 00:10

Pieter888