怎样做一个app初始界面的闪屏页呢?
第一步要先设置一个背景图片,这里涉及到的知识点有
- 旋转动画
- 缩放动画
- 渐变动画
创建一个SplashActivity和splash_activity.xml文件.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_bg_newyear">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:src="@drawable/splash_horse_newyear"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
现在开始设置动画,首先拿到整体布局
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
rlroot = (RelativeLayout)findViewById(R.id.rl_root);
//这里添加了动画属性,旋转动画
animRotat = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animRotat.setDuration(1000);//动画时间
animRotat.setFillAfter(true);//保持动画结束状态
//缩放动画
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);//保持动画结束状态
//渐变动画
animAlpha = new AlphaAnimation(0,1);
animAlpha.setDuration(2000);//设置动画时间
animAlpha.setFillAfter(true);//保持动画结束状态
//动画集合
set = new AnimationSet(true);
set.addAnimation(animRotat);
set.addAnimation(animScale);
set.addAnimation(animAlpha);
//启动动画
rlroot.startAnimation(set);
}
这个就是闪屏页的动画.主要用到了RotateAnimation ScaleAnimation AlphaAnimation.
跳转页面
通过set.setAnimationListener这个方法,在 onAnimationEnd这个方法中开始编写
public void onAnimationEnd(Animation animation) {
//动画结束,跳转页面
//如果是第一次进入,进入新手引导页
//否则跳转到主页面
/* SharedPreferences sp = getSharedPreferences("config",MODE_PRIVATE);
Boolean isFirstEnter = sp.getBoolean("is_first_enter",true);*/
Boolean isFirstEnter = PrefUtils.getBoolean(SplashActivity.this,"is_first_enter",true);
Intent intent;
if(isFirstEnter){
//跳到新手引导页面
intent = new Intent(getApplicationContext(),GuideActivity.class);
}else
{
//跳到主页面
intent = new Intent(getApplicationContext(),MainActivity.class);
}
startActivity(intent);
finish();//结束当前闪屏应用
}
在此期间封装一个sharePerfences的工具类如下
package smartxinhua.com.smartxinhua.utils;
import android.content.Context;
import android.content.SharedPreferences;
/**
* 封装的工具类
* SharedPreferences的封装
* Created by Zoe on 2016/11/1.
*/
public class PrefUtils {
public static boolean getBoolean(Context ctx,String key,boolean defValue) {
//ctx为当前activity的对象.
SharedPreferences sp = ctx.getSharedPreferences("config",ctx.MODE_PRIVATE);
return sp.getBoolean(key,defValue);
}
public static void setBoolean(Context ctx,String key,boolean value) {
//ctx为当前activity的对象.
SharedPreferences sp = ctx.getSharedPreferences("config",ctx.MODE_PRIVATE);
sp.edit().putBoolean(key,value).commit();
}
public static void setString(Context ctx,String key,String value) {
//ctx为当前activity的对象.
SharedPreferences sp = ctx.getSharedPreferences("config",ctx.MODE_PRIVATE);
sp.edit().putString(key,value).commit();
}
public static String getString(Context ctx,String key,String defValue) {
//ctx为当前activity的对象.
SharedPreferences sp = ctx.getSharedPreferences("config",ctx.MODE_PRIVATE);
return sp.getString(key,defValue);
}
public static void setInt(Context ctx,String key,int value) {
//ctx为当前activity的对象.
SharedPreferences sp = ctx.getSharedPreferences("config",ctx.MODE_PRIVATE);
sp.edit().putInt(key,value).commit();
}
public static int getInt(Context ctx,String key,int defValue) {
//ctx为当前activity的对象.
SharedPreferences sp = ctx.getSharedPreferences("config",ctx.MODE_PRIVATE);
return sp.getInt(key,defValue);
}
}
创建两个activity,一个是新手引导页面,一个是主界面.这里就不贴具体的代码了.这里是代码地址https://github.com/ZoeSj/SmartXinHua.git.