温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

实现Android启动屏画面的案例

发布时间:2021-02-18 14:29:09 来源:亿速云 阅读:162 作者:小新 栏目:移动开发

这篇文章给大家分享的是有关实现Android启动屏画面的案例的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

Android启动屏不正确的实现可能会导致用户长时间等待,或者可能会出现黑白屏。这里简单演示如何正确实现Android启动屏。

演示分为以下几个步骤:

  1. 在res/drawable文件夹中创建splash_background.xml文件。

  2. 编辑res/values/styles.xml

  3. 创建java/.../SplashActivity

  4. 编辑manifests/AndroidManifest.xml

1、在res/drawable文件夹中创建splash_background.xml文件

根据你的需求调整位图图像的重力和尺寸。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:drawable="@color/colorPrimary"/>

  <item android:gravity="center" android:width="100dp" android:height="100dp">
    <bitmap
      android:gravity="fill_horizontal|fill_vertical"
      android:src="@drawable/logo"/>
  </item>

</layer-list>

2、编辑res/values/styles.xml

这里的样式用于启动画面。 这是为了在启动屏幕时隐藏操作栏。

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_background</item>
  </style>

</resources>

3、创建java/.../SplashActivity

一旦App启动,SplashActivity将启动,然后转移到MainActivity。

package com.example.jtdan.goodSplash;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //switch from splash activity to main activity
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
  }
}

4、编辑manifests/AndroidManifest.xml

在清单文件中添加新的启动画面Activity。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.jtdan.goodSplash">

  <application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="goodSplash"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name="com.example.jtdan.goodSplash.SplashActivity" android:theme="@style/SplashTheme">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

    <activity android:name="com.example.jtdan.goodSplash.MainActivity"></activity>

  </application>

</manifest>

感谢各位的阅读!关于“实现Android启动屏画面的案例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI