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

Kei*_*ith 42 android fadein

我已经在这几天苦苦挣扎,最后才决定问.这很简单,我必须遗漏一些非常基本的东西.

我有一个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);
     }
}   
Run Code Online (Sandbox Code Playgroud)

Anim资源中的两个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> 
Run Code Online (Sandbox Code Playgroud)

和另外一个:

 <?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>
Run Code Online (Sandbox Code Playgroud)

小智 97

补间淡化的最佳方法:

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

在你的res/anim/create tween.xml补间1s启动opacity 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>
Run Code Online (Sandbox Code Playgroud)

  • 它对我有用!! 我将fromAlpha保持为0.2,这样视图就不会完全消失.谢谢Buchs,+1为此. (3认同)
  • @ brainmurphy1也许你忘记了`android:repeatCount ="无限"`.重复计数需要是正数或无限数. (2认同)

met*_*din 31

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


Kar*_*KPN 16

您可以使用以下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);
Run Code Online (Sandbox Code Playgroud)

在此之后将动画添加到您的视图中,

view.setAnimation(blinkanimation); 
Run Code Online (Sandbox Code Playgroud)

要么

view.startAnimation(blinkanimation);
Run Code Online (Sandbox Code Playgroud)


Zak*_*kir 5

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

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)
Run Code Online (Sandbox Code Playgroud)