use glide for loading static gifs for better performance

This commit is contained in:
tibbi 2019-07-02 11:53:18 +02:00
parent 1645ecb8fa
commit 2bc12a86d1

View file

@ -452,20 +452,21 @@ fun Context.loadImage(type: Int, path: String, target: MySquareImageView, horizo
loadJpg(path, target, cropThumbnails, skipMemoryCacheAtPaths) loadJpg(path, target, cropThumbnails, skipMemoryCacheAtPaths)
} }
} else if (type == TYPE_GIFS) { } else if (type == TYPE_GIFS) {
if (!animateGifs) {
loadStaticGIF(path, target, cropThumbnails, skipMemoryCacheAtPaths)
return
}
try { try {
val gifDrawable = GifDrawable(path) val gifDrawable = GifDrawable(path)
target.setImageDrawable(gifDrawable) target.setImageDrawable(gifDrawable)
if (animateGifs) { gifDrawable.start()
gifDrawable.start()
} else {
gifDrawable.stop()
}
target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER
} catch (e: Exception) { } catch (e: Exception) {
loadJpg(path, target, cropThumbnails, skipMemoryCacheAtPaths) loadStaticGIF(path, target, cropThumbnails, skipMemoryCacheAtPaths)
} catch (e: OutOfMemoryError) { } catch (e: OutOfMemoryError) {
loadJpg(path, target, cropThumbnails, skipMemoryCacheAtPaths) loadStaticGIF(path, target, cropThumbnails, skipMemoryCacheAtPaths)
} }
} else if (type == TYPE_SVGS) { } else if (type == TYPE_SVGS) {
loadSVG(path, target, cropThumbnails) loadSVG(path, target, cropThumbnails)
@ -525,6 +526,22 @@ fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boo
.into(target) .into(target)
} }
fun Context.loadStaticGIF(path: String, target: MySquareImageView, cropThumbnails: Boolean, skipMemoryCacheAtPaths: ArrayList<String>? = null) {
val options = RequestOptions()
.signature(path.getFileSignature())
.skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true)
.priority(Priority.LOW)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
val builder = Glide.with(applicationContext)
.asBitmap() // make sure the GIF wont animate
.load(path)
if (cropThumbnails) options.centerCrop() else options.fitCenter()
builder.apply(options)
.into(target)
}
fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boolean) { fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boolean) {
target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER