问题
I have a pretty complex animation I need to code and I'm using a bunch of ObjectAnimators like the following:
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(view, TRANSLATION_X, value).setDuration(BASE_DURATION * 2);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view, TRANSLATION_Y, value).setDuration(BASE_DURATION * 2);
Is it possible to group the X and Y translations into the same ObjectAnimator rather than creating a bunch of them then adding them all into an AnimatorSet?
Thanks!
回答1:
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(TRANSLATION_X, value);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(TRANSLATION_Y, value);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY);
animator.setDuration(BASE_DURATION * 2);
animator.start();
http://developer.android.com/guide/topics/graphics/prop-animation.html#views One ObjectAnimator
回答2:
I think you should be using an AnimationSet. It practically does what you want and is the nice way of doing it because when I think of PropertyValuesHolder I don't think of Keyframes.
So yeah:
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(view, TRANSLATION_X, value);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(view, TRANSLATION_Y, value);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator1, objectAnimator2);
animatorSet.setDuration(BASE_DURATION * 2);
animatorSet.start();
You can add as many ObjectAnimators as you want.
回答3:
An alternative approach I've used to add multiple properties to an object's animation is to use a mix of code and XML to define the animation. This is based on this documentation
For example, in XML I can setup AnimatorSets and ObjectAnimators for a single object with static values, and define a sequential sequence of changes (res/animator/moveout.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<set>
<objectAnimator
android:propertyName="scaleX"
android:duration="2000"
android:valueTo="0.8"
android:interpolator="@android:anim/decelerate_interpolator"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="2000"
android:valueTo="0.8"
android:interpolator="@android:anim/decelerate_interpolator"/>
<objectAnimator
android:propertyName="alpha"
android:duration="2000"
android:valueTo="0"
android:interpolator="@android:anim/decelerate_interpolator"/>
</set>
<set>
<objectAnimator
android:propertyName="scaleX"
android:duration="2000"
android:valueTo="1.2"
android:interpolator="@android:anim/accelerate_interpolator"/>
<objectAnimator
android:propertyName="scaleY"
android:duration="2000"
android:valueTo="1.2"
android:interpolator="@android:anim/accelerate_interpolator"/>
<objectAnimator
android:propertyName="alpha"
android:duration="2000"
android:valueTo="1"
android:interpolator="@android:anim/accelerate_interpolator"/>
</set>
</set>
Then I can load these AnimatorSets/ObjectAnimators at runtime and modify their values with dynamically generated values:
AnimatorSet firstSet = (AnimatorSet) AnimatorInflater.loadAnimator(this,
R.animator.moveout);
AnimatorSet secondSet = firstSet.clone();
firstSet.setTarget(button);
secondSet.setTarget(anotherButton);
// Choreograph the animations
// Change the duration of all child elements in the set
firstSet.setDuration(1000);
secondSet.setDuration(200);
// Set start delay so second set plays after the first set
secondSet.setStartDelay(2000);
AnimatorSet anim = new AnimatorSet();
anim.playTogether(firstSet,secondSet);
anim.start();
回答4:
If you are animating a view (like your example suggests), you could use a ViewPropertyAnimator:
view.animate().translationX(value_x).translationY(value_y).setDuration(duration).start();
回答5:
Animating view could be done even easier:
v.animate().x(valueX).y(valueY).setDuration(500).start();
(In this snippet ViewPropertyAnimator has been used).
来源:https://stackoverflow.com/questions/28352352/change-multiple-properties-with-single-objectanimator