Android.animation.ValueAnimator
The main timing engine for property animation that also computes the values for the property to be animated. It has all of the core functionality that calculates animation values and contains the timing details of each animation, information about whether an animation repeats, listeners that receive update events, and the ability to set custom types to evaluate. There are two pieces to animating properties: calculating the animated values and setting those values on the object and property that is being animated. ValueAnimator does not carry out the second piece, so you must listen for updates to values calculated by the ValueAnimator and modify the objects that you want to animate with your own logic. See the section about Animating with ValueAnimator for more information.
기존 Animation 방식에 비해서 View가 아닌 Object 등 모든 것들에 Animation을 적용 할 수 있다.
- TimeInterpolator
- TypeEvaluator
- duration
- startPropertyValue
- endPropertyValue
How to use ValueAnimator
아래와 같이 사용할 수 있다.
ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(1000);
animation.start()
// or (for Object)
ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);
animation.setDuration(1000);
animation.start();
ValueAnimator가 Object에 직접적인 실제 변화를 주지는 않는다. 이를 위해서는 AnimationListener
를 달아서 getAnimatedValue
를 통해 계산 된 값을 얻어 변화를 주어야 한다.
ObjectAnimator를 사용하기 위해서는 아래 사항을 준수해야 한다.
- Property Name에 대해서 setter method가 camel 표기 법을 따라서 존재해야 함. (foo 가 setter라면 setFoo() )
- Option 1 : setter 추가
- Option 2 : Wrapper class를 만들어서 setter 추가.
- Option 3 : 대신에 ValueAnimator를 사용.
- 이것은 onAnimationUpdate() callback을 이용하여 처리.
- 만약 View의 RGB값 변경 같은 경우는 onAnimaionUpdate에서 invalidation을 따로 해줄 필요가 없어짐. 왜냐하면 이미 그런 Drawable에는 setAlpha, getAlpha method가 이미 준비되어 있고, property는 alpha와 같이 이미 해당 이름으로 명시했을 것이기 때문이다.
Favorite site
References
-
Xicnt.blog.me_-_Android_Property_Animation.pdf ↩