不管是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);
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有