安卓:采用高分辨率绘图资源时,极端滞后 [英] Android: Extreme lag when using high-resolution drawables

查看:176
本文介绍了安卓:采用高分辨率绘图资源时,极端滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定在我的应用程序的开始处创建一个启动画面。我创建了一个1600x900的图像,并用它作为绘制。当我跑我的应用程序,这似乎是每一个行动负有1秒的延迟。检查一切之后,我意识到,这是启动画面某种程度上造成这种滞后。测试显示制成可绘制这种方式造成延误的高分辨率图像,我不知道为什么。

我的形象重100KB,我逐渐降低的分辨率和尺寸,并逐渐降低以及滞后。我还做了一个高分辨率,5KB图像和滞后坚持,这意味着该决议主要是罪魁祸首。

这是为什么发生,我怎么能不有后来的反响一个启动画面?

code:

在样式:

 <样式名称=splashscreenTheme父=Theme.AppCompat.Light.NoActionBar>
        <! - 在这里自定义的主题。 - >
        <项目名称=机器人:windowBackground> @绘制/闪屏和LT; /项目>
    < /风格>

在清单:
    
    

 <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/标志
        机器人:标签=@字符串/ APP_NAME
        机器人:fullBackupContent =假
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=。MainActivity
            机器人:screenOrientation =画像
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@风格/ splashscreenTheme>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
< /清单>


解决方案

您所遇到的延迟可能是由于垃圾收集或堆的大小增加。

您的图像 1600×900 = 1440000px 。每个像素具有四个字节的内存中的大小 - 每色酮(RGB),一个用于透明度(A)中。因此,我们可以计算出: 1440000 * 4B = 5760000B 所以周围堆单独的字节数组分配5.7 MB。但它是一个可绘制的,并具有一定比字节数组更多的领域,所以你最终多一点。

添加活动,弦乐,其他图像和资源,它的高得多。

现在,只要你创建一个新的对象VM试图删除一些不使用的对象(垃圾收集)造成滞后小。如果它不能释放足够的内存堆将会增加(同时造成小滞后)。

您可能正在面临着这些问题之一,所以你能做些什么呢?

您没写,但我认为闪屏是不是你的应用程序的唯一活动。问题是,在初始屏幕中的背景保持有效。你不能真正做关于任何事情,但你至少可以手动删除绘制。

 查看窗口= getWindow()getDecorView()。
如果(window.getBackground()!= NULL){
    window.getBackground()setCallback(空)。
    window.setBackground(NU​​LL);
}

在一般情况下,这是给的不使用大图像的在XML属性更好的主意。这是更多的内存高效创建自己的位图的大小,你真的需要(不是所有的设备都具有1600×900分辨率)。

Android为您提供了 BitmapFactory 类来做到这一点。查看<一个href=\"https://www.youtube.com/watch?v=HY9aaXHx8yA&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE&index=43\"相对=nofollow> pre-缩放位图和<一个href=\"http://stackoverflow.com/questions/32121058/most-memory-efficient-way-to-resize-bitmaps-on-android\">Most内存有效的方式,在Android的位图调整?中对整个主题的更多信息。

I decided to create a splashscreen at the beginning of my app. I created a 1600x900 image and used it as a drawable. When I ran my app, it seemed like every action bears a 1 second delay. After checking everything I realized that it was the splashscreen somehow causing this lag. Tests reveal that high resolution images made into drawables this way cause delays, I don't know why.

My image weighs 100kb, I gradually lowered the resolution and the size and the lag gradually lowered as well. I also made a high resolution, 5kb image and the lag persisted, meaning that the resolution is mostly the culprit.

Why is this happening and how can I have a splashscreen without later repercussions?

code:

in styles:

<style name="splashscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowBackground">@drawable/splashscreen</item>
    </style>

in manifest:

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:fullBackupContent="false"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name"
            android:theme="@style/splashscreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

解决方案

The lag you are experiencing is probably due to Garbage Collection or Heap size increase.

Your image is 1600*900 = 1440000px. Each pixel is has an in-memory size of four byte - one per color (RGB) and one for transparency (A). We can therefore calculate: 1440000 * 4B = 5760000B so around 5.7 MB allocated on the heap for the byte-array alone. But it's a drawable, and has some more fields than the byte-array, so you end up with a little more.

Add Activities, Strings, other images and resources and it's much higher.

Now, whenever you create a new object the VM tries to delete some unused objects (Garbage collection) causing a small lag. If it cannot free enough memory the Heap will increase (also causing a small lag).

You are probably facing one of these issues, so what can you do about it?

You didn't write it, but I assume that the splash screen is not the only Activity of your App. The problem is that the splash screen stays active in the background. You can't really do anything about that, but you can at least remove the drawable manually.

View window = getWindow().getDecorView();
if (window.getBackground() != null) {
    window.getBackground().setCallback(null);
    window.setBackground(null);
}

In general, it's a better idea to not use large images in xml attributes. It's much more memory efficient to create the bitmap yourself in size you really need (not all devices have a 1600*900 resolution).

Android gives you the BitmapFactory class to do this. Check out Pre-scaling Bitmaps and Most memory efficient way to resize bitmaps on android? for more info on the whole topic.

这篇关于安卓:采用高分辨率绘图资源时,极端滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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