安卓开发运行时,页面切换的动画效果对用户体验有一定影响,以前不知道怎么弄的,还以为是主题自带的,所以今天就探讨一下画面切换时效果的实现。画面切换主要分两种:Activity与Fragment,由于两者有相似性,所以今天重点讨论Activity的画面切换。主代码如下,非常简单!
package com.example.activitytransfer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
/**
* activity的画面转换动画
* 方法:overridePendingTransition(enterAnim, exitAnim);
* 参数:1、enterAnim:新activity页面进入时的动画
* 2、exitAnim:旧activity页面推出时的动画
*
* */
public class MainActivity extends Activity {
private Button mButton1;
private Button mButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initView();
initAction();
}
private void initAction() {
// TODO Auto-generated method stub
}
private void initView() {
// TODO Auto-generated method stub
mButton1 = (Button) findViewById(R.id.button1);
mButton1 = (Button) findViewById(R.id.button2);
}
public void click(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//button1切换方式:从右到左
case R.id.button1:
Intent intent1 = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent1);
overridePendingTransition(R.anim.right_to_middle, R.anim.middle_to_left);
break;
//button2切换方式:从上到下
case R.id.button2:
Intent intent2 = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent2);
overridePendingTransition(R.anim.down_to_middle, R.anim.middle_to_up);
break;
default:
break;
}
}
}
下面是MainActivity布局文件,也非常简单:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="从左到右"
android:onClick="click" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="从下到上"
android:onClick="click" />
</LinearLayout>
</RelativeLayout>
SecondActivity没什么内容,这里就不赘述了,接下来是动画部分:res/anim下新建XML文件(其中有关屏幕的基础知识点也不赘述了)
1、down_to_middle.xml,由屏幕下方向上滑到屏幕中间
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0"
android:duration="1000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="1000" />
</set>
2、middle_to_up.xml,由屏幕中间向上滑出屏幕
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0" android:toYDelta="-100%p"
android:duration="1000"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="1000" />
</set>
3、right_to_middle.xml,
由屏幕右方向左滑入屏幕中间
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="1000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="1000" />
</set>
4、right_to_middle.xml,由屏幕中间向左滑出屏幕
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="1000"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="1000" />
</set>
好了,核心代码就这些了,是不是很简单,同学们可以自己试着写出更炫酷的页面切换效果!
另外,Fragment的切换实现主要是setCustomAnimation()方法实现,有兴趣的可以看看链接的文章,写的很详细。
http://www.cnblogs.com/mengdd/p/3494041.html