Android – 使用Alpha淡出animation使图像闪烁

我一直在这方面挣扎了好几天,最后才决定问。 这很简单,我必须错过一些非常基本的东西。

我有一个定义了图像的XML布局页面。 我有两个animationXML页面,一个将alpha从0更改为1,另一个从1更改为0,以创build“闪烁”效果。 所以alphaAnimation在XML中定义,我只需要调用它。

图像popup,但没有循环闪烁效果。

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

Anim资源中的两个XMLanimation布局:

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

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

淡入淡出的最佳方式:

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

在你的res / anim /创buildtween.xml补间1s开始不透明0到1,并反向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> 

你可以使用下面的alphaanimation来设置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); 

在这之后,将animation添加到您的视图中,

 view.setAnimation(blinkanimation); 

要么

 view.startAnimation(blinkanimation);