Android 3.0以前已有動畫框架Animation(詳見:Android之視圖動畫Animation),但存在一些侷限性,當某個元素髮生視圖動畫後,其響應事件位置還在動畫前的地方。因而3.0以後,Google提出了屬性動畫。
android
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "translationX", 300);
objectAnimator1.setInterpolator(new AccelerateInterpolator());
objectAnimator1.setDuration(2000);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);//Animation.INFINITE 表示重複屢次
objectAnimator.setRepeatMode(ValueAnimator.RESTART);//RESTART表示從頭開始,REVERSE表示從末尾倒播
objectAnimator1.start();
第一個參數:操縱的view
第二個參數:操縱的動畫屬性值
第三個參數:可變數組參數api
translationX和translationY:增量控制view從它佈局容器左上角座標偏移數組
ObjectAnimator.ofFloat(imageView, "translationX", 300f);
rotation、rotationX、rotationY:控制view繞支點進行2D或3D旋轉框架
ObjectAnimator.ofFloat(imageView, "rotation", 360);
scaleX、scaleY:控制view繞支點進行2D縮放ide
ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.5f,1f);
alpha:控制view透明度,默認是1(不透明),0徹底透明(不可見)佈局
ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.5f);
x和y:描述view在容器最終位置動畫
能夠有一個到N個,若是是一個值的話默認這個值是動畫過渡值的結束值。若是有N個值,動畫就在這N個值之間過渡。spa
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);
objectAnimator1.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
通常咱們只關心onAnimationEnd,因此Android提供了AnimatorListenerAdapter:code
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);ValueAnimator
objectAnimator1.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
ValueAnimator 自己不提供任何動畫效果,像個數值 發生器,用來產生具備一點規律數字。xml
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);PropertyValuesHolder
valueAnimator.setTarget(imageView);
valueAnimator.setDuration(2000).start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Int value = (Integer) animation.getAnimatedValue();
//TODO use the value
Toast.makeText(getApplicationContext(), "value=" + value, Toast.LENGTH_LONG).show();
}
});
針對同一個對象多個屬性,同時做用多種動畫
PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("translationX", 300f);AnimatorSet
PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);
PropertyValuesHolder propertyValuesHolder3 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f);
PropertyValuesHolder propertyValuesHolder4 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f);
ObjectAnimator.ofPropertyValuesHolder(imageView, propertyValuesHolder1, propertyValuesHolder2, propertyValuesHolder3, propertyValuesHolder4)
.setDuration(5000).start();
與PropertyValuesHolder相似,但AnimatorSet多了playTogether(同時執行)、playSequentially(順序執行)、play(objectAnimator1).with(objectAnimator2)、before、after這些方法協同工做。
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.5f);xml使用屬性動畫
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(imageView, "translationY", 300);
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(5000);
animatorSet.playTogether(objectAnimator1, objectAnimator2,objectAnimator3);
animatorSet.start();
res下創建animator文件夾,而後創建res/animator/set_animator.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="alpha"
android:valueFrom="0.1"
android:valueTo="1.0"
android:valueType="floatType" />
調用:
Animator animator = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.set_animator);
animator.setTarget(imageView);
animator.start();
動畫組合
set標籤,有一個orderring屬性設置爲together,還有另外一個值:sequentially(表示一個接一個執行)。
<?xml version="1.0" encoding="utf-8"?>View的animate方法
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:duration="1000"
android:propertyName="scaleX"
android:valueFrom="1"
android:valueTo="0.5" />
<objectAnimator
android:duration="1000"
android:propertyName="scaleY"
android:valueFrom="1"
android:valueTo="0.5" />
</set>
Android 3.0後,谷歌給View增長animate方法直接驅動屬性動畫。
imageView.animate()佈局動畫
.alpha(0.5f)
.y(300)
.setDuration(2000)
//api min is 16
.withStartAction(new Runnable() {
@Override
public void run() {
}
})
//api min is 16
.withEndAction(new Runnable() {
@Override
public void run() {
}
})
.start();
設置子View過渡動畫
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_vertical_margin">
<ImageView
android:id="@+id/imageMove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher" />
</LinearLayout>
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
ScaleAnimation scaleAnimation=new ScaleAnimation(0,1,0,1);
scaleAnimation.setDuration(2000);
LayoutAnimationController layoutAnimationController=new LayoutAnimationController(scaleAnimation,0.5f); layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL); parentLayout.setLayoutAnimation(layoutAnimationController);