com.isyscore.kotlin.android.DrawableExtension.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-android Show documentation
Show all versions of common-android Show documentation
iSysCore Common Android Library
The newest version!
@file:Suppress("unused", "DEPRECATION")
package com.isyscore.kotlin.android
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.PixelFormat
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
fun Drawable.bitmap(): Bitmap = Bitmap.createBitmap(
intrinsicWidth,
intrinsicHeight,
if (opacity != PixelFormat.OPAQUE) Bitmap.Config.ARGB_8888 else Bitmap.Config.RGB_565
).apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
draw(Canvas(this))
}
fun Drawable.scale(newWidth: Float, newHeight: Float): Drawable =
BitmapDrawable(Bitmap.createBitmap(bitmap(), 0, 0, intrinsicWidth, intrinsicHeight, Matrix().apply {
postScale(newWidth / intrinsicWidth, newHeight / intrinsicHeight)
}, true))