ObjectAnimator not fading in

2019-03-22 16:56发布

问题:

I am trying to play a set of animations sequentially using the Animator set. Everything is working except for the alpha animation(set1). It is changing from 0.25f to 1 but it is not fading throughout the animation and at the end of the set1 animation it is changing from 0.25 to 1 and not taking in consideration the setDuration(as a result I am not getting the fade in effect). So I don't have the fade in effect... When I do this animation by itself the fade in effect is there....Any ideas?

I am using the wonderful nineoldandroids library to implement this.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ImageView image = (ImageView) findViewById(R.id.image);
    final AnimatorSet set = new AnimatorSet();
    set.play(ObjectAnimator.ofFloat(image, "translationX", 0, 100).setDuration(3000));

    final AnimatorSet set1 = new AnimatorSet();
    //THIS IS THE PROBLEMATIC ANIMATION!!
    set1.play(ObjectAnimator.ofFloat(image, "alpha", 0.25f, 1).setDuration(3000));

    final AnimatorSet set2 = new AnimatorSet();
    set2.play(ObjectAnimator.ofFloat(image, "translationX", 100, 200).setDuration(3000));

    final AnimatorSet set3 = new AnimatorSet();
    set3.playSequentially(set,set1,set2);
    set3.start();
}   

回答1:

While working on 4.0+

ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(image, View.ALPHA, 0,1);


回答2:

try this.

ObjectAnimator.ofFloat(image, "alpha", 0.25f, 1, 1)


回答3:

You should start object animator after the layout has been finished.

final View image = findViewById(R.id.image);
final ViewTreeObserver observer = image.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        observer.removeOnGlobalLayoutListener(this);
        // start animators
    }
});