Skip to content

Instantly share code, notes, and snippets.

@masaibar
Last active September 14, 2018 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaibar/2c6eb1a6e3cf38ace0e5e7b2a5961e21 to your computer and use it in GitHub Desktop.
Save masaibar/2c6eb1a6e3cf38ace0e5e7b2a5961e21 to your computer and use it in GitHub Desktop.
Picassoでwrap_contentなImageViewに画像を読み込む際に縦横それぞれN倍する ref: https://qiita.com/masaibar/items/7aba5cb506fe652d49e7
class ScaleTransformation(
private val scale: Float
) : Transformation {
override fun transform(source: Bitmap?): Bitmap? {
source ?: return null
//1倍にするとcreateBitmapしてもsourceを返そうとして死ぬ
if (scale == 1f) {
return source
}
val matrix = Matrix().apply {
setScale(
scale,
scale
)
}
val result = Bitmap.createBitmap(
source,
0,
0,
source.width,
source.height,
matrix,
true
)
source.recycle()
return result
}
override fun key(): String =
"ScaleTransformation(scale = $scale)"
}
Picasso.get()
.load(imageUrl)
.transform(ScaleTransformation(2f)) //← 倍率を指定
.into(image_view)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment