Android的背景图像减慢应用 [英] android background image slows down app

查看:426
本文介绍了Android的背景图像减慢应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是viewpager刷卡之间在我的应用程序片段。我定义了XML的背景,所以

I am using a viewpager to swipe amongst fragments in my app. I define the background in the XML, so

android:background="@drawable/bg_final"

如果我用一个简单的背景颜色,我的应用程序的工作原理非常流畅。如果我在这样的背景图片使用它,FPS下降,我的应用程序只变成差强人意。这是不慢,只是没有那么顺利,但它可以工作laggy较弱的设备。它是为应用背景图像一个糟糕的方​​式?整个图像是一个480×800的PNG与14.7kB的大小。可能是什么问题?

If I use a simple background color, my app works very smooth. If I use it with this background image, fps decreases and my app becomes only passable. It is not slow, just not so smooth, however on weaker devices it could work laggy. Is it a bad way to apply a background image? The whole image is a 480x800 png with the size of 14.7kB. What might be the problem?

(片段的背景都是透明的,所述viewpager是其中有其背景与该图像的main.xml中)

(the backgrounds of the fragments are transparent, the viewpager is in a main.xml which has its background with this image)

推荐答案

有很多答案在那里,点pre-缩放位图。这是非常项重要的,如果你能,你应该这样做,尝试一些不同的东西后,不过,我经历过的最大的性能提升是创建一个视图,并重写的onDraw方法。

There are lots of answers out there that point to pre-scaling your bitmap. This is very imporant and you should do it if you can, however after trying several different things, the biggest performance gain I experienced was to create a view and override the onDraw method.

类可能是像这样简单:

public class DTImageView extends View {

     public Bitmap imageBitmap;

     public DTImageView(Context context) { 
         super(context); 
     } 

     @Override
     protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(imageBitmap != null)
          canvas.drawBitmap(imageBitmap, 0, 0, null); 
     }
}

使用prescaled位调用code:

Invoke the code using a prescaled bitmap:

 DTImageView bgImageView = new DTImageView(context);
 bgImageView.imageBitmap = Bitmap.createScaledBitmap(bitmap,width,height,true);

在这一点上,你可以将此视图添加到它所属的视图层次结构。就我而言,我的看法层次有bgImage,一个中间地带,所有的其他功能发生了,fgImage。

At this point you can add this view to the view hierarchy where it belongs. In my case, my view hierarchy had a bgImage, a middle ground where all the other functionality happened, and fgImage.

使用了Android SDK工具层次查看器来分析应用程序,看到每个视图正在如何快速绘制。我的应用程序花费30-40ms绘制bgImage和fgImage。 pre-缩放和压倒一切的onDraw减少了30-40ms约1.5毫秒。

Use the android sdk tool Hierarchy Viewer to profile the app and see how fast each view is taking to draw. My app spent 30-40ms drawing the bgImage and fgImage. Pre-scaling and overriding onDraw reduced that 30-40ms to about 1.5ms.

下面是之前和之后我的景色之一绘图性能的截图:

Here is a screenshot of the Draw performance for one of my views before and after:

BEFORE:

BEFORE:

AFTER:

AFTER:

这篇关于Android的背景图像减慢应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆