当前位置:   article > 正文

Android进阶之路 - 快速实现启动页_android 启动页

android 启动页

Welcom页面,Splash页面,代表的意义相同都是 启动页面,除了个别的企业app或者政府app的话,都会用到本章的知识,请努力掌握!

启动页不同于引导页

  • 引导页一般是介绍产品,内容相对较长,且仅展示一次
  • 启动页每次登陆app都会进行展示,主要用于品牌推广、提前缓存数据等操作;
  • 启动页是引导页之后的一个跳转界面~

关联篇

可以关注一下启动页视图没加载出来之前,出现短暂空白页面的处理方式

Effect
这里写图片描述

前情提要

如有需求,可下载此Demo

Tips

  1. 倒计时功能采用了Android原生的CountDownTimer
  2. 右上边的背景可使用UI切图,也可以 shape 自己画
  3. 倒计时功能采用了Android原生的CountDownTimer
  4. ImageView图片显示不全,在ImageView中加入下面这行属性
 android:scaleType="fitXY"
  • 1
  1. CountDownTimeronDestroy()生命周期内的销毁,减少内存开销

开发实践

GuideActivity(启动页)

package com.yl.shape.guide;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.yl.shape.shape.R;

/**
 * Created by YongLiu on 2017/8/2.
 */

public class GuideActivity extends AppCompatActivity {

    private TextView mTimer;
    private CountDownTimer countDownTimer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        mTimer = (TextView) findViewById(R.id.tv_time);

 //    兄弟篇中的实现方式,固定展示3秒后跳转
 //    new Handler().postDelayed(new Runnable() {
 //         @Override
 //          public void run() {
 //               startActivity(new //Intent(GuideActivity.this,MainActivity.class));
 //               finish();
 //          }
 //       },3000);

        mTimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                finish();
            }
        });

        countDownTimer = new CountDownTimer(4000, 1000){

            @Override
            public void onTick(long millisUntilFinished) {
                mTimer.setText(millisUntilFinished/1000 + "秒");
                mTimer.setTextColor(Color.WHITE);
            }

            @Override
            public void onFinish() {
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                finish();
            }
        };
        countDownTimer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        countDownTimer.cancel();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

activity_guide

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/background"
        android:id="@+id/image"
        android:scaleType="fitXY"
        />
    <TextView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/back"
        android:id="@+id/tv_time"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text=""
        android:gravity="center"
        />

</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

AndroidMainfest

设置GuideActivity为第一个启动的主Activity,同时注意注册MainActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yl.shape.shape">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name="com.yl.shape.guide.GuideActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity android:name="com.yl.shape.guide.MainActivity"/>
    </application>

</manifest>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

以下基本可忽略,重心不在这儿...

MainActivity

package com.yl.shape.guide;

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

import com.yl.shape.shape.R;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yl.shape.shape.com.yl.shape.guide.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Baby,GO GO GO !"
        android:textColor="#e322e1"
    />
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

所遇问题

启动黑屏

创建 SplashTheme

    <style name="SplashTheme" parent="Theme.AppCompat">
        <!-- 防止启动黑屏 ,可以设置背景图片或者使用透明背景 -->
        <item name="android:windowBackground">@drawable/drawable_app_launch</item>

        <item name="android:windowFullscreen">true</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

AndroidManfest 引用 SplashTheme

 <activity
            android:name=".home.activity.GuideActivity"
            android:theme="@style/SplashTheme"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
启动白屏

首次进入启动页,没有直接显示UI ,而是进入短暂空白,之后再跳出我们的UI

产生原因:在此时问题已经解决了,主要原因在于Theme是在App启动之后加载,而Activity中的背景图是在Activity启动之后加载,所以之前在App启动之后会看到Theme的默认背景
简单言之,App的Theme属性在App启动时就加载,而背景图是在所依赖的Activity启动时才绘制加载,App的启动当然优先于Activity了,问题解决!

value 下的 style(加入以下代码)

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/gray2</item>
    </style>
  • 1
  • 2
  • 3
  • 4
  • 5

AndroidManfest 找到 启动页Activity 修改theme

 <activity
            android:name=".home.activity.GuideActivity"
            android:theme="@style/AppTheme"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
8.0 兼容

问题:Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
原因:因对应 AndroidManfest 在清单文件注册中的 theme 属性包含以下配置

android:theme="@android:style/Theme.Translucent.NoTitleBar"
  • 1

解决:Android各版本的兼容问题

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/144650
推荐阅读