两个ImageView之间的动态过渡

17

一方面,我有一个包含两个ImageView的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/image_cross2"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/image_size"
        android:layout_gravity="top"
        android:scaleType="centerCrop" />

    <ImageView
        android:id="@+id/image_cross1"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/image_size"
        android:layout_gravity="top"
        android:scaleType="centerCrop" />
</FrameLayout>

另一方面,我有一份图像资源列表:

static int                      mImages[]               = new int[] {
        R.drawable.artistes,
        R.drawable.couple1,
        R.drawable.couple2,
        R.drawable.couple3,
        R.drawable.enfant,
        R.drawable.manege,
        R.drawable.manege2,
        R.drawable.metropolitain,
        R.drawable.panoramique,
        R.drawable.sacrecoeur               };

我还有一个由Handler + postDelayed()制作的调度程序,用于使用计时器依次显示图像。 这很好用。

我的问题是有关从一个ImageView过渡动画到另一个ImageView,需要注意的是每次都必须清除ImageView以避免OutOfMemoryExceptions :

现在我在调度回调方法中执行此操作:

if (mIndex == mImages.length) {
                    mIndex = 0; // repeat
                }
                if (mIndex % 2 != 0) { // pair
                    mImageCross2.setImageResource(mImages[mIndex++]);
                    Utils.crossfade(mImageCross2, mImageCross1, 1000/*duration*/);
                    mImageCross1.setImageResource(0);
                } else {
                    mImageCross1.setImageResource(mImages[mIndex++]);
                    Utils.crossfade(mImageCross1, mImageCross2, 1000);
                    mImageCross2.setImageResource(0);
                }

使用此动画:

public static void crossfade(final ImageView viewIn, final ImageView viewOut, int duration) {
        Animation fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setDuration(duration);
        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setDuration(duration);
        fadeOut.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewOut.setVisibility(View.GONE);
            }
        });
        fadeIn.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewIn.setVisibility(View.VISIBLE);
            }
        });
        viewOut.startAnimation(fadeOut);
        viewIn.startAnimation(fadeIn);
    }

动画效果不太好,淡入淡出效果也不是很流畅,有什么办法可以让它更加流畅而又不必每次清除 ImageView?

1个回答

43

我的第一个建议是简化您的大部分代码。Android有一个很好的小类叫做TransitionDrawable,所以您可以只使用1个图像视图并使用TransitionDrawable:

TransitionDrawable td = new TransitionDrawable( new Drawable[] {
    getResources().getDrawables(mImages[x]),
    getResources().getDrawables(mImages[y])
});
imageView.setImageDrawable(td);

并调用在td上的动画

td.startTransition(1000);
 // and
td.reverseTransition(1000);

并继续使用postDelayed来触发它们


哦,顺便说一下...我是凭记忆打的,可能会有几个错别字,一旦你在IDE上输入就会看到。 - Budius
我在使用"getResources().getDrawables(mImages[0])"时遇到了空指针异常,即使直接使用资源号"getResources().getDrawables(R.drawable.artistes)"也是如此。你有什么想法吗? - An-droid
在那一行上设置一个断点,然后在您的IDE的调试窗口中检查getResources()是否返回null?然后检查getResources().getDrawable(R.drawable.artiste)是否返回null? - Budius
好的,问题解决了。我猜是因为我太快使用了“getRessource()”方法。现在我遇到了众所周知的OutOfMemoryException异常 ^^"。 - An-droid
这对我使用它来说有点过头了。我回到了我的方法中,在onAnimationEnd中重置了ImageViews。但是你的解决方案也很好 =) - An-droid
显示剩余2条评论

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