From 15e7c7210456cb6b4ebd8cf2bbf69d89630a2f65 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 18 Feb 2021 20:59:38 +0100 Subject: [PATCH] apply the rounded corners to images loaded by Picasso too --- .../gallery/pro/extensions/Context.kt | 15 ++++++--- .../PicassoRoundedCornersTransformation.kt | 32 +++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/PicassoRoundedCornersTransformation.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index e46837729..2c3e5d2cd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -476,7 +476,7 @@ fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boo .apply(options) .listener(object : RequestListener { override fun onLoadFailed(e: GlideException?, model: Any?, targetBitmap: Target?, 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) { } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/PicassoRoundedCornersTransformation.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/PicassoRoundedCornersTransformation.kt new file mode 100644 index 000000000..109807d5d --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/PicassoRoundedCornersTransformation.kt @@ -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" +}