Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overdraw and Romain Guy's blog post Android Performance Case Study

Tags:

android

Based upon Romain Guy's blog post Android Performance Case Study when talking about Overdraw he says this:

Removing the window background: the background defined in your theme is used by the system to create preview windows when launching your application. Never set it to null unless your application is transparent. Instead, set it to the color/image you want or get rid of from onCreate() by calling getWindow().setBackgroundDrawable(null).***

However getWindow().setBackgroundDrawable(null) seems to have no effect. Here's a sample with the code:

//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>

This sample produces the results in the image. You can see the outerlayer has an overdraw and the window background color is still visible. I expected the window's background to be gone and only the lineralayout to have overdraw.

enter image description here

like image 419
user123321 Avatar asked Dec 17 '12 00:12

user123321


1 Answers

Just move getWindow().setBackgroundDrawable(null) down, until anywhere after setContentView(R.layout.main); e.g.:

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

The setContentView(...) call propagates setting the content on the window the activity is attached to and probably overrides the change you meant to made with setBackgroundDrawable(null).

Result:

enter image description here

like image 164
MH. Avatar answered Nov 15 '22 14:11

MH.