1

早上好,

在编写我的第一个 android 应用程序的过程中,所以请耐心等待。

我想为我使用的活动提供背景图像。我的第一种方法是将它简单地添加到活动的布局中,

....
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/desc_background"
    android:scaleType="centerCrop"
    android:src="@drawable/background"
    android:tint="#dd000022" />
....

不幸的是,当我反复调用某些活动时,这似乎会导致内存泄漏。最后我找到并找到了为什么在Android上避免内存泄漏的原因

我的下一个想法是执行上述文章中 Romain 所说的“非常快速且非常错误”的解决方案 - 将位图保存在静态字段或 Application 对象中。

我发现了有关该主题的各种文章,但似乎都没有意识到 Romains 的观点。我也不清楚如何实施 Romains 解决方案。

任何提示如何在不遇到内存泄漏的情况下处理 android 中的图像将不胜感激。

非常感谢

马丁

4

1 回答 1

-2

为什么不只是为您正在使用的布局添加背景。

前使用线性布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/my_background"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    **REST OF LAYOUT HERE**
</LinearLayout>

或为所有活动使用一张图片创建您自己的风格,然后在您的清单中应用

在你的 res/values 添加一个 styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="android:Theme">
        <item name="android:windowBackground">@drawable/my_background</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

</resources>

并在每个活动的清单中添加

android:theme="@style/MyTheme"
于 2012-08-30T15:23:59.633 回答