Android ObjectAnimator 不淡入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11070067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:53:36  来源:igfitidea点击:

ObjectAnimator not fading in

androidandroid-animation

提问by user182192

I am trying to play a set of animations sequentially using the Animatorset. 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 set1animation 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?

我正在尝试使用该Animator集合按顺序播放一组动画。除了 alpha 动画 ( set1)之外,一切都在工作。它从 0.25f 变为 1,但在整个动画过程中都没有淡入淡出,在动画结束时,set1它从 0.25 变为 1 并且没有考虑到setDuration(因此我没有得到淡入淡出效果)。所以我没有淡入效果......当我自己制作这个动画时,淡入效果就在那里......有任何想法吗?

I am using the wonderful nineoldandroidslibrary to implement this.

我正在使用精彩的Nineoldandroids库来实现这一点。

@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();
}   

回答by Zen

While working on 4.0+

在 4.0+ 上工作时

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

回答by Mohsin Shafqat

try this.

尝试这个。

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

回答by Yaroslav Mytkalyk

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

您应该在布局完成后启动 object animator。

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
    }
});