java 两个图像视图之间的动画过渡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26060182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:13:59  来源:igfitidea点击:

Animated transition between two imageview

javaandroidanimationandroid-animationandroid-imageview

提问by An-droid

On one hand I have a layout with two ImageView :

一方面,我有一个带有两个 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>

On the other hand i have a list of image ressources :

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

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               };

i have also a Scheduler made from Handler + postDelayed() to display the images one after another with a timer. This is working fine

我还有一个由 Handler + postDelayed() 制成的调度程序,可以用计时器一个接一个地显示图像。这工作正常

my issue is about the transition animation from one imageview to the other, knowing that i have to clean the imageviews each time to avoid OutOfMemoryExceptions :

我的问题是关于从一个图像视图到另一个图像视图的过渡动画,知道我每次都必须清理图像视图以避免 OutOfMemoryExceptions :

For now i do that in the schduled callback method :

现在我在调度回调方法中这样做:

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);
                }

with this animation :

有了这个动画:

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);
    }

The animation is not great, the fade is not really smooth, what can i do to make it more smooth while having to clean the ImageView each times ?

动画不是很好,淡入淡出不是很平滑,我该怎么做才能使它更平滑,同时每次都必须清洁 ImageView?

回答by Budius

My first suggestion is to simplify most of your code. Android has this nice little class called TransitionDrawable

我的第一个建议是简化大部分代码。Android 有一个不错的小类叫做TransitionDrawable

so you can have just 1 image view and use the TransitionDrawable:

所以你可以只有 1 个图像视图并使用 TransitionDrawable:

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

and call the animation on tdwith

并呼吁动画td

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

and keep using the postDelayedto trigger them

并继续使用postDelayed来触发它们