Android屬性動畫(一):ValueAimator和ObjectAnimator的基本使用
補間動畫
在屬性動畫之前,有補間動畫(Animation類)供我們使用,為什麼有了補間動畫還要屬性動畫?
1.補間動畫只能對view進行操作,有很大的侷限性
2.補間動畫只能實現移動,縮放,旋轉,淡進淡出,其他的效果很難做到
3.補間動畫只是改變了view的顯示效果,而沒有改變view的屬性,這是很致命的
屬性動畫
1.ValueAimator
ValueAimator是一種對值進行操作的類
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);//建立valueAnimator對浮點數值0f-1f進行平滑 //ValueAnimator animator = ValueAnimator.ofInt(1, 2);//建立valueAnimator對浮點數值1-2進行平滑 valueAnimator.setDuration(2000);//設定動畫持續時間 valueAnimator.setStartDelay(2000);//設定延遲啟動時間 valueAnimator.setRepeatCount(2);//設定重複次數 valueAnimator.setRepeatMode(MODE_APPEND);//設定重複模式 valueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Float currentValue = (Float) animation.getAnimatedValue(); Log.d(TAG, "The current value is " + currentValue); } }); valueAnimator.start();//啟動動畫
以上是比較常用的方法,就不詳細說了,也很簡單
2.ObjectAnimator
ObjectAnimator是ValueAnimator的子類,所以擁有ValueAnimator的方法和屬性,比較常用
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f, 1f);//淡進淡出 //ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f); //旋轉 //ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", curTranslationX, -500f, curTranslationX);
//在水平方向平移
//ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "scaleY", 1f, 3f, 1f);//y軸方向實現縮放
animator.setDuration(2000);
animator.setStartDelay(2000);
animator.start();
這裡也是用ofFloat建立了一個ObjectAnimator例項,但是引數有些不同,第一個引數textView是想進行操作的物件,這裡的物件可以是任意物件,第二個是對該物件的哪個屬性進行動畫操作,之後的具體的實現。好像第一個,實現的就是淡進淡出透明度從1變為0再變為1。
但是textView是沒有alpha之類的這些屬性的,那是如何操作的呢?
ObjectAnimator並不是直接對傳入的屬性進行操作,而是會去尋找操作的物件對應的get和set方法,也就是說,textView中肯定有setAlpha和getAlpha等之類的方法
3.組合動畫
實現組合動畫功能主要需要藉助AnimatorSet這個類,這個類提供了一個play()方法,如果我們向這個方法中傳入一個Animator物件(ValueAnimator或ObjectAnimator)將會返回一個AnimatorSet.Builder的例項,AnimatorSet.Builder中包括以下四個方法:
after(Animator anim) 將現有動畫插入到傳入的動畫之後執行
after(long delay) 將現有動畫延遲指定毫秒後執行
before(Animator anim) 將現有動畫插入到傳入的動畫之前執行
with(Animator anim) 將現有動畫和傳入的動畫同時執行
ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);
AnimatorSet animSet = new AnimatorSet();
animSet.play(rotate).with(fadeInOut).after(moveIn);
animSet.setDuration(5000);
animSet.start();
4.Animatior監聽器
ObjectAnimator是繼承自ValueAnimator的,而ValueAnimator又是繼承自Animator的,因此不管是ValueAnimator還是ObjectAnimator都是可以使用addListener()這個方法的。另外AnimatorSet也是繼承自Animator的,因此addListener()這個方法算是個通用的方法。
新增監聽器
anim.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
} //動畫開始時呼叫
@Override
public void onAnimationRepeat(Animator animation) {
} //動畫重複時呼叫
@Override
public void onAnimationEnd(Animator animation) {
} //動畫結束時呼叫
@Override
public void onAnimationCancel(Animator animation) {
} //動畫取消時呼叫
});
新增監聽器,一定要實現這四個方法,但是有時我們只想在某一時刻進行監聽,而實現這四個方法就會顯得很臃腫,這是我們的介面卡 AnimatorListenerAdapter 就出來幫忙了
AnimatorListenerAdapter已經把每一個方法都實現好了,因此我們可以選擇性得對某些方法進行重寫
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
}
});
5.用XML編寫動畫
用xml編寫動畫和用java程式碼編寫動畫相比,可重用性更好
如果想要使用XML來編寫動畫,首先要在res目錄下面新建一個animator資料夾,所有屬性動畫的XML檔案都應該存放在這個資料夾當中。然後在XML檔案中我們一共可以使用如下三種標籤:
<animator> 對應程式碼中的ValueAnimator
<objectAnimator> 對應程式碼中的ObjectAnimator
<set> 對應程式碼中的AnimatorSet
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially" >
<objectAnimator
android:duration="2000"
android:propertyName="translationX"
android:valueFrom="-500"
android:valueTo="0"
android:valueType="floatType" >
</objectAnimator>
<set android:ordering="together" >
<objectAnimator
android:duration="3000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360"
android:valueType="floatType" >
</objectAnimator>
<set android:ordering="sequentially" >
<objectAnimator
android:duration="1500"
android:propertyName="alpha"
android:valueFrom="1"
android:valueTo="0"
android:valueType="floatType" >
</objectAnimator>
<objectAnimator
android:duration="1500"
android:propertyName="alpha"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType" >
</objectAnimator>
</set>
</set>
</set>
上面用xml編寫了一個組合動畫,命名為 anim_file 那怎麼樣在java程式碼中去執行?
Animator animator = AnimatorInflater.loadAnimator(context, R.animator.anim_file);
animator.setTarget(view);
animator.start();
以上就是android動畫的基礎。
發現了一個非常詳細的xml編寫動畫並解釋各種屬性的文章,記錄一下
http://www.cnblogs.com/yc-755909659/p/4290114.html