Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 两个imageview之间的动画过渡_Java_Android_Animation_Android Animation_Android Imageview - Fatal编程技术网

Java 两个imageview之间的动画过渡

Java 两个imageview之间的动画过渡,java,android,animation,android-animation,android-imageview,Java,Android,Animation,Android Animation,Android 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=

一方面,我有一个带有两个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以避免OutOfMemoryException:

<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               };
现在,我在计划回调方法中执行此操作:

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时,我可以做些什么使其更平滑?

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

因此,您可以仅使用一个图像视图并使用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上键入,你就会看到我在“getResources().getDrawables(mImages[0])处得到一个空指针异常,即使直接使用资源编号getResources().getDrawables(R.drawable.artistes),你知道吗?在那一行上放一个断点,在IDE的调试窗口中检查
getResources()
它是否返回null?然后检查
getResources().getDrawable(R.drawable.artiste)
它是否返回null?好的,我想这是因为我使用“getRessource()”太快了。现在我得到了众所周知的OutOfMemoryException^^“OOM是一个不同的怪物。你可以尝试看看
Picasso
库是否能帮助你。或者在某个时候,你只需要使用较小的图像,或者将其更改为
VideoView