2013/5/17
Java技术qq交流群:JavaDream:251572072
56_activity切换动画与页面切换动画
----------------------------------------------
1.Activity切换动画效果:
这里打算,新的activity进来的时候,由看得见慢慢变的看不见,
旧的activity离去的时候,由看得见,慢慢的看不见
-------------------------------------------------------
下面是这个例子的所有源码:
a.animation新建这个android项目
b./animation/src/com/credream/animation/AnimationActivity.java
package com.credream.animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class AnimationActivity extends Activity {
private ViewFlipper viewFlipper;
private float startX;
private Animation in_lefttoright;
private Animation out_lefttoright;
private Animation in_righttoleft;
private Animation out_righttoleft;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright);
out_lefttoright = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);
in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft);
out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
startX = event.getX();
}else if(event.getAction()==MotionEvent.ACTION_UP){
float endX = event.getX();
if(endX > startX){
viewFlipper.setInAnimation(in_lefttoright);
viewFlipper.setOutAnimation(out_lefttoright);
viewFlipper.showNext();//显示下一页
}else if(endX < startX){
viewFlipper.setInAnimation(in_righttoleft);
viewFlipper.setOutAnimation(out_righttoleft);
viewFlipper.showPrevious();//显示前一页
}
return true;
}
return super.onTouchEvent(event);
}
//main.xml中的按钮会执行下面这个方法
public void openActivity(View v){
//1.使用意图调用要激活的activity对象
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);//2.激活这个对象.OtherActivity
//3.通过这个方法来实现activity的切换,两个int类型的参数,传入动画效果定义文件的id.
//第一个是activity进来的时候的动画,第二个是activity退出的时候的动画.
//对于这里指的是,AnimationActivity-->到OtherActivity
//所以OtherActivity是进来的activity而AnimationActivity是出去的activity
this.overridePendingTransition(R.anim.enteralpha, R.anim.outalpha);//实现Activity切换动画效果
}
}
-----------------------------------------------------------
c./animation/src/com/credream/animation/OtherActivity.java
package com.credream.animation;
import android.app.Activity;
import android.os.Bundle;
public class OtherActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//1.设置这个activity的界面文件.
setContentView(R.layout.other);
}
}
---------------------------------------------------------
d./animation/res/anim/enteralpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 当进来的时候,要由看不见到看得见所以透明度从0到1,持续5秒中 -->
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="5000"
/>
</set>
--------------------------------------
e./animation/res/anim/outalpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 当离开的时候是由看得见到看不见,透明度从1.0到0,持续5秒中. -->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0"
android:duration="5000"
/>
</set>
---------------------------------------------------
f./animation/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开新Activity"
android:onClick="openActivity"
/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
----------------------------------------------------
g./animation/res/layout/other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#6600FF"
>
<!-- android:background="#6600FF"定义背景颜色 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是新Activity"
/>
</LinearLayout>
--------------------------------------------------------------
2.界面切换动画
-----------------
3.当用户手指触摸屏幕,由左边往右边滑动的时候,那么这个时候,屏幕activity会慢慢的,从左到右移除
这时候新的activity会从左边到右边慢慢的显示.
当用户手指触摸屏幕,由右边往左边滑动的时候,相反
--------------------------------------------------------
下面是界面切换动画的所有源码:
---------------------------------
a.新建android项目 :animation
b./animation/src/com/credream/animation/AnimationActivity.java
package com.credream.animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class AnimationActivity extends Activity {
private ViewFlipper viewFlipper;
private float startX;
private Animation in_lefttoright;
private Animation out_lefttoright;
private Animation in_righttoleft;
private Animation out_righttoleft;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//12.这里需要生成,这两个动画对象.
in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright);
out_lefttoright = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);
in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft);
out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
//5.当用户,用手指触摸屏幕由一个点移动到另一个点(从左到右),那么代表,界面往右边移动
//当用户,用手指触摸屏幕由一个点移动到另一个点(从右到左),那么代表,界面往左边移动
//4.找到这个viewFlipper控件,
viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
}
//6.处理触屏事件
@Override
public boolean onTouchEvent(MotionEvent event) {
//7.判断这个事件是什么事件,这个如果是按下的屏幕事件
if(event.getAction()==MotionEvent.ACTION_DOWN){
//8.这里记录按下的时候的,也就是开始的时候的x轴坐标.
startX = event.getX();
//9.手指抬起来的时候,得到结束位置的x轴坐标
}else if(event.getAction()==MotionEvent.ACTION_UP){
float endX = event.getX();
if(endX > startX){//10.往右滑动.
//11.设置进入和出去的动画.
viewFlipper.setInAnimation(in_lefttoright);
viewFlipper.setOutAnimation(out_lefttoright);
viewFlipper.showNext();//11.这时候需要显示下一页
}else if(endX < startX){//12.往左边滑动,这里需要显示前一页.
viewFlipper.setInAnimation(in_righttoleft);
viewFlipper.setOutAnimation(out_righttoleft);
viewFlipper.showPrevious();//显示前一页
}
return true;
}
return super.onTouchEvent(event);
}
//main.xml中的按钮会执行下面这个方法
public void openActivity(View v){
//1.使用意图调用要激活的activity对象
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);//2.激活这个对象.OtherActivity
//3.通过这个方法来实现activity的切换,两个int类型的参数,传入动画效果定义文件的id.
//第一个是activity进来的时候的动画,第二个是activity退出的时候的动画.
//对于这里指的是,AnimationActivity-->到OtherActivity
//所以OtherActivity是进来的activity而AnimationActivity是出去的activity
this.overridePendingTransition(R.anim.enteralpha, R.anim.outalpha);//实现Activity切换动画效果
}
}
------------------------------------------------------------------------
c./animation/src/com/credream/animation/OtherActivity.java
package com.credream.animation;
import android.app.Activity;
import android.os.Bundle;
public class OtherActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//1.设置这个activity的界面文件.
setContentView(R.layout.other);
}
}
--------------------------------------------------------------------
d./animation/res/anim
/animation/res/anim/enter_lefttoright.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 这里用到了平移动画,这里只动x轴坐标就可以了
-100%p:这就是屏幕的宽度:这里的p代表parent,父元素的宽度,都是
手机屏幕宽度,第一页要从-100%p移动到0,持续5秒中.
-->
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="5000"
/>
</set>
-----------------------------------------------------
e./animation/res/anim/enter_righttoleft.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 目前x轴和y轴都是零,而x轴需要移动的x坐标就是窗口的宽度 -->
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="5000"
/>
</set>
----------------------------------------------------------------------
f./animation/res/anim/out_lefttoright.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 这里需要移动从0到100%p,也就是移动一个窗口的宽度. -->
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="5000"
/>
</set>
-------------------------------------------------------
g./animation/res/anim/out_righttoleft.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="5000"
/>
</set>
------------------------------------------------------
h./animation/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- ViewFlipper,这个显示控件中的子控件会被看成一页,
这个控件就是用来实现界面activity的切换的
--> <ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewFlipper"
>
<!-- 第一页 :第一个子控件.-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 用于显示第一页 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一页"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开新Activity"
android:onClick="openActivity"
/>
</LinearLayout>
<!-- 第二页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#339900"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二页"
/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
---------------------------------------------------------
i./animation/res/layout/other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#6600FF"
>
<!-- android:background="#6600FF"定义背景颜色 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是新Activity"
/>
</LinearLayout>
---------------------------------------------
j./animation/res/anim/outalpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 当离开的时候是由看得见到看不见,透明度从1.0到0,持续5秒中. -->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0"
android:duration="5000"
/>
</set>
------------------------------------------------
k./animation/res/anim/enteralpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 当进来的时候,要由看不见到看得见所以透明度从0到1,持续5秒中 -->
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="5000"
/>
</set>
---------------------------------------------------
l.注意:这里的源码,包含了上一个效果的源码