首页 > 解决方案 > 带有 Glide 的 ImageSwitcher

问题描述

当屏幕被淡化效果触摸时,我正在使用ImageSwitcher淡入淡出改变背景。

工作守则

这是我在使用Glide之前更改它们的方式:

主要活动

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    val `in` = AnimationUtils.loadAnimation(this, R.anim.fadein)
    val out = AnimationUtils.loadAnimation(this, R.anim.fadeout)    
    view_background.setFactory {
       val imageView = ImageView(this@MainActivity)
       imageView.scaleType = ImageView.ScaleType.CENTER_CROP
       imageView
   }
   ...
}

改变背景的外部方法:

private fun setBackgroundWithImage(activity: Activity, image: Int) {
    val backgroundView= activity.findViewById<ImageSwitcher>(R.id.view_background)
    backgroundView.setImageResource(image)
}

view_background:_

<ImageSwitcher
    android:id="@+id/view_background"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:alpha="0.3"
    android:onClick="onTouchBackground"
    android:scaleType="centerCrop" />

这工作正常,褪色正确发生。

问题:

这是我用 Glide 尝试过的:

private fun setBackgroundWithImage(activity: Activity, url: String) {
    val backgroundView= activity.findViewById<ImageSwitcher>(R.id.view_background)
    Glide.with(activity)
            .asGif()
            .load(url)
            .into(backgroundView.nextView as ImageView)
    backgroundView.showNext()
}

但是,这不起作用,始终显示空白背景。为什么会这样?

标签: androidandroid-glideimageswitcher

解决方案


推荐阅读