Android: 像 Whatsapp 一样在没有 GPU OverDraw 的情况下绘制背景

Android: Draw Background with no GPU OverDraw like Whatsapp

为了提高应用性能,我遇到了GPU Overdraw问题。根据 Romain Guy's 文章,这里是基本颜色:



为了测试这一点,我使用 XML 创建了一个简单的项目,如下所示

XML代码

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"

    android:layout_margin="50dp"
    android:background="#fff">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

</RelativeLayout>

并得到结果:

然后我尝试了Whatsapp的透支。令我惊讶的是:

那么 Whatsapp 如何使用 无透支 (无蓝色调)绘制背景墙纸,但在我的 简单xml,即使着色也会透支??

PS: 我特意添加了背景颜色以表明 添加颜色会透支但添加墙纸不会

我认为你透支的是你RelativeLayout的背景。您可以删除 android:background="#fff" 背景将根据应用主题绘制一次。

套用Bill Gates的提示,原来在app中styles.xml设置背景实际上节省了一次透支。这段代码节省了一次 overDraw

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowBackground">@color/highlighted_text_material_dark</item>
    </style>

</resources>

现在添加背景不会占用任何 overDraw 所以 Relative Layout 仍然是 One overDraw

更新 上述方法有效,如果您希望所有应用程序的背景颜色相同,则该方法非常有用。 但是如果你想要不同的颜色并且仍然想保存 OverDraw,请将上面的行替换为

<item name="android:windowBackground">@null</item>


繁荣..你很高兴..

提示:如果你的应用程序有黑色作为背景..去吧,再节省一次透支;)

每个 Activity 都有一个来自主题 android:windowBackground 属性的默认背景 Drawable。

WhatsApp 正在使用可以在两个方向上重复以填满整个屏幕的背景图像。这样他们就不需要为不同的屏幕尺寸处理不同的图像 - 它的响应速度非常快!

假设您有这样一张平铺图像(例如 drawable-nodpi/background_tile.png)

只需创建一个新的 xml Drawable(例如 drawable/background.xml),如下所示:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background_tile"
    android:tileMode="repeat" />

现在只需将此 Drawable 设置为您 Activity 的主题背景即可:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowBackground">@drawable/background</item>
</style>

您还可以在运行时设置 Activity 的背景 Drawable getWindow().setBackgroundDrawablegetWindow().setBackgroundDrawableResource

您还可以将背景 Drawable 设置为 null,例如,如果整个 Activity 显示一个 MapView 或类似的东西。

另请注意,如果 Activity 是您的主要启动器 Activity。

,则 Zygote 将在应用程序启动阶段使用主题的背景 Drawable