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

来源:互联网 发布:ftp上传工具 for mac 编辑:程序博客网 时间:2024/06/10 02:52

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

public class SplashActivity extends Activity {@Overrideprotected 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(){@Overridepublic void run() {// TODO Auto-generated method stubIntent 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图片之前,屏幕是全黑的。


原创粉丝点击