不用线程做Android软件欢迎界面,透明效果,完成后自动跳转
最近在网上看到一些Android软件的欢迎界面做得都挺复杂的(个人觉得),因为一般都用到了线程,接着就想有没有简单一点的办法。然后就有了下文:
这个欢迎界面主要是借助Animation动画来实现的(效果如图),不需要用到线程。实现的方法很简单,为动画设置监听就可以了,在动画播放结束时结束欢迎界面并跳转到软件的主界面。
Java代码
1./**
2. * 欢迎界面
3. * @author 小建枫叶
4. *
5. */
6.public class WelcomeActivity extends Activity implements AnimationListener {
7. private ImageView imageView = null;
8. private Animation alphaAnimation = null;
9.
10. @Override
11. protected void onCreate(Bundle savedInstanceState) {
12.
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.welcome);
15. imageView = (ImageView)findViewById(R.id.welcome_image_view);
16. alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.welcome_alpha);
17. alphaAnimation.setFillEnabled(true); //启动Fill保持
18. alphaAnimation.setFillAfter(true); //设置动画的最后一帧是保持在View上面
19. imageView.setAnimation(alphaAnimation);
20. alphaAnimation.setAnimationListener(this); //为动画设置监听
21. }
22.
23. @Override
24. public void onAnimationStart(Animation animation) {
25.
26. }
27.
28. @Override
29. public void onAnimationEnd(Animation animation) {
30. //动画结束时结束欢迎界面并转到软件的主界面
31. Intent intent = new Intent(this, MainActivity.class);
32. startActivity(intent);
33. this.finish();
34. }
35.
36. @Override
37. public void onAnimationRepeat(Animation animation) {
38.
39. }
40.
41. @Override
42. public boolean onKeyDown(int keyCode, KeyEvent event) {
43. //在欢迎界面屏蔽BACK键
44. if(keyCode==KeyEvent.KEYCODE_BACK) {
45. return false;
46. }
47. return false;
48. }
49.
50.}
/**
* 欢迎界面
* @author 小建枫叶
*
*/
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.anim.welcome_alpha);
alphaAnimation.setFillEnabled(true); //启动Fill保持
alphaAnimation.setFillAfter(true); //设置动画的最后一帧是保持在View上面
imageView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(this); //为动画设置监听
}
@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) {
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//在欢迎界面屏蔽BACK键
if(keyCode==KeyEvent.KEYCODE_BACK) {
return false;
}
return false;
}
}
动画welcome_alpha.xml
Xml代码
1.<?xml version="1.0" encoding="utf-8"?>
2.<set xmlns:android="http://schemas.android.com/apk/res/android"
3. android:interpolator="@android:anim/accelerate_interpolator">
4. <alpha
5. android:fromAlpha="0.0"
6. android:toAlpha="1.0"
7. android:duration="2000"
8. />
9. <alpha
10. android:fromAlpha="1.0"
11. android:toAlpha="0.0"
12. android:startOffset="3000" //延迟3秒再开始
13. android:duration="3000"
14. />
15.</set>
<?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:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="3000" //延迟3秒再开始
android:duration="3000"
/>
</set>
布局welcome.xml
Xml代码
1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="fill_parent"
4. android:layout_height="fill_parent"
5. androidrientation="vertical"
6. android:gravity="center_vertical|center_horizontal">
7. <ImageView
8. android:id="@+id/welcome_image_view"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:src="@drawable/welcome"
12. />
13.</LinearLayout> |