码迷,mamicode.com
首页 > 移动开发 > 详细

Android之带渐变动画的欢迎界面

时间:2015-06-18 13:38:36      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:welcome   android   splash   alpha   animation   

这是一个简单的例子,主要功能是在欢迎界面是用一个渐变动画,当动画播放完成后,跳转到主界面中去。


首先需要在res文件夹下新建一个anim文件夹,这个文件夹用来存放动画定义的xml文件,渐变动画的内容是:

<?xml version="1.0" encoding="UTF-8"?>

<!-- 
	alpha表示渐变动画
	duration设置动画播放时长
	fromAlpha表示动画开始时的透明度 
	toAlpha表示动画结束时的透明度
      (0表示完全透明 1表示完全不透明)
 -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="0.3"
    android:toAlpha="1.0" />

接着我们在欢迎界面里面去使用AnimationUtils加载刚刚定义的动画,然后播放动画、监听动画,

需要注意的是这里需要使用LayoutInflater这个类加载res资源下的布局xml文件,不能使用findViewById(),

详细请参考以下代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;

import com.bear.notes.R;
import com.bear.notes.utils.CommonUtil;

public class SplashActivity extends Activity {
	
	private Handler mHandler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CommonUtil.setNoTitleBar(SplashActivity.this);
		CommonUtil.setFullScreen(SplashActivity.this);
		//使用LayoutInflater来加载activity_splash.xml视图
		View rootView = LayoutInflater.from(this).inflate(R.layout.activity_splash, null);
		
		/**
		 * 这里不能使用findViewById(R.layout.acitivyt_spash)方法来加载
		 * 因为还没有开始调用setContentView()方法,也就是说还没给当前的Activity
		 * 设置视图,当前Activity Root View为null,findViewById()方法是从当前
		 * Activity的Root View中获取子视图,所以这时候会报NullPointerException异常
		 * 
		 * View rootView = findViewById(R.layout.activity_splash);
		 * 
		 */

		setContentView(rootView);
		mHandler = new Handler();
		
		//初始化渐变动画
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
		//设置动画监听器
		animation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				//当监听到动画结束时,开始跳转到MainActivity中去
				mHandler.post(new Runnable() {
					
					@Override
					public void run() {
						Intent i = new Intent(SplashActivity.this, MainActivity.class);						
						startActivity(i);
						SplashActivity.this.finish();
					}
				});
			}
		});
		
		//开始播放动画
		rootView.startAnimation(animation);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.splash, menu);
		return true;
	}

}


希望以上内容对你有所帮助。

Android之带渐变动画的欢迎界面

标签:welcome   android   splash   alpha   animation   

原文地址:http://blog.csdn.net/bear_huangzhen/article/details/46546249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!