main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView" android:layout_width="60dip" android:layout_height="90dip" android:scaleType="fitXY" android:layout_marginTop="80dip" android:src="@drawable/haha" android:layout_centerHorizontal="true" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/imageView" android:layout_marginTop="35dip" > <Button android:id="@+id/alphaButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="alpha" /> <Button android:id="@+id/scaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="scale" android:layout_marginLeft="15dip" /> <Button android:id="@+id/rotateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="rotate" android:layout_marginLeft="15dip" /> <Button android:id="@+id/translateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="translate" android:layout_marginLeft="15dip" /> </LinearLayout> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="270dip" android:text="@string/hello_world" tools:context=".MainActivity" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Jump To 1" android:layout_below="@id/textView" android:layout_marginTop="20dip" android:layout_alignParentLeft="true" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Jump To 2" android:layout_below="@id/textView" android:layout_marginTop="20dip" android:layout_alignParentRight="true" /></RelativeLayout>
secondActivity布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="120dip" android:layout_height="wrap_content" android:text="The second Activity" android:layout_gravity="center_vertical" android:layout_marginLeft="120dip" /></LinearLayout>
mainActivity如下:
package com.example.testactivityjumpanimation;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity { private Button button1; private Button button2; private Button alphaButton; private Button scaleButton; private Button rotateButton; private Button translateButton; private Animation animation; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Activity跳轉間動畫1 button1=(Button) findViewById(R.id.button1); button1.setOnClickListener(new ButtonClickListener()); //Activity跳轉間動畫1 button2=(Button) findViewById(R.id.button2); button2.setOnClickListener(new ButtonClickListener()); //展示動畫的ImageView imageView=(ImageView) findViewById(R.id.imageView); //測試四個動畫的按鈕 alphaButton=(Button) findViewById(R.id.alphaButton); scaleButton=(Button) findViewById(R.id.scaleButton); rotateButton=(Button) findViewById(R.id.rotateButton); translateButton=(Button) findViewById(R.id.translateButton); alphaButton.setOnClickListener(new ButtonClickListener()); scaleButton.setOnClickListener(new ButtonClickListener()); rotateButton.setOnClickListener(new ButtonClickListener()); translateButton.setOnClickListener(new ButtonClickListener()); } private class ButtonClickListener implements OnClickListener{public void onClick(View v) {switch (v.getId()) {//Activity之間跳轉的動畫1case R.id.button1:Intent intent1=new Intent(MainActivity.this, SecondActicity.class);startActivity(intent1);//注意參數的解釋://第一個參數 enterAnim//A resource ID of the animation resource to use for the incoming activity.//Use 0 for no animation.//第二個參數 exitAnim//A resource ID of the animation resource to use for the outgoing activity. //Use 0 for no animation.//注意該方法應該緊挨著startActivity()或者finish()後調用overridePendingTransition(R.anim.in1, R.anim.out1);break;case R.id.button2:Intent intent2=new Intent(MainActivity.this, SecondActicity.class);startActivity(intent2);overridePendingTransition(R.anim.in2, R.anim.out2);break;//透明度動畫case R.id.alphaButton:animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alphaanimation);imageView.startAnimation(animation);break;//大小變化動畫 case R.id.scaleButton: animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanimation); imageView.startAnimation(animation);break;//旋轉動畫 case R.id.rotateButton: animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotateanimation); imageView.startAnimation(animation);break;//位移動畫 case R.id.translateButton: animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.translateanimation); imageView.startAnimation(animation);break;default:break;}} }}
secondActivity如下:
package com.example.testactivityjumpanimation;import android.app.Activity;import android.os.Bundle;public class SecondActicity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); }}
以下為動畫xml檔案
alphaanimation.xml如下:
<?xml version="1.0" encoding="utf-8"?><!-- Alpha的值界於0.0與1.0之間 --><!-- Alpha=0的時候不可見 --><!-- Alpha=1的時候完全可見 --><!-- repeatCount設定了動畫重複的次數 --><!-- repeatCount=1,那麼動畫一共會執行2次 --><!-- repeatCount的意思是除了原本的執行以後還要執行幾次 --><!-- android:repeatMode有兩種,測試一下即明白 --><!-- android:fillAfter和android:fillBefore只能在set中設定!!!android:fillAfter="true"表示動畫結束後,當前畫面就為動畫結束後的效果android:fillBefore="true"表示動畫結束後,當前畫面就為動畫開始前的效果--><!-- android:startOffset也是在set中設定才有效果--><!-- android:interpolator用於控制動畫執行過程中的速度 --><set xmlns:android="http://schemas.android.com/apk/res/android" android:fillBefore="true" > <alpha android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="10000" android:repeatCount="1" /></set>
scaleanimation.xml如下:
<?xml version="1.0" encoding="utf-8"?><!-- fromX(Y)Scale和toX(Y)Scale的值: --><!-- 0.0表示收縮到沒有 --><!-- 1.0表示正常無收縮 --><!-- 值小於1.0表示收縮 --><!-- 值大於1.0表示放大 --><!-- 注意: --><!-- 應該同時設定X和Y的from和to,否則無效果--><!-- pivot本意是樞軸,中心點的意思 --><!-- pivotX(Y)表示動畫相對於物件的X,Y座標的開始位置 --><!-- pivotX(Y)取值範圍為0%到100% --><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="0.4" android:toXScale="2.0" android:fromYScale="0.4" android:toYScale="2.0" android:pivotX="100%" android:pivotY="50%" android:duration="3000" /></set>
rotateanimation.xml如下:
<?xml version="1.0" encoding="utf-8"?><!-- android:fromDegrees表示動畫開始時相對於原物件的角度 --><!-- android:toDegrees表示動畫結束時相對於原物件旋轉的角度--><!-- 此處使用到的角度:正數表示順時針旋轉 --><!-- 此處使用到的角度:負數表示逆時針旋轉 --><!-- 以下說法不準確 --><!-- from為負數,to為正數,順時針旋轉 --><!-- from為負數,to為負數,逆時針旋轉 --><!-- from為正數,to為正數,順時針旋轉 --><!-- from為正數,to為負數,逆時針旋轉 --><!-- 應該是to減去from=結果 --><!-- 結果大於0,順時針旋轉 --><!-- 結果小於0,逆時針旋轉 --><!-- 和前面描述from和to是一樣的: --><!-- 正數表示順時針旋轉 --><!-- 負數表示逆時針旋轉 --><!-- 更準確應該這麼理解 --><!-- 應該是from減去to=結果 --><!-- 結果大於0,逆時針旋轉 --><!-- 結果小於0,正時針旋轉 --><!-- 這樣符合一貫的理解:順時針為負,逆時針為正 --><!-- pivot本意是樞軸,中心點的意思 --><!-- pivotX(Y)表示動畫開始的時候相對於原來物件的X,Y座標--><!-- pivotX(Y)取值範圍為0%到100% --><set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:fromDegrees="90" android:toDegrees="-90" android:pivotX="50%" android:pivotY="50%" android:duration="4000" /> </set>
translateanimation.xml如下:
<?xml version="1.0" encoding="utf-8"?><!-- fromXDelta 表示動畫開始的點的X離當前View X座標上的差值 toXDelta 表示動畫結束的點的X離當前View X座標上的差值 fromYDelta 表示動畫開始的點的Y離當前View Y座標上的差值 toYDelta 表示動畫開始的點的Y離當前View Y座標上的差值 --><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100" android:toXDelta="0" android:fromYDelta="30" android:toYDelta="0" android:duration="3000" /></set>
in1.xml如下:
<?xml version="1.0" encoding="utf-8"?><!-- 作用於即將出現的Activity --><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" > <scale android:duration="3000" android:fromXScale="2.0" android:toXScale="1.0" android:fromYScale="2.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" /></set>
out1.xml如下:
<?xml version="1.0" encoding="utf-8"?><!-- 作用於即將消失的Activity --><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:zAdjustment="top" > <scale android:duration="5000" android:fromXScale="1.0" android:toXScale="0.5" android:fromYScale="1.0" android:toYScale="0.5" android:pivotX="50%" android:pivotY="50%" /> <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" /></set>
in2.xml如下:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="320" android:toXDelta="0" android:duration="300" /></set>
out2.xml如下:
<?xml version="1.0" encoding="utf-8"?><!-- 設定android:fromXDelta="0"且android:toXDelta="0"作用:防止在Activity頁面跳轉的時候,出現黑屏.且注意:out2中的android:duration和in2中的保持一致--><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:duration="300" /></set>