標籤:
簡介
屬性動畫包含:ObjectAnimator 動畫的執行類ValueAnimator 動畫的執行類AnimatorSet 用於控制一組動畫的執行:線性,一起,每個動畫的先後執行等。AnimatorInflater 使用者載入屬性動畫的xml檔案
ObjectAnimator 單一屬性動畫
縮放X軸:ScaleX
ObjectAnimator().ofFloat(imageView,”ScaleY”,1.0f,0.0f).setDuration(3000).start();
縮放Y軸:ScaleY
ObjectAnimator().ofFloat(imageView,”ScaleX”,1.0f,0.0f).setDuration(3000).start();
旋轉動畫(X軸):rotationX
ObjectAnimator().ofFloat(imageView,”rotationX”,0.0f,360f).setDuration(3000).start();
旋轉動畫(Y軸):rotationY
ObjectAnimator().ofFloat(imageView,”rotationY”,0.0f,360f).setDuration(3000).start();
旋轉動畫(中心點):rotation
ObjectAnimator().ofFloat(imageView,”rotation”,0.0f,360f).setDuration(3000).start();
淡入淡出:alpha
ObjectAnimator().ofFloat(imageView,”alpha”,1.0f,0.0f).setDuration(3000).start();
位移動畫(X軸):
ObjectAnimator().ofFloat(imageView,”translationX”,0.0f,100.0f).setDuration(3000);
位移動畫(Y軸):
ObjectAnimator().ofFloat(imageView,”translationY”,0.0f,100.0f).setDuration(3000);
代碼實現如下
imageView.clearAnimation(); ObjectAnimator objectAnimator=new ObjectAnimator().ofFloat(imageView,"translationX",0.0f,100.0f,0.0f,100.0f).setDuration(3000); objectAnimator.start();
在xml中實現
- 首先需要在res檔案夾下建立animator檔案夾,在其中定義xml檔案如下
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000"> <objectAnimator android:propertyName="scaleX" android:repeatCount="-1" android:valueFrom="0.0f" android:valueTo="1.0f"></objectAnimator></set>
Animator animatorObjext = AnimatorInflater.loadAnimator(this, R.animator.objectanimator); animatorObjext.setTarget(imageView2); animatorObjext.start();
ValueAnimator
imageView2.clearAnimation(); imageView2.setImageResource(R.mipmap.ic_launcher); //這裡的屬性值可以根據需要設定多個 ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.0f, 1.0f, 0.0f, 1.0f);//設定屬性值 animator.setTarget(imageView2);//設定作業對象 animator.setDuration(5000).start();//動畫開始 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { imageView2.setScaleY((Float) animation.getAnimatedValue());//設定Y軸上的變化 imageView2.setScaleX((Float) animation.getAnimatedValue());//設定X軸上的變化 } });
PropertyValuesHolder 組合動畫
- 就是設定多個不同的動畫,同時讓他們載入就好了
- 相應的代碼實現
imageView.clearAnimation(); PropertyValuesHolder scaleX=PropertyValuesHolder.ofFloat("scaleX",1,0,1); PropertyValuesHolder scaleY=PropertyValuesHolder.ofFloat("scaleY",1,0,1); PropertyValuesHolder rotattion=PropertyValuesHolder.ofFloat("rotation",0,360); ObjectAnimator objectAnimator2=new ObjectAnimator().ofPropertyValuesHolder(imageView,scaleX,scaleY,rotattion).setDuration(3000); objectAnimator2.start();
AnimatorSet 組合動畫(可以設定動畫的順序)
ObjectAnimator objectAnimator3=new ObjectAnimator().ofFloat(imageView,"translationX",0.0f,100.0f,0.0f,100.0f).setDuration(3000); ObjectAnimator objectAnimator4=new ObjectAnimator().ofFloat(imageView,"scaleX",1,0,1).setDuration(3000); ObjectAnimator objectAnimator5=new ObjectAnimator().ofFloat(imageView,"rotation",0,360).setDuration(3000); AnimatorSet animatorSet=new AnimatorSet(); //設定動畫同時執行 animatorSet.playTogether(objectAnimator3,objectAnimator4); //設定動畫的執行順序 animatorSet.play(objectAnimator5).after(objectAnimator3); animatorSet.start();
]
AnimatorInflater 載入屬性動畫的xml檔案
- xml還是在res檔案夾下建立的animator檔案夾下的xml檔案
- 相應的xml檔案的定義如下
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together" android:duration="1000"> <!-- ordering 指動畫是一起播放還是一個接一個播放--> <objectAnimator android:propertyName="scaleX" android:valueFrom="0.0" android:valueTo="1.0"></objectAnimator> <objectAnimator android:propertyName="scaleY" android:valueFrom="0.0" android:valueTo="1.0"></objectAnimator></set>
imageView2.clearAnimation(); imageView2.setImageResource(R.mipmap.ic_launcher); Animator animatorMy = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animatorinflater); animatorMy.setTarget(imageView2); animatorMy.start();
Android動畫--屬性動畫Property Animation