首先在res中新建一個anim的文件夾 php
在anim中新建須要的動畫xml資源文件(這裏我把四個都寫出來) java
anim/alpha.xml(漸變更畫) android
<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="2000" android:fromAlpha="0.1" android:toAlpha="1.0" /> </set>
duration:動畫的持續時間(單位是毫秒) app
fromAlpha:開始的透明度 ide
toAlpha:結束的透明度 學習
透明度0.0是徹底透明,1.0是不透明 動畫
anim/scale(伸縮動畫) this
<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="2000" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="00%" android:pivotY="00%" android:toXScale="1.4" android:toYScale="1.4" /> </set>
interpolator:動畫插入器。 spa
accelerate_decelerate_interpolator:加速-減速 code
accelerate_interpolator:加速
decelerate_interpolator:減速
fromXScale,fromYScale:開始X和Y的大小(0爲縮小到沒有)
toXScale,toYScale:結束X和Y的大小(1爲沒有變化)
pivotX,privotY:左上角的座標(能夠用百分比也能夠用數字)
anim/translate(移動動畫)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="2000" android:fromXDelta="30" android:fromYDelta="30" android:toXDelta="-80" android:toYDelta="300" /> </set>fromXDelta,fromYDelta:開始時候左上角的座標
toXDelta,toYDelta:結束時候左上角的座標
anim/rotate(旋轉動畫的)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="3000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+360" />
</set>
interpolator:動畫插入器。
accelerate_decelerate_interpolator:加速-減速
accelerate_interpolator:加速
decelerate_interpolator:減速
fromDegrees:開始的角度
toDegrees:結束的角度
pivotX,privotY:旋轉中心的座標(能夠用百分比也能夠用數字)
到此xml資源文件就完成了,接下來就是如何調用這些資源文件。
以alpha.xml爲例子,先創建AlphaActivity
activity_alpha.xml
<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" android:background="@drawable/start_bg" tools:context=".AlphaActivity" > </RelativeLayout>background是我本身的一張圖片,能夠使用其餘的
AlphaActivity.java
package com.example.animationdemo; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.widget.Toast; public class AlphaActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view=View.inflate(this, R.layout.activity_alpha, null); setContentView(view); Animation animation=AnimationUtils.loadAnimation(this, R.anim.alpha); view.startAnimation(animation); animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation arg0) {} @Override public void onAnimationRepeat(Animation arg0) {} @Override public void onAnimationEnd(Animation arg0) { Toast.makeText(AlphaActivity.this, "動畫完成", Toast.LENGTH_SHORT).show(); } }); } }這段代碼對於入門的同窗來講應該不是問題了,要注意的是這裏
View view=View.inflate(this, R.layout.activity_alpha, null); setContentView(view);不然動畫是沒法start的。以後我設置了動畫監聽器,能夠加上本身喜歡的事件。
經過在http://www.eoeandroid.com/forum.php?mod=viewthread&tid=564學習後寫出這篇博文