[英]Animate ImageView from alpha 0 to 1
我有一个 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);
尝试使您的 ImageView 在 xml 中invisible
:
<ImageView
...
android:visibility="invisible"/>
然后,通过添加AnimationListener
,使其在onAnimationStart
visible
:
...
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);
在 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>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 函数被命名为闪烁
您可以简单地将初始 alpha 设置为 0,然后以所需的持续时间将其设置为 1;
imageView.setAlpha(0);
imageView.animate()
.alpha(1)
.setDuration(200);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.