Android - 使用 Alpha 淡入淡出动画闪烁图像

发布于 2024-09-27 03:15:02 字数 1738 浏览 3 评论 0原文

纠结了好几天,最后还是决定来问问。它是如此简单,我必须错过一些非常基本的东西。

我有一个定义了图像的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绅刃 2024-10-04 03:15:02

补间淡入淡出的最佳方法:

ImageView myImageView = (ImageView) findViewById(R.id.imageView2); 
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Splash.this, R.anim.tween);
myImageView.startAnimation(myFadeInAnimation);

在你的 res/anim/ 创建 tween.xml 补间 1s 开始不透明度 0 到 1 并反向无限...

<?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:duration="1000" 
    android:repeatMode="reverse"
    android:repeatCount="infinite" />
</set>

The best way to tween fade :

ImageView myImageView = (ImageView) findViewById(R.id.imageView2); 
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Splash.this, R.anim.tween);
myImageView.startAnimation(myFadeInAnimation);

in your res/anim/ create tween.xml tween 1s start opacity 0 to 1 and reverse infinit...

<?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:duration="1000" 
    android:repeatMode="reverse"
    android:repeatCount="infinite" />
</set>
前事休说 2024-10-04 03:15:02

为什么不使用 android:repeatMode="reverse"

Why not use android:repeatMode="reverse"

萝莉病 2024-10-04 03:15:02

您可以使用下面的 alpha 动画为 android 中的视图设置闪烁效果。

blinkanimation= new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
blinkanimation.setDuration(300); // duration
blinkanimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
blinkanimation.setRepeatCount(3); // Repeat animation infinitely
blinkanimation.setRepeatMode(Animation.REVERSE);

之后将动画添加到您的视图中,例如,

view.setAnimation(blinkanimation); 

或者

view.startAnimation(blinkanimation);

You can use the below alpha animation to set the blinking effects to the views in android.

blinkanimation= new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
blinkanimation.setDuration(300); // duration
blinkanimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
blinkanimation.setRepeatCount(3); // Repeat animation infinitely
blinkanimation.setRepeatMode(Animation.REVERSE);

After this add the animation to your view like,

view.setAnimation(blinkanimation); 

or

view.startAnimation(blinkanimation);
未央 2024-10-04 03:15:02

如果有人决定使用编程版本:

val anim = AlphaAnimation(1.0f, 0.0f)
anim.duration = 750
anim.fillAfter = true  

// here is repeat settings
anim.repeatMode = AlphaAnimation.REVERSE // ping pong mode
anim.repeatCount = 1 // count of repeats

yourView.startAnimation(anim)

If someone will decide to use programmatic version:

val anim = AlphaAnimation(1.0f, 0.0f)
anim.duration = 750
anim.fillAfter = true  

// here is repeat settings
anim.repeatMode = AlphaAnimation.REVERSE // ping pong mode
anim.repeatCount = 1 // count of repeats

yourView.startAnimation(anim)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文