你知道吗?windowBackground会影响到Activity的绘制速度! 而我们在开发android应用时,很多时候并没有注意到这些。因为我们大多数时候,不会更改Activity主题风格,于是android使用默认的主题风格。而这个主题风格中就包含有一个 windowBackground。这个默认的主题风格定义在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>
- ......
这个默认的 windowBackground 定义为screen_background_dark,可以在frameworks\base\core\res\res\values\colors.xml 中找到,定义为:<drawable name="screen_background_dark">#ff000000</drawable> 我们首先来看看windowBackground 对程序的影响,然后再找出影响的原因。 1.新建一个叫做windowBackground的工程,其它步骤略。注意由于没有修改主题,所以工程中的activity使用默认的主题风格。下面在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./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中不停的调用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++;
- }
- }
运行程序,得到如下结果:
可以看到目前的帧率大概在44 左右。 2.修改AndroidManifest.xml,为activity指定主题,即不使用默认主题,在activity标签内添加一行: 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讲没有背景 运行程序,得到如下结果:
|