动画ImageView从alpha 0到1

时间:2016-05-17 16:02:41

标签: android android-layout imageview alpha-transparency

我有一个想要以隐身方式启动的imageView。单击某个按钮后,我想将图像设置为视图,然后我希望它保持在alpha 1.我该怎么做?到目前为止没有运气。如果我在xml中将alpha设置为0,那么我永远不会看到图像。如果我没有在xml中设置alpha,那么图像始终可见,当单击该按钮时,它会从0到1 alpha动画。

这是我的动画代码。

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

3 个答案:

答案 0 :(得分:2)

尝试在xml:

中制作ImageView invisible
<ImageView
    ...
    android:visibility="invisible"/>

然后,通过添加AnimationListener,在visible中将其设为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);

答案 1 :(得分:2)

在MainActivity.java中添加

public void blink(View view) {
        ImageView image = (ImageView) findViewById(R.id.imageView);
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);
        image.startAnimation(animation1);
    }

在res&gt; anim文件夹中创建名为blink.xml的文件并添加此代码

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="--YOUR DURATION--"
        android:repeatMode="reverse"
        android:repeatCount="0"/>
</set>

确保按钮上的onClick功能名为blink

答案 2 :(得分:1)

您可以简单地将初始alpha设置为0,然后以所需的持续时间通过1设置动画;

imageView.setAlpha(0);

imageView.animate()
    .alpha(1)
    .setDuration(200);
相关问题