2019-09-10 03:50:59 +00:00
|
|
|
package org.thoughtcrime.securesms.loki
|
2019-09-06 01:49:02 +00:00
|
|
|
|
2019-09-10 03:50:59 +00:00
|
|
|
import android.graphics.*
|
2019-09-06 01:49:02 +00:00
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basically a [Bitmap] wrapper, the [Bitmap] size must be known when instantiating it
|
|
|
|
* but when drawing it will draw the [Bitmap] to fit the canvas.
|
|
|
|
*/
|
2019-09-10 03:50:59 +00:00
|
|
|
abstract class IdenticonDrawable(width: Int, height: Int, hash: Long) : Drawable() {
|
2019-09-06 01:49:02 +00:00
|
|
|
private val bitmapRect: Rect = Rect(0, 0, width, height)
|
|
|
|
private val destinationRect: Rect = Rect(0, 0, width, height)
|
|
|
|
private val bitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
|
|
|
private val canvas: Canvas = Canvas(bitmap)
|
|
|
|
private val bitmapPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
|
|
|
|
|
|
|
var hash: Long = hash
|
|
|
|
set(value) {
|
|
|
|
field = value
|
|
|
|
onSetHash(value)
|
|
|
|
invalidateBitmap()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected fun invalidateBitmap() {
|
|
|
|
drawBitmap(canvas)
|
|
|
|
invalidateSelf()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract fun drawBitmap(canvas: Canvas)
|
|
|
|
|
|
|
|
protected open fun onSetHash(newHash: Long) = Unit
|
|
|
|
|
|
|
|
override fun draw(canvas: Canvas) {
|
|
|
|
destinationRect.set(0, 0, canvas.width, canvas.height)
|
|
|
|
canvas.drawBitmap(bitmap, bitmapRect, destinationRect, bitmapPaint)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun setAlpha(i: Int) {
|
|
|
|
bitmapPaint.alpha = i
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun setColorFilter(colorFilter: ColorFilter?) {
|
|
|
|
bitmapPaint.colorFilter = colorFilter
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getOpacity(): Int {
|
|
|
|
return bitmapPaint.alpha
|
|
|
|
}
|
|
|
|
}
|