每个手机APP在打开的时候总会先有一个几秒钟闪屏的界面出现,今天就学了这个页面的设计
1、首先写一个布局文件,这个布局文件就是闪屏页要显示的东西,我写的界面如下:简单来说就是一个相对布局文件中放了一张图片,和一些文字,其属性值可根据要求自行设置
ProgressBar 这个是一个圆形的加载的进度条
<?xml version="1.0" encoding="utf-8"?>
<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/splash"
android:id="@+id/root"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="现男友ing"
android:layout_centerVertical="true"
android:textColor="#B4A6A6"
android:textSize="20sp"
android:shadowColor="#f00"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1"
android:layout_centerHorizontal="true" />
<ProgressBar
android:id="@+id/pb_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_name"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
2、接着闪屏页出现几秒后进入主界面,两种方法,第一种可以设置监听事件在动画结束后跳转至主页面,第二种是通过延时2秒后进入主界面,简单写一个demo用来演示过程
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AlphaAnimation;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private RelativeLayout mRoot;
@Override
protected void onCreate(Bundle savedInstanceState) {
mRoot=(RelativeLayout)findViewById(R.id.root);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postdelay();// 调用延时方法
//渐变动画
AlphaAnimation anim=new AlphaAnimation(0.2f,1);
anim.setDuration(2000);
// mRoot.startAnimation(anim);
//缩放动画
ScaleAnimation animScale=new ScaleAnimation(0,1,0,1,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
animScale.setDuration(1000); //时间
animScale.setFillAfter(true);
//旋转动画
RotateAnimation animRotate=new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
animRotate.setDuration(1000); //时间
animRotate.setFillAfter(true);
//动画集合,将动画添加到集合里面
AnimationSet animationSet=new AnimationSet(true);
animationSet.addAnimation(animRotate);
animationSet.addAnimation(animScale);
animationSet.addAnimation(anim);
mRoot.startAnimation(animationSet);
/*//一、通过设置监听 动画结束后跳转
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}*/
}
//二、写一个延时方法
private void postdelay(){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//延时2秒后进入执行此方法跳转到主页面
startActivity(new Intent(this,Main2Activity.class));
finish();
}
}, 2000);//2秒后执行Runnable中的run方法
}
}
另外,还需要注意,闪屏页是充满全屏的,因此需要隐藏标题栏
方法如下:
更改AndroidManifest.xml 文件的application中的theme,点进入这个文件,或者直接进入资源文件res下的styles.xml
android:theme="@style/AppTheme"
在style里面加入下面这一行,为true时表示隐藏标题栏,false为显示标题栏
<item name="windowNoTitle">true</item>