打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
(图文)android UI 优化系之windowBackground 一

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

  1. <style name="Theme">  
  2. <item name="colorForeground">@android:color/bright_foreground_dark</item>  
  3. <item name="colorForegroundInverse">@android:color/bright_foreground_dark_inverse</item>  
  4. <item name="colorBackground">@android:color/background_dark</item>  
  5. <item name="colorBackgroundCacheHint">?android:attr/colorBackground</item>  
  6. <item name="disabledAlpha">0.5</item>      
  7. <item name="backgroundDimAmount">0.6</item>
  8. ......
  9. <item name="windowBackground">@android:drawable/screen_background_dark</item>        
  10. <item name="windowFrame">@null</item>        
  11. <item name="windowNoTitle">false</item>      
  12. <item name="windowFullscreen">false</item>        
  13. <item name="windowIsFloating">false</item>      
  14. <item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>        
  15. <item name="windowShowWallpaper">false</item>      
  16. ......

 
 
这个默认的 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,如下: 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--  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">
  3.     <com.example.android.window.FpsImageView        android:layout_width="fill_parent"        android:layout_height="fill_parent"                android:src="@drawable/antelope_canyon"        android:scaleType="center" />
  4.     <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" />        
  5. </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讲没有背景  运行程序,得到如下结果: 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
安卓手机全局背景美化教程。
android_002_04: 自定义主题样式
Android根据Button状态(normal,focused,pressed)显示不同...
Delphi XE5开发的Android启动时黑屏解决方法
自定义progressdialog
自定义 RadioButton 选中和未选中时的图片
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服