2014-09-11 87 views
0

我在Android中遇到动画问题。在这个动画的结尾和开始之间,在屏幕上闪烁整个图像,没有alpha效果。动画开头字母0,用α-0结束得我有这样的代码:Android动画重复

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:fillAfter="true" > 

<alpha 
    android:duration="4000" 
    android:fromAlpha="0.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="1.0" /> 

<scale 
    android:duration="12000" 
    android:fromXScale="0.5" 
    android:fromYScale="0.5" 
    android:pivotX="50%" 
    android:pivotY="10%" 
    android:toXScale="3" 
    android:toYScale="2" 
    android:interpolator="@android:anim/linear_interpolator"/> 

<alpha 
    android:startOffset="10000" 
    android:duration="1500" 
    android:fromAlpha="1.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="0" /> 
</set> 

这个代码在java中

imageView.startAnimation(animace); 

animace.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 
     imageView.setVisibility(View.VISIBLE); 
    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     imageView.setVisibility(View.INVISIBLE); 
     imageView.startAnimation(animace); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
    } 
}); 
+0

你的标题是说一些关于**的run()**,但它不是在代码中提到片断你提供,请张贴完整代码 – nobalG 2014-09-11 09:34:55

+0

你是否在调用'imageView.setVisibility(View.VISIBLE)'''之前尝试调用'imageView.setAlpha(0f);'onAnimationStart''? – Stanislav 2014-09-11 09:35:41

+0

不,这不起作用,当设置alpha到imageview,然后imageview保持透明和动画显示什么都没有。 – 2014-09-11 09:51:14

回答

1

这是因为XML文件中的动画在同一拼命地跑时间。你同时拥有一个将Alpha从0改变为1的动画,以及一个从1改变为0的动画。我假设你想要在一个序列中运行它们,所以你应该把它们分成不同的文件。一个解决办法是,像这样:(未测试)

fade_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillAfter="true" > 

<alpha 
    android:duration="4000" 
    android:fromAlpha="0.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="1.0" /> 

</set> 

fade_out.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillAfter="true" > 

<alpha 
    android:startOffset="10000" 
    android:duration="1500" 
    android:fromAlpha="1.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="0" /> 

</set> 

scale.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillAfter="true" > 

<scale 
    android:duration="12000" 
    android:fromXScale="0.5" 
    android:fromYScale="0.5" 
    android:pivotX="50%" 
    android:pivotY="10%" 
    android:toXScale="3" 
    android:toYScale="2" 
    android:interpolator="@android:anim/linear_interpolator"/> 

</set> 

,然后在你的代码,同时启动fade_in和缩放,并在fade_in完成后运行fade_out:

imageView.startAnimation(fadeInAnimation); 
imageView.startAnimation(scaleAnimation); 

fadeInAmination.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     imageView.startAnimation(fadeOutAnimation); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { } 
}); 

我不确定您需要在哪里设置可见和不可见,但您可以为每个对象附加一个AnimationListener,并根据您的需要放置它们。

希望这有助于:)

+0

我有第二个阿尔法开始设置10000,这个动画是好的。现在,我通过@Stanislav找到了解决方案。我不知道为什么,但设置INVISIBLE或VISIBLE的可见性不工作(Android Studio不显示任何错误),但是当我改变这个设置在animationEnd和1f在动画开始时,现在是它的工作。 – 2014-09-11 10:16:33

+0

的确,我错过了那一个。对不起:) – tudor 2014-09-11 10:59:42

0

这项工作:

animace.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 
      imageView.setAlpha(1f); 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      imageView.setAlpha(0f); 
      imageView.startAnimation(animace); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 
    });