引子
不管是Android還是iOS平台中,都可以看到一些應用在啟動的時候會先出現一個啟動畫面(Splash Activity),如QQ、微信等。這個啟動畫面中往往會將ActionBar和Status Bar隱藏掉,然后用戶進入一種沉浸的狀態,形成更強烈的視覺沖擊。一方面,這可以給用戶留下更深刻的使用體驗,從而產生一定品牌效應;另一方面,也給應用的啟動初始化留下了充裕的時間,避免因為啟動時間過長而給用戶留下不良的印象。因此,全屏顯示在手機應用中得到了廣泛的應用。那么這篇博客中就記錄下全屏顯示的一些實現方案。
實現
方案一:給布局管理器設置背景圖片。這種方案是通過設置android:background和NoActionBar主題來實現的。
1 <!-- Base application theme. --> 2 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 3 <!-- Customize your theme here. --> 4 <item name="colorPrimary">@color/colorPrimary</item> 5 <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 6 <item name="colorAccent">@color/colorAccent</item> 7 </style>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" tools:context="com.hnb.zzk.clippingtest.MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:adjustViewBounds="false"> </ImageView> </RelativeLayout>
這里@drawable/background是放在drawable目錄下的一個圖片資源。此時,還有一點遺憾,status Bar還是沒有隱藏掉,因此還要調用方法將Status Bar隱藏掉:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
方案二、在FrameLayout中添加一個全屏的子視圖ImageView。具體說來就是將ImageView作為FrameLayout的第一個子視圖,基於FrameLayout的屬性,后面添加的子視圖都將疊加到第一個子視圖之上,間接地實現了全圖片視圖背景。
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.hnb.zzk.clippingtest.MainActivity"> 7 8 <ImageView 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:adjustViewBounds="false" 12 android:src="@drawable/background" 13 android:scaleType="centerCrop"> 14 15 </ImageView> 16 17 </FrameLayout>
在Java代碼中還是一樣設置:
1 requestWindowFeature(Window.FEATURE_NO_TITLE); 2 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
這樣實現的效果和方案一並沒有什么差別。但是要注意當加載分辨率較大的圖片時、或者圖片較多時,容易導致內存溢出。
方案三、使用Java代碼動態加載圖片設置全屏背景。這種方案的原理是,根據顯示屏幕的大小對圖片進行縮放,從而對屏幕尺寸進行適配。
/* create a full screen window */ requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.your_activity); /* adapt the image to the size of the display */ Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource( getResources(),R.drawable.background),size.x,size.y,true); /* fill the background ImageView with the resized image */ ImageView iv_background = (ImageView) findViewById(R.id.iv_background); iv_background.setImageBitmap(bmp);
