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

时间:2010-10-11 13:37:49

标签: 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);
     }
}   

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> 

另一个:

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

5 个答案:

答案 0 :(得分:96)

补间淡化的最佳方式:

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

在你的res / anim / create 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>

答案 1 :(得分:30)

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

答案 2 :(得分:15)

您可以使用以下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);

答案 3 :(得分:2)

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

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)

答案 4 :(得分:0)

如果您想在ImageView上进行淡入淡出动画,但不想自己动画或不想让背景闪烁,则自定义视图是唯一的方法 - 请参阅我的答案:{{3 }}

相关问题