I've got an Activity
made by XML, I've set it's background to a background image I have in my drawable folder.
我有一个由XML制作的活动,我将它的背景设置为我在可绘制文件夹中的背景图像。
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.
在相同的活动中,我创建了一个ViewPager来允许在视图之间来回切换。ViewPager中的所有视图都包含一个映像。
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.
我尝试将背景图像设置为白色(#fff),所有的延迟都消失了。完美的工作!问题是我确实需要这个应用程序的背景。
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.
我还试着将图像裁剪成较小的尺寸,然后将它拉伸到屏幕上,但我没有注意到任何性能改进。同样,位于ViewPager中的不是图像错误,当我使用textview测试它时,也存在同样的延迟。
3 个解决方案
#1
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.
显然,当图像被拉伸时,android在渲染背景时会遇到很多麻烦。至少在运行Android 3.2的Galaxy Tab 10.1上是这样的。
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.
我使用的背景图片是一张木地板的图片,因为它是同一块木板的图案重复出现,我成功地将图片从1440 x 1050裁剪到350x500。当把这张图片作为背景,在整个屏幕上展开时,会有一个巨大的性能下降。当在左上角显示单个图像时,它工作得很好。
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!
所以在这里,希望这能帮助其他人解决同样的性能问题!
#2
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.
在以后的几年里,另一个解决方案是:你必须将你的可绘制文件放入适当的文件夹中。我有我的背景。png在drawable文件夹中,它滞后了,因为它与streching斗争。我不能使用可重复,因为我的手机没有这种模式。
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
但是,当我把可绘制的背景放在drawable-xhdpi中时,它完全没有任何延迟。因此,为了将来的参考,它也有可能解决你的问题。请参考这个问题:ViewPager在三星S4上的速度确实很慢
#3
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.
希望它会有所帮助。
#1
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.
显然,当图像被拉伸时,android在渲染背景时会遇到很多麻烦。至少在运行Android 3.2的Galaxy Tab 10.1上是这样的。
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.
我使用的背景图片是一张木地板的图片,因为它是同一块木板的图案重复出现,我成功地将图片从1440 x 1050裁剪到350x500。当把这张图片作为背景,在整个屏幕上展开时,会有一个巨大的性能下降。当在左上角显示单个图像时,它工作得很好。
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!
所以在这里,希望这能帮助其他人解决同样的性能问题!
#2
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.
在以后的几年里,另一个解决方案是:你必须将你的可绘制文件放入适当的文件夹中。我有我的背景。png在drawable文件夹中,它滞后了,因为它与streching斗争。我不能使用可重复,因为我的手机没有这种模式。
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
但是,当我把可绘制的背景放在drawable-xhdpi中时,它完全没有任何延迟。因此,为了将来的参考,它也有可能解决你的问题。请参考这个问题:ViewPager在三星S4上的速度确实很慢
#3
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.
希望它会有所帮助。