1.启动xml布局页面使用RelativeLayout布局,内容如下:
<?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:id="@+id/rl_splash_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_bg_newyear"
tools:context=".SplashActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/splash_sheep_newyear" />
</RelativeLayout>
2.到SplashActivity写编写代码,内容如下:
package com.example.beijing;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
* 页面一般用:Activity
*/
public class SplashActivity extends Activity {
private RelativeLayout rl_splash_root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
rl_splash_root = findViewById(R.id.rl_splash_root);
//软件启动动画
//渐变动画,缩放动画,旋转动画
AlphaAnimation aa = new AlphaAnimation(0,1);//0到1,从看不见到看得见
// aa.setDuration(500);//设置持续播放时间
aa.setFillAfter(true);
ScaleAnimation sa = new ScaleAnimation(0,1,0,1,ScaleAnimation.RELATIVE_TO_SELF,0.5f,ScaleAnimation.RELATIVE_TO_SELF,0.5f);
// sa.setDuration(500);
sa.setFillAfter(true);
RotateAnimation ra = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
// ra.setDuration(500);
ra.setFillAfter(true);
//创建AnimationSet来的使用来让三个动画同时跑起来
AnimationSet set = new AnimationSet(false);
//添加三个动画无先后顺序,目的同时播放嘛
set.addAnimation(aa);
set.addAnimation(sa);
set.addAnimation(ra);
set.setDuration(2000);
//播放动画
rl_splash_root.startAnimation(set);
//监听,参数是AnimationListener接口,需要创建一个类去实现该接口
set.setAnimationListener(new MyAnimationListener());
}
class MyAnimationListener implements Animation.AnimationListener{
/**
* 动画开始播放的时候回调这个方法
* @param animation
*/
@Override
public void onAnimationStart(Animation animation) {
}
/**
* 动画结束播放的时候回调这个方法
* @param animation
*/
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(SplashActivity.this, "动画播放完成了!", Toast.LENGTH_SHORT).show();
}
/**
* 动画重复播放的时候回调这个方法
* @param animation
*/
@Override
public void onAnimationRepeat(Animation animation) {
}
}
}