如何在安卓平台上制作流畅的帧动画?

5

我制作了一帧动画,但是图片之间的转换效果看起来很糟糕。如何应用交叉淡入淡出效果?

当使用TransitionDrawable时,我得到了正确的结果,但它在执行一次后就停止了。

有人有解决方法吗?

public void startAnimation() {
    if (logoAnimation != null) {
        if (logoAnimation.isRunning()) {
            logoAnimation.stop();
        }
        logoAnimation.start();
    }
}

private int setLogoAnimation(int animationID, int targetID) {
    imageView = (ImageView) window.findViewById(targetID);
    imageView.setImageResource(animationID);
    logoAnimation = (AnimationDrawable) imageView.getDrawable();

    if (imageView != null && logoAnimation != null) {
        return 1;
    } else {
        return 0;
    }
}

然后我只需通过object.startAnimation()运行它,它可以工作,但动画效果很丑,我需要它更加流畅。

2个回答

7
如果你想在两张图片之间进行淡入淡出的过渡效果,为什么不使用AlphaAnimation呢?它可以改变两个视图的透明度,从而创建所需的效果。
创建两个动画:
res/anim/fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="1.0" android:toAlpha="0.0"
    android:duration="@android:integer/config_mediumAnimTime" />

res/anim/fadein.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0.0" android:toAlpha="1.0"
    android:duration="@android:integer/config_mediumAnimTime" />

然后覆盖默认的活动之间的转换:

startActivity( new Intent( this, SecondActivity.class ) );
overridePendingTransition( R.anim.fadeout, R.anim.fadein );

或者您可以将动画应用于特定的小部件:

Animation animation = AnimationUtils.loadAnimation( this, R.anim.fadeout );
image1.startAnimation( animation );

我目前正在我的博客上写有关动画的系列文章,这可能会为您提供进一步的信息。


不是这样的。我有一个带有一些按钮的主活动。我制作了一个自定义标题栏,动画应该在其中工作。这个标题栏中有一个ImageView,它应该在整个过程中进行动画处理。我使用帧动画成功地对其进行了动画处理。现在我想添加淡入淡出效果。我该如何实现? - Olek
再看一遍我的答案,我解释了如何将动画应用于特定的小部件以及活动之间的转换。 - Mark Allison
我不确定我是否确切知道你的意思。我不想改变活动之间的转换,因为它们很好。我想改变ImageView动画中的转换效果。当设置了帧动画时,使用“imageView.startAnimation(alphaAnimation)”无效。 - Olek
正如我已经说了三次:您可以通过在特定的小部件(即两个ImageView实例)上调用startAnimation来应用动画。 - Mark Allison
好的,谢谢,我现在全都明白了。我遇到了另一个问题,我正在为Android 1.6编写应用程序,但它不支持overridePendingTransition。有什么解决办法吗? - Olek
我还卡在死点上。动画没有按照预期工作。有人可以帮忙吗? - Olek

0

通过使用帧动画,增加帧数可以帮助您获得良好的结果。在我的情况下,在我的HTC Evo上的结果令人满意。

在使用补间动画时,您可以使用线性插值器。例如旋转

<?xml version="1.0" encoding="UTF-8"?>  
<rotate  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:fromDegrees="0"  
    android:toDegrees="359"  
    android:pivotX="50%"  
    android:pivotY="50%"  
    android:repeatCount="infinite"  
    android:duration="1000"  
    android:interpolator="@anim/linear_interpolator" />  

在这里插入您的插值器文件内容

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

这是用于旋转的,您可以在此处查看完整列表。

http://developer.android.com/guide/topics/resources/animation-resource.html

或者您可以阅读我写的一篇博客文章,了解如何制作加载动画。

http://yekmer.posterous.com/how-to-make-a-loading-animator-in-android


问题是我只有两张图片,而我想要像TransitionDrawable一样的效果。因此,元素会交叉淡入淡出。 - Olek
你可以使用AnimationDrawable类的setOneShot(boolean oneShot)方法来获取一个不间断的动画。 - Yekmer Simsek
谢谢,但这不是问题。我有一个ImageView,在那里我想要动画2张图片。这两张图片应该交叉淡入淡出。 - Olek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接