滑音转换不适用于听众?

时间:2018-04-13 12:40:14

标签: android-glide

我的下面有我的Glide代码

    Glide.with(this)
            .load("https://flybubble.com/media/wysiwyg/images/home/mainpage-box-5L.jpg")
            .transition(DrawableTransitionOptions.withCrossFade())
            .apply(RequestOptions()
                    .placeholder(R.drawable.placeholder)
                    .centerCrop()
                    .transforms(CenterCrop(), RoundedCorners(1000)))
            .into(my_image_view)

一切正常,加载图片时,withCrossFade()显示它已淡入。

但是,当我按如下方式添加监听器时

    Glide.with(this)
            .load("https://flybubble.com/media/wysiwyg/images/home/mainpage-box-5L.jpg")
            .transition(DrawableTransitionOptions.withCrossFade())
            .apply(RequestOptions()
                    .placeholder(R.drawable.placeholder)
                    .centerCrop()
                    .transforms(CenterCrop(), RoundedCorners(1000)))
            .listener(object: RequestListener<Drawable>{
                override fun onLoadFailed(e: GlideException?, model: Any, target: Target<Drawable>, isFirstResource: Boolean): Boolean {
                    return false
                }
                override fun onResourceReady(resource: Drawable, model: Any, target: Target<Drawable>, dataSource: DataSource, isFirstResource: Boolean): Boolean {
                    my_image_view.setImageDrawable(resource)
                    return true
                }
            })
            .into(my_image_view)

withCrossFade()无法再使用了。是因为transition不能与listener一起使用,还是我错过了什么?

2 个答案:

答案 0 :(得分:1)

找到这样做的方法不是通过转换,而是Transition来代替target.onResourceReady

Glide.with(this)
        .load("https://flybubble.com/media/wysiwyg/images/home/mainpage-box-5L.jpg")
        //.transition(DrawableTransitionOptions.withCrossFade()) //Remove this line
        .apply(RequestOptions()
                .placeholder(R.drawable.placeholder)
                .centerCrop()
                .transforms(CenterCrop(), RoundedCorners(1000)))
        .listener(object: RequestListener<Drawable>{
            override fun onLoadFailed(e: GlideException?, model: Any, target: Target<Drawable>, isFirstResource: Boolean): Boolean {
                return false
            }
            override fun onResourceReady(resource: Drawable, model: Any, target: Target<Drawable>, dataSource: DataSource, isFirstResource: Boolean): Boolean {
                target.onResourceReady(resource, DrawableCrossFadeTransition(1000, !isFirstResource))
                return true
            }
        })
        .into(my_image_view)

答案 1 :(得分:0)

如果没有咨询Glide文档,我会说这是因为你已经覆盖了Glides部分工作。您在Drawable中获取了图书馆为您提取的onResourceReady,而不是使用Glide加载图片,而是将其强制加入ImageView。在这个例子中,我将Glides函数比作一个可绘制的下载器,而你是将图像加载到视图中的那个。