Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
flick/sample/src/main/java/me/saket/flick/sample/viewer/PicassoPaddingTransformation.kt
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
37 lines (29 sloc)
1.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.saket.flick.sample.viewer | |
import android.graphics.Bitmap | |
import android.graphics.Canvas | |
import android.graphics.Paint | |
import androidx.annotation.ColorInt | |
import com.squareup.picasso.Transformation | |
/** Adds a solid padding around an image. */ | |
class PicassoPaddingTransformation( | |
private val paddingPx: Float, | |
@ColorInt private val paddingColor: Int | |
) : Transformation { | |
override fun key() = "padding_$paddingPx" | |
override fun transform(source: Bitmap): Bitmap { | |
if (paddingPx == 0F) { | |
return source | |
} | |
val targetWidth = source.width + paddingPx * 2F | |
val targetHeight = source.height + paddingPx * 2F | |
// It would have been nice if Picasso offered a Bitmap pool, just like Glide. | |
val bitmapWithPadding = Bitmap.createBitmap(targetWidth.toInt(), targetHeight.toInt(), Bitmap.Config.ARGB_8888) | |
val canvas = Canvas(bitmapWithPadding) | |
val paint = Paint() | |
paint.color = paddingColor | |
canvas.drawRect(0F, 0F, targetWidth, targetHeight, paint) | |
canvas.drawBitmap(source, paddingPx, paddingPx, null) | |
source.recycle() | |
return bitmapWithPadding | |
} | |
} |