Welcom页面,Splash页面,代表的意义相同都是
启动页面
,除了个别的企业app或者政府app的话,都会用到本章的知识,请努力掌握!
启动页不同于引导页
引导页一般是介绍产品
,内容相对较长,且仅展示一次
!启动页每次登陆app都会进行展示
,主要用于品牌推广、提前缓存数据
等操作;启动页是引导页之后的一个跳转界面~
关联篇
- Android进阶之路 - Splash、Welcome欢迎页面的初始使用(启动页 - 基础入门)
- 引导页的详细使用(引导页 - 基础入门)
- Android进阶之路 - CountDownTimer倒计时的分秒实现
可以关注一下启动页视图没加载出来之前,出现短暂空白页面的处理方式
Effect
前情提要
如有需求,可下载此Demo
Tips
- 倒计时功能采用了
Android
原生的CountDownTimer
- 右上边的背景可使用UI切图,也可以
shape
自己画 - 倒计时功能采用了
Android
原生的CountDownTimer
- 如
ImageView
图片显示不全,在ImageView
中加入下面这行属性
android:scaleType="fitXY"
CountDownTimer
在onDestroy()
生命周期内的销毁,减少内存开销
开发实践
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();
}
}
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>
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>
以下基本可忽略,重心不在这儿...
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);
}
}
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>
所遇问题
启动黑屏
创建 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>
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>
启动白屏
首次进入启动页,没有直接显示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>
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>
8.0 兼容
问题:Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
原因:因对应 AndroidManfest
在清单文件注册中的 theme
属性包含以下配置
android:theme="@android:style/Theme.Translucent.NoTitleBar"