android background

你知道吗?windowBackground会影响到Activity的绘制速度! 
        而我们在开发android应用时,很多时候并没有注意到这些。因为我们大多数时候,不会更改Activity主题风格,于是android使用默认的主题风格。而这个主题风格中就包含有一个 windowBackground。这个默认的主题风格定义在frameworks\base\core\res\res\values\themes.xml 中,如下: 

  1. <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.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中不停的调用postInvalidate以使得FpsImageView不停的绘制,从而显示出帧率。FpsImageView代码如下: 

  1. public class FpsImageView extends ImageView {
  2.     private long mStartTime = -1;
  3.     private int mCounter;
  4.     private int mFps;
  5.     private final Paint mPaint;
  6.     
  7.     public FpsImageView(Context context, AttributeSet attrs) {
  8.         super(context, attrs);
  9.         
  10.         mPaint = new Paint();
  11.         mPaint.setColor(0xFFFFFFFF);
  12.         mPaint.setAntiAlias(true);
  13.     }
  14.     @Override
  15.     public void draw(Canvas canvas) {
  16.         if (mStartTime == -1) {
  17.             mStartTime = SystemClock.elapsedRealtime();
  18.             mCounter = 0;
  19.         }
  20.         long now = SystemClock.elapsedRealtime();
  21.         long delay = now - mStartTime;
  22.         super.draw(canvas);
  23.         
  24.         canvas.drawText(mFps + " fps", 100, 50, mPaint);
  25.         if (delay > 1000L) {
  26.             mStartTime = now;
  27.             mFps = mCounter;
  28.             mCounter = 0;
  29.         }
  30.         
  31.         mCounter++;
  32.     }
  33. }

 
 
运行程序,得到如下结果: 
 
可以看到目前的帧率大概在44 左右。 
 
 
2.修改AndroidManifest.xml,为activity指定主题,即不使用默认主题,在activity标签内添加一行: 
android:theme="@style/Theme.NoBackground" 
其中的 Theme.NoBackground 定义在res/values/theme.xml中,如下: 

  1. <?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讲没有背景  运行程序,得到如下结果: 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值