Android主要提供了兩種建立動畫的機制:補間動畫(tweened animation)和逐幀動畫(frame-by-frame animation)。
補間動畫主要完成一些簡單的轉場,例如位置、大小變化;
逐幀動畫主要是依次加載一系列的可繪制資源。
一、補間動畫
1. Tweened Animation可以運用在view,surface或者其它對象上,主要分四類:
Alpha | 透明度漸變動畫 |
Scale | 尺寸漸變動畫 |
Translate | 位置移動動畫 |
Rotate | 畫面旋轉動畫 |
動畫屬性介紹:
Alpha | |
fromAlpha | 動畫起始時透明度,0.0表示完全透明 |
toAlpha | 動畫結束時透明度,1.0表示完全不透明 |
Scale | |
fromXScale | 動畫起始時X坐标上的伸縮尺寸 |
toXScale | 動畫結束時X坐标上的伸縮尺寸 |
fromYScale | 動畫起始時Y坐标上的伸縮尺寸 |
toYScale | 動畫結束時Y坐标上的伸縮尺寸 |
pivotX | 動畫相對于物件的X坐标的開始位置 |
pivotY | 動畫相對于物件的Y坐标的開始位置 |
Translate | |
fromXDelta | 動畫起始時X坐标上的位置 |
toXDelta | 動畫結束時X坐标上的位置 |
fromYDelta | 動畫起始時Y坐标上的位置 |
toYDelta | 動畫結束時Y坐标上的位置 |
Rotate | |
fromDegrees | 動畫起始時物件的角度 |
toDegrees | 動畫結束時物件旋轉的角度,可以大于360度 |
pivotX | 動畫相對于物件的X坐标的開始位置 |
pivotY | 動畫相對于物件的Y坐标的開始位置 |
2. 使用Animation的方式有兩種,一種是在XML中定義動畫,另一種是在Java代碼中定義動畫。
在XML中定義動畫
示例一:将動畫應用在View上
(1) 建立animation,建立res\anim\alpha.xml。
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1" android:toAlpha="0"
android:duration="@android:integer/config_longAnimTime"/>
(2) 建立layout,建立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"
>
<TextView android:id="@+id/text01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/alpha" />
</LinearLayout>
(3) 建立Activity,AnimationActivity.java。通過AnimationUtils.loadAnimation加載動畫,然後aView.startAnimation來運作動畫。
package com.xeedroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
public class AnimationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button01).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
findViewById(R.id.text01).startAnimation(
AnimationUtils.loadAnimation(AnimationActivity.this,
R.anim.alpha));
}
});
}
}
(4) 運作應用,點選Alpha按鈕,文本資訊會出現漸變效果。
示例二:将動畫應用在Activity上
Android官方示例,com.example.android.apis.app.Animation。在此隻列出關鍵部分。
(1) animation xml檔案。
res/anim/fade.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_longAnimTime" />
res/anim/hold.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0" android:toXDelta="0"
android:duration="@android:integer/config_longAnimTime" />
res/anim/zoom_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Special window zoom animation: this is the element that enters the screen,
it starts at 200% and scales down. Goes with zoom_exit.xml. -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromXScale="2.0" android:toXScale="1.0"
android:fromYScale="2.0" android:toYScale="1.0"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
res/anim/zoom_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Special window zoom animation: this is the element that exits the
screen, it is forced above the entering element and starts at its
normal size (filling the screen) and scales down while fading out.
This goes with zoom_enter.xml. -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top">
<scale android:fromXScale="1.0" android:toXScale=".5"
android:fromYScale="1.0" android:toYScale=".5"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
<alpha android:fromAlpha="1.0" android:toAlpha="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
(2) 主要Activity 檔案,com/example/android/apis/app/Animation.java。通過overridePendingTransition方法将自定義動畫覆寫Activity之間跳轉時預設的動畫,
package com.example.android.apis.app;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R;
import com.example.android.apis.view.Controls1;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* <p>Example of explicitly starting and stopping the {@link LocalService}.
* This demonstrates the implementation of a service that runs in the same
* process as the rest of the application, which is explicitly started and stopped
* as desired.</p>
*/
public class Animation extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.fade_animation);
button.setOnClickListener(mFadeListener);
button = (Button)findViewById(R.id.zoom_animation);
button.setOnClickListener(mZoomListener);
}
private OnClickListener mFadeListener = new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Animation.this, Controls1.class));
overridePendingTransition(R.anim.fade, R.anim.hold);
}
};
private OnClickListener mZoomListener = new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Animation.this, Controls1.class));
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
};
}
在Java代碼中定義動畫
示例一:
(1) layout檔案同上文的res\layout\main.xml。
(2) 修改AnimationActivity.java代碼。在代碼裡建立animation并設定屬性。
package com.xeedroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
public class AnimationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button01).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 建立rotate animation
Animation anim = new RotateAnimation(0, 360);
anim.setDuration(5000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
findViewById(R.id.text01).startAnimation(anim);
}
});
}
}
二、逐幀動畫
後續