Bitmap
A Bitmap is a store of Bitmap information, that is, the color information of each pixel of a rectangular image.
Bitmap and Drawable conversions
In fact, since bitmaps and Drawable are two different concepts, they don’t exactly “convert” each other, but rather get objects from one and the other:
- Bitmap -> Drawable: Creates a BitmapDrawable.
- Drawable -> Bitmap: if it is a BitmapDrawable, use
Bitmapdrawable.getbitmap (); If not, create a Bitmap and a Canvas and use Drawable to draw the content from the Canvas to the Bitmap.
Drawable
- Drawable is an overhead tool that calls the Canvas for drawing. Drawable. Draw (Canvas) draws the Drawable content to the Canvas
In the.
- Drawable stores the drawing rule internally, which can be a concrete Bitmap, a pure color, or even an abstract, flexible description. A Drawable may contain no specific pixel information, as long as it contains enough information to draw when the draw(Canvas) method is called.
- Since Drawable stores only drawing rules, it needs to call drawable.setbounds () to set its drawing bounds before its draw() method is called.
A custom Drawable
Customize an implementation of ColorDrawable
ColorDrawable
class DrawableView(context: Context? , attrs: AttributeSet?) : View(context, attrs) { private val drawable = ColorDrawable(Color.RED) override fun onDraw(canvas: Canvas) { super.onDraw(canvas) drawable.setBounds(50.dp.toInt(), 80.dp.toInt(), width, height) drawable.draw(canvas) } }Copy the code
The effect
Grid lines Drawable
- Override several abstract methods
- When you overwrite setAlpha() remember to overwrite getAlpha()
- Rewrite the draw(Canvas) method and do the actual drawing inside
- For example: MeshDrawable
class MeshDrawable:Drawable() { private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.parseColor("#F9A825") strokeWidth = 5.dp } private val INTERVAL = 50.dp override fun draw(canvas: Canvas) {var x = bounds.left. ToFloat () while (x < bounds.right){Canvas. DrawLine (x,bounds.top. ToFloat (),x, Bounds.bottom.tofloat (),paint) x += INTERVAL} var y = bounds.top.tofloat () while (y < bounds.bottom){ canvas.drawLine(bounds.left.toFloat(),y, bounds.right.toFloat(),y,paint) y += INTERVAL } } ... }Copy the code
And then we can do that in our DrawableView
private val drawable = MeshDrawable()
Copy the code
The effect
For what?
- You need to share drawing code across multiple views, write it in Drawable, and then reference the same Drawable across multiple custom views without pasting code from each other.
- For example?
- Stock software multiple candle chart interface, you can share the candle chart interface put in
- Share the Drawable
Drawable class CandleDrawable: Drawable()Copy the code
- Simple candle view
class SimpleCandleView(context: Context? , attrs: AttributeSet?) : View(context, attrs) { val drawable = CandleDrawable() ... }Copy the code
- Complex candle view
class DetailedCandleView(context: Context? , attrs: AttributeSet?) : View(context, attrs) { val drawable = CandleDrawable() ... }Copy the code
- Stock software multiple candle chart interface, you can share the candle chart interface put in