Andorid Activity跳转动画
下面的Demo是我找到的一个觉得用来在程序中进行Activity之间切换的很好的一个例子:
- package com.asai.android.demo;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.Spinner;
- public class demoMain extends Activity {
- private Button btnDemo;
- private Spinner sprAnim;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btnDemo = (Button) findViewById(R.id.btnDemo);
- sprAnim = (Spinner) findViewById(R.id.sprAnim);
- //Spinner的内容适配器
- ArrayAdapter<?> animType = ArrayAdapter.createFromResource(this,
- R.array.anim_type, android.R.layout.simple_spinner_item);
- animType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- sprAnim.setAdapter(animType);
- //默认选中第一项
- sprAnim.setSelection(0);
- btnDemo.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- startActivity(new Intent(demoMain.this, demoDetail.class));
- switch (sprAnim.getSelectedItemPosition()) {
- case 1:
- //淡入淡出效果
- overridePendingTransition(R.anim.fade, R.anim.hold);
- break;
- case 2:
- overridePendingTransition(R.anim.alpha_rotate,
- R.anim.my_alpha_action);
- break;
- case 3:
- overridePendingTransition(R.anim.alpha_scale_rotate,
- R.anim.my_alpha_action);
- break;
- case 4:
- overridePendingTransition(
- R.anim.alpha_scale_translate_rotate,
- R.anim.my_alpha_action);
- break;
- case 5:
- overridePendingTransition(R.anim.alpha_scale_translate,
- R.anim.my_alpha_action);
- break;
- case 6:
- //中心放大效果
- overridePendingTransition(R.anim.alpha_scale,
- R.anim.my_alpha_action);
- break;
- case 7:
- overridePendingTransition(R.anim.alpha_translate_rotate,
- R.anim.my_alpha_action);
- break;
- case 8:
- overridePendingTransition(R.anim.alpha_translate,
- R.anim.my_alpha_action);
- break;
- case 9:
- overridePendingTransition(R.anim.my_rotate_action,
- R.anim.my_alpha_action);
- break;
- case 10:
- overridePendingTransition(R.anim.my_scale_action,
- R.anim.my_alpha_action);
- break;
- case 11:
- overridePendingTransition(R.anim.my_translate_action,
- R.anim.my_alpha_action);
- break;
- case 12:
- overridePendingTransition(R.anim.myanimation_simple,
- R.anim.my_alpha_action);
- break;
- case 13:
- overridePendingTransition(R.anim.myown_design,
- R.anim.my_alpha_action);
- break;
- case 14:
- overridePendingTransition(R.anim.scale_rotate,
- R.anim.my_alpha_action);
- break;
- case 15:
- overridePendingTransition(R.anim.scale_translate_rotate,
- R.anim.my_alpha_action);
- break;
- case 16:
- overridePendingTransition(R.anim.scale_translate,
- R.anim.my_alpha_action);
- break;
- case 17:
- overridePendingTransition(R.anim.translate_rotate,
- R.anim.my_alpha_action);
- break;
- case 18:
- overridePendingTransition(R.anim.hyperspace_in,
- R.anim.hyperspace_out);
- break;
- case 19:
- overridePendingTransition(R.anim.shake,
- R.anim.my_alpha_action);
- break;
- case 20:
- overridePendingTransition(R.anim.push_left_in,
- R.anim.push_left_out);
- break;
- case 21:
- //从下方移出效果 overridePendingTransition(R.anim.push_up_in,
- R.anim.push_up_out);
- break;
- case 22:
- overridePendingTransition(R.anim.slide_left,
- R.anim.slide_right);
- break;
- case 23:
- overridePendingTransition(R.anim.slide_top_to_bottom,
- R.anim.my_alpha_action);
- break;
- case 24:
- overridePendingTransition(R.anim.wave_scale,
- R.anim.my_alpha_action);
- break;
- }
- }
- });
- }
- }
另外一个activity可以自己随便写个,这里还用到了就是arrays.xml作为Spinner的适配器:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="anim_type">
- <item>zoom_enter</item>
- <item>fade</item>
- <item>alpha_rotate</item>
- <item>alpha_scale_rotate</item>
- <item>alpha_scale_translate_rotate</item>
- <item>alpha_scale_translate</item>
- <item>alpha_scale</item>
- <item>alpha_translate_rotate</item>
- <item>alpha_translate</item>
- <item>my_rotate_action</item>
- <item>my_scale_action</item>
- <item>my_translate_action</item>
- <item>myanimation_simple</item>
- <item>myown_design</item>
- <item>scale_rotate</item>
- <item>scale_translate_rotate</item>
- <item>scale_translate</item>
- <item>translate_rotate</item>
- <item>hyperspace_in</item>
- <item>shake</item>
- <item>push_left_in</item>
- <item>push_up_in</item>
- <item>slide_left</item>
- <item>slide_top_to_bottom</item>
- <item>wave_scale</item>
- </string-array>
- </resources>
其实这个Demo最重要的就是anim中的一些xml动画定义:
下面我一个一个的来介绍下(我觉得实用性很好的):
push_up_in.xml:
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="300"/>
- <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
- </set>
相信这段代码我不用解释应该也看的懂:改变y坐标来实现动画效果;
相对应的就是:
push_up_out.xml:
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="300"/>
- <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
- </set>
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=49969