如何制作闪烁的图像动画?

时间:2017-07-27 08:01:52

标签: android

我默认ImageView alpha=0。因此它可以作为另一个图像上的隐形叠加。

点击后,我想创建一个动画,显示覆盖图像200ms,然后再次隐藏它。

以下确实有效,但只有一次!为什么呢?

final ImageView flash = (ImageView) view.findViewById(R.id.flash);

flash.animate()
        .alpha(255) //make visible
        .setDuration(200)
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                flash.setImageAlpha(0); //hide again
            }
        });

1 个答案:

答案 0 :(得分:3)

您正在设置2个不同的Alpha值,第一个是View的alpha,但是当您结束动画时,您将ImageView类alpha设置为0,因此如果再次启动动画,则视图alpha是1.0f,但图像alpha将为0,你什么也看不见。 将其更改为

flash.animate()
        .alpha(1.f)
        .setDuration(200)
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                flash.setAlpha(0.f);
            }
        });

View(因此ImageView)有一个方法setAlpha(float)ImageView还添加了另一种方法setAlpha(int),因为它已被弃用令人困惑,现在它被重命名为setImageAlpha(int)。动画将调用View' s setAlpha(float)