你知道嗎?windowBackground會影響到Activity的繪制速度!
而我們在開發(fā)android應(yīng)用時,很多時候并沒有注意到這些。因為我們大多數(shù)時候,不會更改Activity主題風(fēng)格,于是android使用默認(rèn)的主題風(fēng)格。而這個主題風(fēng)格中就包含有一個 windowBackground。這個默認(rèn)的主題風(fēng)格定義在frameworks\base\core\res\res\values\themes.xml 中,如下:
- <style name="Theme">
- <item name="colorForeground">@android:color/bright_foreground_dark</item>
- <item name="colorForegroundInverse">@android:color/bright_foreground_dark_inverse</item>
- <item name="colorBackground">@android:color/background_dark</item>
- <item name="colorBackgroundCacheHint">?android:attr/colorBackground</item>
- <item name="disabledAlpha">0.5</item>
- <item name="backgroundDimAmount">0.6</item>
- ......
- <item name="windowBackground">@android:drawable/screen_background_dark</item>
- <item name="windowFrame">@null</item>
- <item name="windowNoTitle">false</item>
- <item name="windowFullscreen">false</item>
- <item name="windowIsFloating">false</item>
- <item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>
- <item name="windowShowWallpaper">false</item>
- ......
這個默認(rèn)的 windowBackground 定義為screen_background_dark,可以在frameworks\base\core\res\res\values\colors.xml 中找到,定義為:<drawable name="screen_background_dark">#ff000000</drawable>
我們首先來看看windowBackground 對程序的影響,然后再找出影響的原因。
1.新建一個叫做windowBackground的工程,其它步驟略。注意由于沒有修改主題,所以工程中的activity使用默認(rèn)的主題風(fēng)格。下面在res/layout/main.xml 中添加一個FpsImageView,如下:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- This layout does not use <merge/> to slow down the drawing --><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
- <com.example.android.window.FpsImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/antelope_canyon" android:scaleType="center" />
- <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|bottom" android:layout_marginBottom="12dip" android:padding="12dip" android:background="#90000000" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Antelope Canyon" />
- </FrameLayout>
其中的FpsImageView繼承自ImageView,在其中增加了 幀率的計算。在activity中不停的調(diào)用postInvalidate以使得FpsImageView不停的繪制,從而顯示出幀率。FpsImageView代碼如下:
- public class FpsImageView extends ImageView {
- private long mStartTime = -1;
- private int mCounter;
- private int mFps;
- private final Paint mPaint;
-
- public FpsImageView(Context context, AttributeSet attrs) {
- super(context, attrs);
-
- mPaint = new Paint();
- mPaint.setColor(0xFFFFFFFF);
- mPaint.setAntiAlias(true);
- }
- @Override
- public void draw(Canvas canvas) {
- if (mStartTime == -1) {
- mStartTime = SystemClock.elapsedRealtime();
- mCounter = 0;
- }
- long now = SystemClock.elapsedRealtime();
- long delay = now - mStartTime;
- super.draw(canvas);
-
- canvas.drawText(mFps + " fps", 100, 50, mPaint);
- if (delay > 1000L) {
- mStartTime = now;
- mFps = mCounter;
- mCounter = 0;
- }
-
- mCounter++;
- }
- }
運行程序,得到如下結(jié)果:
可以看到目前的幀率大概在44 左右。
2.修改AndroidManifest.xml,為activity指定主題,即不使用默認(rèn)主題,在activity標(biāo)簽內(nèi)添加一行:
android:theme="@style/Theme.NoBackground"
其中的 Theme.NoBackground 定義在res/values/theme.xml中,如下:
- <?xml version="1.0" encoding="utf-8"?><resources> <style name="Theme.NoBackground" parent="android:Theme"> <item name="android:windowBackground">@null</item> </style></resources>
從theme.xml中可以看出,它將android:windowBackground置為null了,使用此主題,我們的activity講沒有背景 運行程序,得到如下結(jié)果:
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。