赞
踩
随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
在startActivity或finish()后,调用overridePendingTransition方法,可以加入动画效果。例如:
使用Android自带的淡入淡出:android.R.anim.fade_in,android.R.anim.fade_out。
使用Android自带的由左向右滑入的效果:android.R.anim.slide_in_left,android.R.anim.slide_out_right。
也可以自定义动画效果。
下面用一个简单的示例来演示,代码如下:
MainActivity:
- package com.home.activity;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- import com.home.R;
-
- public class MainActivity extends Activity {
- private Button turnBtn;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- turnBtn = (Button) findViewById(R.id.main_btn_turn);
- turnBtn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(MainActivity.this,
- SecondActivity.class);
- startActivity(intent);
- // 第一个参数是目标Activity进入时的动画,第二个参数是当前Activity退出时的动画
- overridePendingTransition(R.anim.slide_in_left,
- R.anim.slide_out_right);
- }
- });
- }
-
- }

MainActivity的布局文件就一个按钮,在此省略。至于SecondActivity自己定义即可,这里就不给出了。
slide_in_left动画:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
-
- <translate
- android:duration="3000"
- android:fromXDelta="-50%p"
- android:toXDelta="0" />
-
- <alpha
- android:duration="3000"
- android:fromAlpha="0.0"
- android:toAlpha="1.0" />
-
- </set>
slide_out_right动画:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
-
- <translate
- android:duration="3000"
- android:fromXDelta="0"
- android:toXDelta="50%p" />
-
- <alpha
- android:duration="3000"
- android:fromAlpha="1.0"
- android:toAlpha="0.0" />
-
- </set>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。