1. Bitmap and Drawable

A Bitmap is essentially a collection of pixels in an image.

Drawable is not essentially a graph, but rather an image-loading form similar to a View.

In fact, there is no reciprocal relationship between the two, because they are essentially two different things, they are actually a production relationship, Bitmap can produce Drawable, Drawable can produce Bitmap.

2, Bitmap produces Drawable

KTX has a ready-made encapsulation method:

val bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888)
bitmap.toDrawable(resources)
Copy the code

ToDrawable toDrawable toDrawable

/** Create a [BitmapDrawable] from this [Bitmap]. */
public inline fun Bitmap.toDrawable(
    resources: Resources
): BitmapDrawable = BitmapDrawable(resources, this)
Copy the code

The BitmapDrawable class acts as an intermediate production tool

Drawable produces Bitmap

KTX has a ready-made encapsulation method:

val drawable = ColorDrawable()
drawable.toBitmap()
Copy the code

ToBitmap toBitmap

public fun Drawable.toBitmap( @Px width: Int = intrinsicWidth, @Px height: Int = intrinsicHeight, config: Config? = null ): Bitmap {the if (this is BitmapDrawable) {/ / if is already a BitmapDrawable if (config = = null | | Bitmap. The config = = config) {/ / Fast-path to return original. Bitmap.createScaledBitmap will do this check, but it // involves allocation and two jumps into native code so we perform the check ourselves. if (width == IntrinsicWidth && height = = intrinsicHeight) {return bitmap / / returned directly} return bitmap. CreateScaledBitmap (bitmap, width, Height, true)}} val (oldLeft, oldTop, oldRight, oldBottom) = bounds Val bitmap = bitmap. createBitmap(width, height, config? : Config.ARGB_8888) setBounds(0, 0, width, height) draw(Canvas(bitmap)) setBounds(oldLeft, oldTop, oldRight, oldBottom) return bitmap }Copy the code

4. Customize Drawable

In most cases, a Drawable is sufficient, but sometimes we need to draw different custom views on a basic control (custom Drawable), so it is necessary to customize a Drawable. For example, if we need to draw pieces on a chess board, we can easily draw a chess board for the View and reduce the nesting hierarchy optimization performance of the View. Look at the following code:

To achieve the checkerboard effect

A custom Drawable

Private const val GRID_NUMBER = 1 private const val GRID_NUMBER = 1 Drawable() {private var tempX = 1 private var tempY = 1 private val paint = paint (paint.anti_aliAS_flag).apply { color = Color.BLACK style = Paint.Style.FILL } override fun draw(canvas: Canvas) { while (tempX <= GRID_NUMBER) { tempY = 1 if (tempX % 2 ! While (tempY <= GRID_NUMBER) {if (tempY % 2! Paint. Color = "#ECAB98".tocolorint ()} else {// Paint. Color = "#FB7449".tocolorint ()} DrawChess (canvas) tempY++}} else {// Even lines while (tempY <= GRID_NUMBER) {if (tempY % 2! Paint. Color = "#ECAB98".tocolorint ()} else {// Paint. Color = "#ECAB98".tocolorint ()} drawChess(canvas) tempY++ } } tempX++ } } private fun drawChess(canvas: Canvas) { var xLeft = bounds.left.toFloat() var yTop = bounds.top.toFloat() var xRight = bounds.right.toFloat() var yBottom = bounds.bottom.toFloat() var interval = (xRight - xLeft) / GRID_NUMBER canvas.drawRect( xLeft + (tempY - 1) * interval, yTop + (tempX - 1) * interval, xLeft + tempY * interval, yTop + tempX * interval, paint ) } override fun setAlpha(alpha: Int) { paint.alpha = alpha } override fun getAlpha(): Int { return paint.alpha } override fun setColorFilter(colorFilter: ColorFilter?) { paint.colorFilter = colorFilter } override fun getColorFilter(): ColorFilter? {return paint. ColorFilter} // Override Fun getOpacity(): Int {return when (paint. Alpha) {0 -> pixelFormat. TRANSPARENT // TRANSPARENT 0xFF -> pixelFormat. OPAQUE // else -> Pixelformat.always // TRANSLUCENT}}}Copy the code

Use in custom View

class ChessView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) { private val drawable = ChessDrawable() override fun onDraw(canvas: Drawable. setBounds(0, 0, width, height) drawable.draw(Canvas)}}Copy the code

Results show

Personal study notes