Android - 使用 Alpha 淡入淡出动画闪烁图像
纠结了好几天,最后还是决定来问问。它是如此简单,我必须错过一些非常基本的东西。
我有一个定义了图像的 XML 布局页面。我有两个动画 XML 页面,一个将 alpha 从 0 更改为 1,另一个从 1 更改为 0 以创建“闪烁”效果。所以 alphaAnimation 是在 XML 中定义的,我只需要调用它即可。
图像弹出,但没有循环闪烁效果。
public class blinker extends Activity {
//create name of animation
Animation myFadeInAnimation;
Animation myFadeOutAnimation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scanning_view);
//grab the imageview and load the animations
ImageView myImageView = (ImageView) findViewById(R.id.blinkingView01);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(null, R.anim.fade_in);
Animation myFadeOutAnimation = AnimationUtils.loadAnimation(null, R.anim.fade_out);
//fade it in, and fade it out.
myImageView.startAnimation(myFadeInAnimation);
myImageView.startAnimation(myFadeOutAnimation);
}
}
动画资源中的两个 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="50" android:repeatCount="infinite"/>
</set>
另一个:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="1000" android:repeatCount="infinite"/>
</set>
I've been struggling for a few days on this, finally just decided to ask. It's so simple I've got to be missing something very basic.
I have an XML layout page with an image defined. I have two anim XML pages, one to change alpha from 0 to 1, and the other from 1 to 0 in order to create a "blinking" effect. So the alphaAnimation is defined in XML, I just need to call it.
The image pops up, but there's no looping blinking effect.
public class blinker extends Activity {
//create name of animation
Animation myFadeInAnimation;
Animation myFadeOutAnimation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scanning_view);
//grab the imageview and load the animations
ImageView myImageView = (ImageView) findViewById(R.id.blinkingView01);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(null, R.anim.fade_in);
Animation myFadeOutAnimation = AnimationUtils.loadAnimation(null, R.anim.fade_out);
//fade it in, and fade it out.
myImageView.startAnimation(myFadeInAnimation);
myImageView.startAnimation(myFadeOutAnimation);
}
}
Two XML Animation layouts in Anim resource:
<?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="50" android:repeatCount="infinite"/>
</set>
And the other:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="1000" android:repeatCount="infinite"/>
</set>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
补间淡入淡出的最佳方法:
在你的 res/anim/ 创建 tween.xml 补间 1s 开始不透明度 0 到 1 并反向无限...
The best way to tween fade :
in your res/anim/ create tween.xml tween 1s start opacity 0 to 1 and reverse infinit...
为什么不使用 android:repeatMode="reverse"
Why not use
android:repeatMode="reverse"
您可以使用下面的 alpha 动画为 android 中的视图设置闪烁效果。
之后将动画添加到您的视图中,例如,
或者
You can use the below alpha animation to set the blinking effects to the views in android.
After this add the animation to your view like,
or
如果有人决定使用编程版本:
If someone will decide to use programmatic version: