1 android高级编程——程序启动动画的实现

启动动画的原理:程序启动后加载一个只有一个图片的activity页面,该页面占满全屏。一段时间后,关闭当前activity进入主页面或登录页面的activity

public class SplashActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_splash);
		new Handler().postDelayed(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SplashActivity.this,MainActivity.class);
				SplashActivity.this.startActivity(intent);//启动新页面
				SplashActivity.this.finish();//启动页面不需要压栈回退
			}
		}, 1000);//当前页面停留时间大约为1S 
		this.overridePendingTransition(R.anim.enter_alpha, R.anim.exit_alpha);//页面跳转动画设置
	}
}

        动画切换的资源文件放置在res/anim文件夹。

        exit_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
  <alpha android:fromAlpha="1.0"
      android:toAlpha="0.0"
      android:duration="500"/>
</set>

        enter_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
  <alpha android:fromAlpha="0.1"
      android:toAlpha="1.0"
      android:duration="500"/>  

</set>

        Splash采用了一个图片占满全屏,可用多种方式实现,例如可用一个层布局空间,将图片设置为其背景:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  Android:background="@drawable/yancao"
    tools:context=".SplashActivity" >
</RelativeLayout>

        需要注意的一点是,activity刚显示的并不是显示的xml文件布局所见的界面,而是它的主题页面,当activity调用onCreate函数后才重绘了xml布局的界面。所以你的splash很可能会闪出如下右边界面再转为右边xml文件定义的splash界面。


        这种页面切换导致的和软件整体风格不和谐画面是应该尽量避免,方法就是设置这个splashactivity的主题。上面左图的主题是:Theme.Light ,这个主题界面背景默认是白色的有标题栏;如果将主题改成:Theme.NoTitlebar ,默认是黑色背景无标题栏。修改之后,在进入定义的splash图片之前,屏幕是全黑的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值