Applying ObjectAnimator with a Complete Example
The other issue is that you need a new ObjectAnimator for every view you want to modify since it doesn’t support simultaneous changes of several objects. However, starting from API 23, it is possible to use PropertyValuesHolder and Keyframe in resource files to create more complex animations. See here for more details.
Anyway, it worth to say that ObjectAnimator is widely used in AnimatedVectorDrawable to animate SVG due to its ability to animate a property of any type. In my opinion, in any other case, there are better solutions for animations.
Usage:
ObjectAnimator animatorX =
ObjectAnimator.ofFloat(targetView, "property", property values);
ValueAnimator.AnimatorUpdateListener mListener;
animatorX.addUpdateListener(mListener);
AnimatorUpdateListener anim2Listener = new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
edtTxt.invalidate();
}
};
animatorX.start();
One other issue is that we can add a listener to this animation. animatorX.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
Toast.makeText(activity, "End!", Toast.LENGTH_LONG).show();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
Example 1: An EditText is subject to some animations. Please create a new Android Studio project and use Empty template. I create a new Java class Animator1. See below code.
Now in activity_main.xml file we add an EditText and a Button.
In MainActivity.java we should create an instance of Animator1 and an instance of AnimatorUpdateListener. This file can be completed like below.
Now it is ready to run the app and see how this animation works.
Example 2: Here we want to produce an animation like Example 2 of Animation class which is a circle animating from angle 0 to 360 degree.
Step 1- Create a new project in android studio and select Empty template. Step 2- Follow steps in Custom View in Android Apps to produce a custom view. The PieView class itself can be as follows.
Step 2- Modify the activity_main.xml file as below.
Step 3- Go to MainActivity.java and modify it like shown here.
Now its time to run the app. You probably see one animation like below!
ObjectAnimator is good for animating one or two properties simultaneously. But what if we should animate more than two properties at the same time? Please refer to ViewPropertyAnimator for a good answer!
Comments
Post a Comment