【问题标题】:Android animations (zoom+fade out): weird bugAndroid 动画(缩放+淡出):奇怪的错误
【发布时间】:2021-02-08 10:46:51
【问题描述】:

我想制作一个重复的动画,在其中放大和淡出 ImageView(两个动画同时)。

这是我的代码:

    <com.xxxx.SquareImageView
        android:id="@+id/redImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/red_square"
        android:layout_centerInParent="true"
        android:visibility="invisible"
  
        />




float scaleMax = 1f;
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(0);
fadeOut.setDuration(1500);

Animation scale1 = new ScaleAnimation(0, scaleMax, 0, scaleMax, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scale1.setDuration(1500);
scale1.setStartOffset(0);


animSet.setFillEnabled(true);

animSet.addAnimation(scale1);
animSet.addAnimation(fadeOut);

animSet.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

        view.setVisibility(View.VISIBLE);

    }

    @Override
    public void onAnimationEnd(Animation animation) {


        view.setVisibility(View.INVISIBLE);
        view.startAnimation(animSet);


    }

    @Override
    public void onAnimationRepeat(Animation animation) {


    }
});


view.startAnimation(animSet);

图像按比例放大并正确淡出。然而,它会在短时间内(0.1 秒)以全比例出现,并且在两个动画之间没有透明度。我不明白为什么。

有什么想法吗?

谢谢。

【问题讨论】:

  • 那么,重新启动动画时,它确实显示不透明?或者它不会同时播放两个动画并在两个动画之间显示全尺寸的图像?
  • 可能是因为您一遍又一遍地启动 AnimationSets,在 onAnimationEnd() 再次启动之前尝试 animSet.cancel()
  • @AHoneyBustard 就是这样!我不敢相信我必须在 onAnimationEnd 中调用 cancel()... Android 的编码如此糟糕... 疯了。非常感谢!
  • 没问题,我不会说 Android 编码不好,尽管我同意 AnimationSets 有问题,我建议任何人改用 ViewPropertyAnimator。无论如何,对于未来的访问者,我会将其发布为答案。

标签: java android animation android-animation


【解决方案1】:

你一遍又一遍地启动 AnimationSet,所以为了流畅,你需要

animSet.cancel();

你开始的每个动画,在你在 onAnimationEnd() 中重新开始之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2018-08-22
    相关资源
    最近更新 更多