apply the rounded corners to images loaded by Picasso too

This commit is contained in:
tibbi 2021-02-18 20:59:38 +01:00
parent 6d46619cca
commit 15e7c72104
2 changed files with 43 additions and 4 deletions

View file

@ -476,7 +476,7 @@ fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boo
.apply(options)
.listener(object : RequestListener<Bitmap> {
override fun onLoadFailed(e: GlideException?, model: Any?, targetBitmap: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
tryLoadingWithPicasso(path, target, signature)
tryLoadingWithPicasso(path, target, cropThumbnails, roundCorners, signature)
return false
}
@ -495,17 +495,24 @@ fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boo
}
// intended mostly for Android 11 issues, that fail loading PNG files bigger than 10 MB
fun tryLoadingWithPicasso(path: String, view: MySquareImageView, signature: ObjectKey) {
fun Context.tryLoadingWithPicasso(path: String, view: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, signature: ObjectKey) {
var pathToLoad = "file://$path"
pathToLoad = pathToLoad.replace("%", "%25").replace("#", "%23")
try {
Picasso.get()
var builder = Picasso.get()
.load(pathToLoad)
.centerCrop()
.fit()
.stableKey(signature.toString())
.into(view)
if (roundCorners != ROUNDED_CORNERS_NONE) {
val cornerSize = if (roundCorners == ROUNDED_CORNERS_SMALL) R.dimen.rounded_corner_radius_small else R.dimen.rounded_corner_radius_big
val cornerRadius = resources.getDimension(cornerSize).toInt()
builder = builder.transform(RoundedCornersTransformation(cornerRadius.toFloat()))
}
builder.into(view)
} catch (e: Exception) {
}
}

View file

@ -0,0 +1,32 @@
package com.simplemobiletools.gallery.pro.helpers
import android.graphics.*
import com.squareup.picasso.Transformation
// taken from https://stackoverflow.com/a/35241525/1967672
class RoundedCornersTransformation(private val radius: Float) : Transformation {
override fun transform(source: Bitmap): Bitmap {
val size = Math.min(source.width, source.height)
val x = (source.width - size) / 2
val y = (source.height - size) / 2
val squaredBitmap = Bitmap.createBitmap(source, x, y, size, size)
if (squaredBitmap != source) {
source.recycle()
}
val bitmap = Bitmap.createBitmap(size, size, source.config)
val canvas = Canvas(bitmap)
val paint = Paint()
val shader = BitmapShader(squaredBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
paint.shader = shader
paint.isAntiAlias = true
val rect = RectF(0f, 0f, source.width.toFloat(), source.height.toFloat())
canvas.drawRoundRect(rect, radius, radius, paint)
squaredBitmap.recycle()
return bitmap
}
override fun key() = "rounded_corners"
}