在画布上平铺位图 [英] Tiling a Bitmap on a Canvas

查看:214
本文介绍了在画布上平铺位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为通过画布绘制的位图创建一个方格纸"外观,并试图找出实现此目的的最佳方法.

I would like to create a 'graph paper' look to the Bitmap I am drawing via a Canvas, and trying to figure out the best way to do this.

我无法将包含方格纸背景的源位图传递给Canvas构造函数,因为我是通过.lockCanvas()调用在SurfaceView中获得Canvas的.

I can't pass a source Bitmap containing the graph paper background to the Canvas constructor, as I am getting the Canvas in a SurfaceView via the .lockCanvas() call.

我尝试过的一些解决方案:

Some solutions I've tried:

  • 我尝试在SurfaceView的线程中实现此解决方案. run(),但是我相信的问题是,当BitmapDrawable转换为Bitmap ...时,它将失去平铺属性.

  • I've tried implementing this solution in my SurfaceView's Thread.run(), but the issue I believe is when the BitmapDrawable is converted to a Bitmap... it loses the tiling properties.

canvas = mSurfaceHolder.lockCanvas(null); BitmapDrawable TileMe =新的BitmapDrawable(BitmapFactory.decodeResource(getResources(),R.drawable.editor_graph)); TileMe.setTileModeX(Shader.TileMode.REPEAT); TileMe.setTileModeY(Shader.TileMode.REPEAT);

canvas = mSurfaceHolder.lockCanvas(null); BitmapDrawable TileMe = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.editor_graph)); TileMe.setTileModeX(Shader.TileMode.REPEAT); TileMe.setTileModeY(Shader.TileMode.REPEAT);

位图b = TileMe.getBitmap(); canvas.drawBitmap(b,0,0,null);

Bitmap b = TileMe.getBitmap(); canvas.drawBitmap(b, 0, 0, null);

如果我使用

If I use the Canvas.drawBitmap method that takes a destination RectF as a parameter, it looks like the bitmap will be tiled to fill the RectF... but how do I declare a RectF reliably that fills the entire view area?

有什么想法可以实现这一目标吗?

Any ideas how to achieve this?

推荐答案

您有两个简单的解决方案:

You have two easy solutions:

  • 可以使用BitmapDrawable,但不要提取Bitmap,而只需调用BitmapDrawable.draw(Canvas).不要忘记设置绘图对象的边界以填充您的绘图区域.
  • 使用BitmapShader创建一个Paint,并用它绘制一个矩形(这基本上就是BitmapDrawable所做的事情.)

这篇关于在画布上平铺位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆