屬性動畫的詳細用法
阿新 • • 發佈:2018-12-15
ObjectAnimator
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();
動畫屬性值(x和y的值是以你當前的位置為參考)
translationX和translationY:增量控制view從它佈局容器左上角座標偏移
ObjectAnimator.ofFloat(imageView, "translationX", 300f);
rotation、rotationX、rotationY:控制view繞支點進行2D或3D旋轉
ObjectAnimator.ofFloat(imageView, "rotation", 360);
scaleX、scaleY:控制view繞支點進行2D縮放
ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.5f,1f);
alpha:控制view透明度,預設是1(不透明),0完全透明(不可見)
ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.5f);
x和y:描述view在容器最終位置
動畫監聽
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) { } });
ValueAnimator
ValueAnimator 本身不提供任何動畫效果,像個數值 發生器,用來產生具有一點規律數字。
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
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();
}
});
AnimatorSet
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.5f);
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();