diff --git a/app/src/main/java/com/gh/common/util/ImageUtils.kt b/app/src/main/java/com/gh/common/util/ImageUtils.kt index e0866f87d6..9d7e100731 100644 --- a/app/src/main/java/com/gh/common/util/ImageUtils.kt +++ b/app/src/main/java/com/gh/common/util/ImageUtils.kt @@ -286,61 +286,61 @@ object ImageUtils { */ @JvmStatic fun display(view: SimpleDraweeView?, url: String?) { - url?.let { - val width = view?.layoutParams?.width - val height = view?.layoutParams?.height + if (url.isNullOrEmpty()) return - var lowResUrl = "" - var highResUrl = "" + val width = view?.layoutParams?.width + val height = view?.layoutParams?.height - // 找同一图片地址已加载过的图片作为低质量预览图 - // TODO 根据实际请求大小(w_width)来避免小图用大图作为低质量图片 - for (cachedImageUrl in mImageUrlCacheSet) { - if (cachedImageUrl.contains(url)) { - lowResUrl = cachedImageUrl - break - } + var lowResUrl = "" + var highResUrl = "" + + // 找同一图片地址已加载过的图片作为低质量预览图 + // TODO 根据实际请求大小(w_width)来避免小图用大图作为低质量图片 + for (cachedImageUrl in mImageUrlCacheSet) { + if (cachedImageUrl.contains(url)) { + lowResUrl = cachedImageUrl + break } + } - val loadImageClosure: (autoPlay: Boolean, highResUrl: String, lowResUrl: String) -> Unit = { autoPlay, hUrl, lUrl -> - view?.controller = Fresco.newDraweeControllerBuilder() - .setImageRequest(ImageRequest.fromUri(hUrl)) - .apply { - if (lUrl.isNotEmpty() - && lUrl != hUrl - && hUrl != view?.getTag(R.string.highResImageTag)) { - lowResImageRequest = ImageRequest.fromUri(lUrl) - } - autoPlayAnimations = autoPlay + val loadImageClosure: (autoPlay: Boolean, highResUrl: String, lowResUrl: String) -> Unit = { autoPlay, hUrl, lUrl -> + view?.controller = Fresco.newDraweeControllerBuilder() + .setImageRequest(ImageRequest.fromUri(hUrl)) + .apply { + if (lUrl.isNotEmpty() + && lUrl != hUrl + && hUrl != view?.getTag(R.string.highResImageTag)) { + lowResImageRequest = ImageRequest.fromUri(lUrl) } - .build() + autoPlayAnimations = autoPlay + } + .build() - view?.setTag(R.string.highResImageTag, highResUrl) + view?.setTag(R.string.highResImageTag, highResUrl) + } + + val shouldLoadAsGif = url.endsWith(".gif") && view?.getTag(R.id.tag_show_gif) != false + + if (shouldLoadAsGif && view?.tag == url) return + + if (width != null && width > 0) { + highResUrl = if (shouldLoadAsGif) { + resizeGif(url, width, height ?: 0) + } else { + getTransformLimitUrl(url, width, view.context) ?: "" } - - val shouldLoadAsGif = it.endsWith(".gif") && view?.getTag(R.id.tag_show_gif) != false - - if (shouldLoadAsGif && view?.tag == url) return - - if (width != null && width > 0) { + loadImageClosure(shouldLoadAsGif, highResUrl, lowResUrl) + } else { + view?.post { highResUrl = if (shouldLoadAsGif) { - resizeGif(url, width, height ?: 0) + resizeGif(url, view.width, height ?: 0) } else { - getTransformLimitUrl(url, width, view.context) ?: "" + getTransformLimitUrl(url, view.width, view.context) ?: "" } loadImageClosure(shouldLoadAsGif, highResUrl, lowResUrl) - } else { - view?.post { - highResUrl = if (shouldLoadAsGif) { - resizeGif(url, view.width, height ?: 0) - } else { - getTransformLimitUrl(url, view.width, view.context) ?: "" - } - loadImageClosure(shouldLoadAsGif, highResUrl, lowResUrl) - } } - view?.tag = url } + view?.tag = url } // Wifi/4G:x2 traffic:x1