小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

(圖文)android UI 優(yōu)化系之windowBackground 一

 勤奮不止 2013-05-17

 你知道嗎?windowBackground會(huì)影響到Activity的繪制速度! 
        而我們?cè)陂_(kāi)發(fā)android應(yīng)用時(shí),很多時(shí)候并沒(méi)有注意到這些。因?yàn)槲覀兇蠖鄶?shù)時(shí)候,不會(huì)更改Activity主題風(fēng)格,于是android使用默認(rèn)的主題風(fēng)格。而這個(gè)主題風(fēng)格中就包含有一個(gè) windowBackground。這個(gè)默認(rèn)的主題風(fēng)格定義在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. ......

 
 
這個(gè)默認(rèn)的 windowBackground 定義為screen_background_dark,可以在frameworks\base\core\res\res\values\colors.xml 中找到,定義為:<drawable name="screen_background_dark">#ff000000</drawable> 
 
我們首先來(lái)看看windowBackground 對(duì)程序的影響,然后再找出影響的原因。 
 
1.新建一個(gè)叫做windowBackground的工程,其它步驟略。注意由于沒(méi)有修改主題,所以工程中的activity使用默認(rèn)的主題風(fēng)格。下面在res/layout/main.xml 中添加一個(gè)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./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,在其中增加了 幀率的計(jì)算。在activity中不停的調(diào)用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. }

 
 
運(yùn)行程序,得到如下結(jié)果: 
 
可以看到目前的幀率大概在44 左右。 
 
 
2.修改AndroidManifest.xml,為activity指定主題,即不使用默認(rèn)主題,在activity標(biāo)簽內(nèi)添加一行: 
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講沒(méi)有背景  運(yùn)行程序,得到如下結(jié)果: 
 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多