Android - Blinking image using the Alpha fade animation

为君一笑 提交于 2019-11-27 19:59:19

问题


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>

回答1:


Why not use android:repeatMode="reverse"




回答2:


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>



回答3:


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



回答4:


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)


来源:https://stackoverflow.com/questions/3906738/android-blinking-image-using-the-alpha-fade-animation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!