Animate ImageView from alpha 0 to 1

笑着哭i 提交于 2019-12-02 07:38:32

Try to make your ImageView invisible in xml:

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

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

In MainActivity.java add

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

Create file named blink.xml in res>anim folder and add this code

<?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>

And make sure that onClick function on button is named blink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!