原创文章,转载请指明出处并保留原文url地址
最近在网上看到一些Android软件的欢迎界面一般都用到了线程,接着就想有没有简单一点的办法。然后就有了下文:这个欢迎界面主要是借助Animation动画来实现的(效果如图),不需要用到线程。
实现的方法很简单,为动画设置监听就可以了,在动画播放结束时结束欢迎界面并跳转到软件的主界面。
程序创建完成后目录结构如下:
其中代码如下:
WelcomeActivity.java
/*
* Copyright (C) 2010 welcome authors
*
* @author shao
*
* @date 2013.9.20
*
* @blog http://www.iigrowing.cn/
*
* 本程序是一个android客户端软件演示程序
*
*/
package cn.iigrowing.android.welcome;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
public class WelcomeActivity extends Activity implements AnimationListener {
private ImageView imageView = null;
private Animation alphaAnimation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
imageView = (ImageView) findViewById(R.id.welcome_image_view);
alphaAnimation = AnimationUtils.loadAnimation(this, R.layout.welcome_alpha);
alphaAnimation.setFillEnabled(true); // 启动Fill保持
alphaAnimation.setFillAfter(true); // 设置动画的最后一帧是保持在View上面
imageView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(this); // 为动画设置监听
imageView.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.welcome_image_view:
showHelpWeb();
break;
}
}
};
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// 动画结束时结束欢迎界面并转到软件的主界面
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
this.finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
public void showHelpWeb() {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("http://www.iigrowing.cn/");
intent.setData(content_url);
startActivity(intent);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 在欢迎界面屏蔽BACK键
if (keyCode == KeyEvent.KEYCODE_BACK) {
return false;
}
return false;
}
}
MainActivity.java
/*
* Copyright (C) 2010 welcome’s authors
*
* @author shao
*
* @date 2013.9.20
*
* @blog http://www.iigrowing.cn/
*
* 本程序是一个android客户端软件演示程序
*
*/
package cn.iigrowing.android.welcome;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/welcome_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/welcome" />
</LinearLayout>
welcome_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<alpha
android:duration="3000"
android:fromAlpha="1.0"
android:startOffset="3000"
android:toAlpha="0.0" />
</set>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>