安卓开发Activity转换动画

安卓开发运行时,页面切换的动画效果对用户体验有一定影响,以前不知道怎么弄的,还以为是主题自带的偷笑,所以今天就探讨一下画面切换时效果的实现。画面切换主要分两种: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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值