misp-bump/app/src/main/java/lu/circl/mispbump/auxiliary/TileDrawable.kt

49 lines
1.3 KiB
Kotlin
Raw Normal View History

2019-06-11 14:52:41 +02:00
package lu.circl.mispbump.auxiliary
import android.graphics.Bitmap
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.Shader
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
class TileDrawable(drawable: Drawable, tileMode: Shader.TileMode) : Drawable() {
private val paint: Paint
init {
paint = Paint().apply {
shader = BitmapShader(getBitmap(drawable), tileMode, tileMode)
}
}
override fun draw(canvas: Canvas) {
canvas.drawPaint(paint)
}
override fun setAlpha(alpha: Int) {
paint.alpha = alpha
}
override fun getOpacity() = PixelFormat.TRANSLUCENT
override fun setColorFilter(colorFilter: ColorFilter?) {
paint.colorFilter = colorFilter
}
private fun getBitmap(drawable: Drawable): Bitmap {
if (drawable is BitmapDrawable) {
return drawable.bitmap
}
val bmp = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888)
val c = Canvas(bmp)
drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
drawable.draw(c)
return bmp
}
}