Overdraw和Romain Guy的博客发布了Android Performance Case Study

use*_*321 14 android

根据Romain Guy的博客文章Android Performance Case Study在谈到Overdraw时,他说:

删除窗口背景:系统使用主题中定义的背景在启动应用程序时创建预览窗口.除非您的应用程序是透明的,否则永远不要将其设 相反,通过调用getWindow()将其设置为您想要的颜色/图像或从onCreate()中删除.setBackgroundDrawable(null).***

但是getWindow().setBackgroundDrawable(null)似乎没有任何效果.这是一个代码示例:

//MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setBackgroundDrawable(null);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

// main.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:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#FFE0FFE0"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:background="#FFFFFFE0" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="@string/hello_world" />
</LinearLayout>

// styles.xml
<style name="AppTheme" parent="AppBaseTheme">
   <item name="android:windowBackground">@color/yellow</item>
</style>
Run Code Online (Sandbox Code Playgroud)

此示例在图像中生成结果.您可以看到外层有一个透支,窗口背景颜色仍然可见.我预计窗口的背景会消失,只有lineralayout才能透支.

在此输入图像描述

MH.*_*MH. 22

getWindow().setBackgroundDrawable(null)向下移动,直到任何地方setContentView(R.layout.main); 例如:

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getWindow().setBackgroundDrawable(null);
}
Run Code Online (Sandbox Code Playgroud)

setContentView(...)通话设置传播活动连接到也可能是窗口中的内容将覆盖你的意思是与所做的更改setBackgroundDrawable(null).

结果:

在此输入图像描述

  • 你说的没错.调用getWindow()之类的东西时非常奇怪.setBackgroundDrawable(new ColorDrawable(0xFFFF0000)); 在setContentView工作之前就好了.在setContentView意味着默认为已定义的样式之前显然是"null",并且不会在setContentView之后绘制任何类似的东西.感谢您的帮助! (3认同)