zoukankan      html  css  js  c++  java
  • Android开发中的全屏背景显示方案

    引子

        不管是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);  
    

    参考

  • 相关阅读:
    PCMan:Ubuntu下的最快的文件管理器
    Android 不需要root权限也可卸载系统应用的adb命令
    Directory Opus --- 布局灵活的文件管理,支持文件预览,强烈推荐
    Charles 抓包网络接口请求 修改请求参数 修改返回数据
    做直播app功能测试,怎么开展?
    Python字典内置函数和方法
    pycharm2020.3安装破解版激活教程 附:无限重置时间插件(mac windows linux)
    if not用法
    nvm安装与使用
    Python批量安装.Txt文件中的类库包
  • 原文地址:https://www.cnblogs.com/csuftzzk/p/android_full_screen.html
Copyright © 2011-2022 走看看