从 alpha 0 到 1 的动画 ImageView

Animate ImageView from alpha 0 to 1(从 alpha 0 到 1 的动画 ImageView)

本文介绍了从 alpha 0 到 1 的动画 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 imageView,我想一开始是不可见的.单击某个按钮后,我想将图像动画化到视图中,然后我希望它保持在 alpha 1.我该怎么做?到目前为止还没有运气.如果我在 xml 中将 alpha 设置为 0,那么我永远看不到图像.如果我没有在 xml 中设置 alpha,那么图像总是可见的,并且当单击按钮时,它会从 0 到 1 alpha 进行动画处理.

I have an imageView that I want to start as invisible. After a certain button is clicked, I want to animate the Image into view and then I want it to remain at alpha 1. How do I do that? So far no luck. If I set the alpha to 0 in xml, then I never see the image. If I don't set alpha in xml then the image is always visible, and when the button is clicked it animates from 0 to 1 alpha.

这是我的动画代码.

AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);
    animation1.setFillAfter(true);
    tokenBtn.startAnimation(animation1);

推荐答案

尝试让你的ImageView invisible in xml:

Try to make your ImageView invisible in xml:

<ImageView
    ...
    android:visibility="invisible"/>

然后,通过添加一个AnimationListener,使其在onAnimationStartvisible:

Then, by adding an AnimationListener, make it visible in onAnimationStart:

...
animation1.setFillAfter(true);
animation1.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        // pass it visible before starting the animation
        tokenBtn.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {    }
    @Override
    public void onAnimationEnd(Animation animation) {    }
});
// finally, start the animation
tokenBtn.startAnimation(animation1);

这篇关于从 alpha 0 到 1 的动画 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 alpha 0 到 1 的动画 ImageView

基础教程推荐