Picture
android.graphics.Picture
Picture可以记录Canvas上(通过Picture.beginRecording返回的Canvas)的绘制操作,然后通过picture.draw(Canvas)或者是canvas.drawPicture()把它们绘制到canvas中.
对于大多数绘制内容例如文本,矩形,直线来说,使用picture绘制要比直接绘制的API调用要快很多,因为Picture执行回放时不会引起任何方法调用开销
- Picture() 创建一张空白的Picture用来记录绘制操作
Picture(Picture src) 通过复制src中已经记录的内容来创建图片
beginRecording(int width, int height) 开始记录绘制过程,将记录的内容返回到画布中
- endRecording() 结束录制绘制过程
- getHeight()
- getWidth()
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
drawSomething(mPicture.beginRecording(200, 100))
mPicture.endRecording()
mDrawable = PictureDrawable(mPicture)
canvas?.drawColor(Color.WHITE)
// 将picture记录的绘制内容按照原始大小绘制到canvas中
canvas?.drawPicture(mPicture)
// 将picture记录的绘制内容按照缩放大小绘制到canvas指定的边框中
canvas?.drawPicture(mPicture, RectF(0f, 100f, 100f, 200f))
canvas?.drawPicture(mPicture, RectF(0f, 200f, 800f, 300f))
// 给drawable指定一个边界,在调用draw()的时候时候,边框内的内容是可以绘制的,在绘制的时候不会缩放内容,而是剪切出对应大小的内容.
mDrawable.setBounds(0, 300, 200, 400)
mDrawable.draw(canvas)
mDrawable.setBounds(0, 400, 100, 500)
mDrawable.draw(canvas)
mDrawable.setBounds(0, 500, 50, 600)
mDrawable.draw(canvas)
}
internal fun drawSomething(canvas: Canvas) {
val p = Paint(Paint.ANTI_ALIAS_FLAG)
p.color = -0x77010000
canvas.drawCircle(50f, 50f, 40f, p)
p.color = Color.GREEN
p.textSize = 30f
canvas.drawText("Pictures", 60f, 60f, p)
}