动画 ImageView 从 alpha 0 到 1
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);
尝试在 xml 中制作您的 ImageView 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 函数被命名为 blink
您可以简单地将初始 alpha 设置为 0,然后以所需的持续时间通过 1 对其进行动画处理;
imageView.setAlpha(0);
imageView.animate()
.alpha(1)
.setDuration(200);
我有一个 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);
尝试在 xml 中制作您的 ImageView 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 函数被命名为 blink
您可以简单地将初始 alpha 设置为 0,然后以所需的持续时间通过 1 对其进行动画处理;
imageView.setAlpha(0);
imageView.animate()
.alpha(1)
.setDuration(200);