Android中的動畫(幀動畫、補間動畫、屬性動畫)
總的來說,安卓動畫可以分為兩類,最初的傳統動畫和Android3.0之後的屬性動畫。
傳統動畫包括:幀動畫( Frame Animation)和補間動畫(Tweened Animation)。
下面來具體說一下各種動畫的使用及特點:
幀動畫:是最容易實現的一種動畫,它依賴於完善的UI資源;實現原理是將一張張單獨的圖片連貫起來進行播放,從而在視覺上產生動畫的效果,類似於某些製作gif動畫的方式。
建立XML檔案要放在/res/drawable/目錄下,必須以animation-list為根元素,以item表示要輪換顯示的圖片,duration屬性表示各項顯示的時間。
xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/a_0"
android:duration="100" />
<item
android:drawable="@drawable/a_1"
android:duration="100" />
<item
android:drawable ="@drawable/a_2"
android:duration="100" />
...
</animation-list>
java :
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animation);
ImageView animationImg1 = (ImageView) findViewById(R.id .animation1);
animationImg1.setImageResource(R.drawable.frame_anim1);
AnimationDrawable animationDrawable1 = (AnimationDrawable) animationImg1.getDrawable();
animationDrawable1.start();
}
補間動畫:就是對場景裡的物件不斷的進行影象變化來產生動畫效果(旋轉、平移、放縮和漸變)。分別是 alpha(淡入淡出),translate(位移),scale(縮放大小),rotate(旋轉)。
補間動畫的實現方式:一種使用XML檔案(檔案放在res/anim),一種直接程式碼搞定動畫型別
一般會採用xml 檔案的形式;程式碼會更容易書寫和閱讀,同時也更容易複用。
首先,在res/anim/ 資料夾下定義如下的動畫實現方式
xml實現:
alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toAlpha="0.0" />
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"/>
java:
Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.alpha_anim);
img = (ImageView) findViewById(R.id.img);
img.startAnimation(animation);
使用set 標籤將多個動畫組合(程式碼源自Android SDK API):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/interpolator_resource"
android:shareInterpolator=["true" | "false"] >
<alpha
android:fromAlpha="float"
android:toAlpha="float" />
<scale
android:fromXScale="float"
android:toXScale="float"
android:fromYScale="float"
android:toYScale="float"
android:pivotX="float"
android:pivotY="float" />
<translate
android:fromXDelta="float"
android:toXDelta="float"
android:fromYDelta="float"
android:toYDelta="float" />
<rotate
android:fromDegrees="float"
android:toDegrees="float"
android:pivotX="float"
android:pivotY="float" />
<set>
...
</set>
</set>
可以看到組合動畫是可以巢狀使用的。
第二種java實現方式:
相關的類:
AlphaAnimation漸變透明度動畫效果
Animation animationAlpha = new AlphaAnimation(0.0f, 1.0f);
animationAlpha.setDuration(3000);
ivAnim.startAnimation(animationAlpha);
ScaleAnimation漸變尺寸伸縮動畫效果
Animation animationScale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationScale.setDuration(3000);
ivAnim.startAnimation(animationScale);
TranslateAnimation畫面轉換位置移動動畫效果
Animation animationTranslate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
animationTranslate.setDuration(3000);
ivAnim.startAnimation(animationTranslate);
RotateAnimation畫面轉移旋轉動畫效果
Animation animationRotate = new RotateAnimation(0.0f,+350.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationRotate.setDuration(3000);
ivAnim.startAnimation(animationRotate);
補間動畫缺點:視覺上的效果變化了,但是屬性上的值沒有變,或者真正的位置沒有改變,還可以互動到
屬性動畫和補間動畫的區別:
補間動畫:是通過父容器來不停的繪製view,view控制元件本身是沒有變化的;
屬性動畫:通過不停改變自己view的屬性值,來改變view。
屬性動畫: 在Android 3.0中才引進的,它更改的是物件的實際屬性;為了就是解決補間動畫的缺點。
a.相關的類:ObjectAnimator, ValueAnimator, AnimatorSet, ViewCompat
b.用法:
ObjectAnimator anim = ObjectAnimator.ofInt(view,"transtionX",100);
anim.setDuration(100);
anim.start();
ViewCompat.animate(view).tranxlaionX(100)
.scaleX(1.2f)
.setDuration()
.start();
ValueAnimator的補充:
本質:是幫助我們定義了一個動畫的執行流程,但是不會擁有任何的動畫邏輯,可以讓我們實現任何動畫效果,比如你想改變一個View的width和height以及背景顏色等屬性。另外還可以實現倒計時效果;
屬性動畫的低版本相容:
Android 3.0以後引入了屬性動畫,屬性動畫可以輕而易舉的辦到許多View動畫做不到的事。但是Android3.0以下是無法使用屬性動畫的,如果需要在Android3.0以下版本中使用屬性動畫,需要使用一個開源的jar包:nineoldandroids.jar(點選下載)