在更改图像资源时在ImageView上创建动画

时间:2011-08-23 13:08:29

标签: android

我的布局中只有一个ImageView,当检测到gasture事件时,我正在更改其资源。我只想在更改ImageView的资源时显示动画。我可以将ViewFlipper用于one ImageView吗?

5 个答案:

答案 0 :(得分:59)

对于单个imageview,您可以使用此辅助函数:

public static void ImageViewAnimatedChange(Context c, final ImageView v, final Bitmap new_image) {
    final Animation anim_out = AnimationUtils.loadAnimation(c, android.R.anim.fade_out); 
    final Animation anim_in  = AnimationUtils.loadAnimation(c, android.R.anim.fade_in); 
    anim_out.setAnimationListener(new AnimationListener()
    {
        @Override public void onAnimationStart(Animation animation) {}
        @Override public void onAnimationRepeat(Animation animation) {}
        @Override public void onAnimationEnd(Animation animation)
        {
            v.setImageBitmap(new_image); 
            anim_in.setAnimationListener(new AnimationListener() {
                @Override public void onAnimationStart(Animation animation) {}
                @Override public void onAnimationRepeat(Animation animation) {}
                @Override public void onAnimationEnd(Animation animation) {}
            });
            v.startAnimation(anim_in);
        }
    });
    v.startAnimation(anim_out);
}

答案 1 :(得分:14)

以下是使用ImageSwitcher的示例,例如@Konstantin Burov建议:

将此ImageSwitcher元素添加到xml布局:

<ImageSwitcher
   android:id="@+id/slide_trans_imageswitcher"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
</ImageSwitcher>

res/anim/创建进出动画:

动画:

//left_to_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <translate android:fromXDelta="0%" android:toXDelta="100%"
               android:fromYDelta="0%" android:toYDelta="0%"
               android:duration="700"/>
</set>

在动画中:

//left_to_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <translate android:fromXDelta="-100%" android:toXDelta="0%"
               android:fromYDelta="0%" android:toYDelta="0%"
               android:duration="700"/>
</set>

在代码中初始化它们:

        Animation in  = AnimationUtils.loadAnimation(this, R.anim.left_to_right_in);
        Animation out = AnimationUtils.loadAnimation(this, R.anim.left_to_right_out);

初始化您的ImageSwitcher

private ImageSwitcher imageSwitcher;
imageSwitcher = (ImageSwitcher)findViewById(R.id.slide_trans_imageswitcher);

将动画附加到它:

imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);

现在,每次为imageSwitcher设置图像资源时,都会切换幻灯片动画。

例如,我们可以每隔X秒更改一次图像:

private int animationCounter = 1;
private Handler imageSwitcherHandler;
imageSwitcherHandler = new Handler(Looper.getMainLooper());
imageSwitcherHandler.post(new Runnable() {
    @Override
    public void run() {
        switch (animationCounter++) {
            case 1:
                imageSwitcher.setImageResource(R.drawable.image1);
                break;
            case 2:
                imageSwitcher.setImageResource(R.drawable.image2);
                break;
            case 3:
                imageSwitcher.setImageResource(R.drawable.image3);
                break;
        }
        animationCounter %= 4;
        if(animationCounter == 0 ) animationCounter = 1;

        imageSwitcherHandler.postDelayed(this, 3000);
    }
});

答案 2 :(得分:13)

我认为ImageSwitcher是最适合该任务的组件。

答案 3 :(得分:3)

我找到了另一种使用转场可绘制动画的方法(我从here那里得到了想法)

    public static void setImageDrawableWithAnimation(ImageView imageView,
                                                     Drawable drawable,
                                                     int duration) {
    Drawable currentDrawable = imageView.getDrawable();
    if (currentDrawable == null) {
        imageView.setImageDrawable(drawable);
        return;
    }

    TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[] {
            currentDrawable,
            drawable
    });
    imageView.setImageDrawable(transitionDrawable);
    transitionDrawable.startTransition(duration);
}

类似kotlin扩展功能:

fun ImageView.setImageDrawableWithAnimation(drawable: Drawable, duration: Int = 300) {
    val currentDrawable = getDrawable()
    if (currentDrawable == null) {
        setImageDrawable(drawable)
        return
    }

    val transitionDrawable = TransitionDrawable(arrayOf(
            currentDrawable,
            drawable
    ))
    setImageDrawable(transitionDrawable)
    transitionDrawable.startTransition(duration)
}

如果可绘制对象包含透明零件,则此标志对于防止旧的可绘制可见性很有用:

transitionDrawable.setCrossFadeEnabled(true);

答案 4 :(得分:0)

考虑使用API​​演示应用中提供的Rotate3dAnimation。 查看&gt;动画&gt; 3D过渡

你也可以在{<3}}上提问