【Android基礎知識】Drawable Animation和View Animation

Android中的動畫主要分爲三類

1.Drawable Animation

2.View Animation

3.Property Animation

這裏介紹其中的兩類,Drawable Animation(逐幀動畫)和View Animation

Drawable Animation 逐幀播放每一張圖片,就好像動畫播放一樣。

View Animation  包括 平移動畫 translate 、縮放動畫 scale 、旋轉動畫 rotate 、漸變動畫 alpha

Drawable Animation (逐幀動畫)

我們使用 animation-list 來定義動畫xml文件,在工程res的anim文件夾或者drawable文件夾下建立我們的動畫文件

anim_list.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
<!-- onshot 爲true表示只播放一次,false表示循環播放 -->
    <item
        android:drawable="@drawable/one"
        android:duration="500"/>
    <item
        android:drawable="@drawable/two"
        android:duration="500"/>
    <item
        android:drawable="@drawable/three"
        android:duration="500"/>
    <item
        android:drawable="@drawable/four"
        android:duration="500"/>
    <item
        android:drawable="@drawable/five"
        android:duration="500"/>
    <item
        android:drawable="@drawable/six"
        android:duration="500"/>
</animation-list>
使用AnimationDrawable 啓動動畫,代碼如下,調用stop可以停止動畫

private AnimationDrawable walkDrawable;
image.setImageResource(R.drawable.anim_list);
			walkDrawable = (AnimationDrawable)image.getDrawable();
			walkDrawable.start();

View Animation

在代碼中定義動畫這裏不做介紹,主要介紹在xml中定義動畫
漸變動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" >
    </alpha>

</set>
縮放動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:duration="2000"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>
平移動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="1000"
        android:fromXDelta="10"
        android:fromYDelta="10"
        android:toXDelta="100"
        android:toYDelta="100" />
</set>
旋轉動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="+360" />
</set>
加載xml動畫的方式爲
Animation loadAnimation;
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.xxx);
view.startAnimation(loadAnimation);
縮放效果


旋轉效果

透明度效果
平移效果

動畫續播1,動畫1播放完畢後播放動畫2,這裏使用了動畫監聽器,監聽到動畫1播放完畢的時候開始播放動畫2
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
			image.startAnimation(loadAnimation);
final Animation loadAnimation2 = AnimationUtils.loadAnimation(this,
					R.anim.rotate);
			//給動畫1設置監聽
			loadAnimation.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation arg0) {

				}

				@Override
				public void onAnimationRepeat(Animation arg0) {

				}

				@Override
				public void onAnimationEnd(Animation arg0) {
					//動畫1播放完畢播放動畫2
					image.startAnimation(loadAnimation2);
				}
			});
動畫續播2:直接在xml文件中進行動畫的定義
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="3000"
        android:fromAlpha="0.2"
        android:toAlpha="1.0" />
    <alpha
        android:duration="3000"
        android:fromAlpha="1.0"
        android:startOffset="3000"
        android:toAlpha="0.2" />

</set>
調用
loadAnimation = AnimationUtils.loadAnimation(this,R.anim.continue_anim);
image.startAnimation(loadAnimation);


閃爍效果,有透明變爲不透明,設置倒序重複模式
//閃爍效果
			AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
			alphaAnimation.setDuration(100);
			alphaAnimation.setRepeatCount(10);
			//倒序重複REVERSE  正序重複RESTART
			alphaAnimation.setRepeatMode(Animation.REVERSE);
			image.startAnimation(alphaAnimation);

抖動動畫,就是左右平移,和閃爍效果一樣,也是設置反向重複模式
TranslateAnimation translate = new TranslateAnimation(-50, 50,
					0, 0);
			translate.setDuration(1000);
			translate.setRepeatCount(Animation.INFINITE);
			translate.setRepeatMode(Animation.REVERSE);
			image.startAnimation(translate);
overridePendingTransition(設置界面切換動畫)
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator" >
  
  <scale
        android:duration="1000"
        android:fromXScale="0.1"
        android:fromYScale="0.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
  <alpha
        android:duration="1000"
        android:fromAlpha="0"
        android:toAlpha="1.0" />
</set>
zoom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:zAdjustment="top" >
    <scale
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toXScale="0.1"
        android:toYScale="0.1" />
    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="1.0"
        android:toAlpha="0" />
</set>
設置界面跳轉動畫
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);


ListView佈局動畫,多個item同時執行一種效果。如果我們需要一個界面中的多個控件按照相同的動畫方式但是每個控件完成該動畫的時刻不同的話,就可採用LayoutAnimationController
LayoutAnimationController lac=new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.zoom_in));
	    //順序播放
	    lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
	    listView.setLayoutAnimation(lac);
	    listView.startLayoutAnimation();



發佈了53 篇原創文章 · 獲贊 39 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章