【问题标题】:ImageSwitcher not animating after first 2 image switchesImageSwitcher 在前 2 个图像切换后没有动画
【发布时间】:2012-06-30 17:34:52
【问题描述】:

我正在使用 Android 鲜为人知的 ImageSwitcher 来为 ImageView 中的图像更改设置动画。目前,有 2 个图像(默认和“ok”图像)在某些事件(所有事件都发生在主线程上)上打开 - 第一个图像淡出,而第二个图像淡入。这就是 ImageSwitcher 应该如何工作.

前 2 个动画(从默认到“ok”然后回到默认)淡出就好了。然后,突然之间,图像不再从一个褪色到另一个,但视图立即切换到第二个图像,并且动画与第二个图像的两个叠加版本混淆(这些图像是半透明的黑色,我可以看到第二张图像如何从几乎全黑变回正常)。

切换是这样实现的:

// Initialization in onCreate():
mRefreshImageSwitcher = (ImageSwitcher) findViewById(R.id.main_refresh);
mRefreshImageSwitcher.setFactory(new RefreshButtonViewFactory());
mRefreshImageSwitcher.setImageResource(R.drawable.refresh);

// ...

    mRefreshImageSwitcher.setInAnimation(getFadeAnimation(true, 300));
    mRefreshImageSwitcher.setOutAnimation(getFadeAnimation(false, 300)); 
    mRefreshImageSwitcher.setImageResource(R.drawable.refresh_ok);

// ...

private Animation getFadeAnimation(boolean in, long durationMillis) {
    Animation a = AnimationUtils.loadAnimation(this, in ? android.R.anim.fade_in : android.R.anim.fade_out);
    a.setDuration(durationMillis);
    return a;
}

或者,我尝试始终使用相同的动画对象,但这并没有改变任何东西。

是否有可能多次设置相同的图像以某种方式弄乱 ImageSwitcher?还有其他想法吗?

【问题讨论】:

    标签: android animation imageswitcher


    【解决方案1】:

    您不是在写如何更改事件中的图像,但一件事是确保您使用的是 mRefreshImageSwitcher.setImageResource() 而不是 mRefreshImageSwitcher.setBackgroundResource() 因为设置背景不会播放动画

    【讨论】:

    • 我不明白,你能重新表述一下答案吗?
    • 这是我在创建时的代码 ` iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher); iSwitcher.setFactory(this); iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));为工厂制作视图 @Override public View makeView() { ImageView iView = new ImageView(this); iView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));返回 iView; }`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多