在画布上平铺位图

发布于 2024-10-15 05:00:50 字数 1110 浏览 11 评论 0原文

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

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

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

  • 我尝试在以下位置实现此解决方案我的 SurfaceView 的 Thread.run(),但我认为问题是当 BitmapDrawable 转换为 Bitmap 时...它会丢失平铺属性。

    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);

  • 如果我使用Canvas.drawBitmap 方法将目标 RectF 作为参数,看起来位图将平铺以填充 RectF。 ..但是如何可靠地声明一个填充整个视图区域的 RectF 呢?

  • 将活动背景设置为所需的方格纸外观也不起作用,因为位图/画布布局是不透明的并且会阻止其被看到。

有什么想法如何实现这一目标?

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.

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:

  • 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 = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.editor_graph));
    TileMe.setTileModeX(Shader.TileMode.REPEAT);
    TileMe.setTileModeY(Shader.TileMode.REPEAT);

    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?

  • Setting the Activities background to the desired graph paper look also doesn't work, as the bitmap/canvas layout is opaque and blocks that from being seen.

Any ideas how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虚拟世界 2024-10-22 05:00:50

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

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

You have two easy solutions:

  • Either use a BitmapDrawable, but instead of extracting the Bitmap, just call BitmapDrawable.draw(Canvas). Don't forget to set the drawable's bounds to fill your drawing area.
  • Create a Paint with a BitmapShader and draw a rectangle with it (this is basically what BitmapDrawable does).
孤者何惧 2024-10-22 05:00:50

我确信有一种方法可以使用 SurfaceView 获得平铺效果。不幸的是,您似乎无法将 BitmapDrawable 与画布一起使用。因此,您可能必须通过在 Canvas 上创建自己的一系列 Rect 并为每个矩形绘制缩放的位图来实现自己的自定义平铺方法。

老实说,这并没有那么难。只需获取视图的宽度/高度,并根据此数据创建一个 Rect 数组,您将在其中绘制 Bitmap

或者,如果您不需要即时修改实际平铺背景,只需将其绘制为背景并在其顶部绘制 SurfaceView 即可。您链接的那篇文章提供了多种可以实现的平铺 BitmapDrawable 解决方案。

I'm sure there is a way to get a tiled effect using a SurfaceView. Unfortunately, it looks like you can't use the BitmapDrawable with a canvas. So you would probably have to implement you own custom tiling method by creating your own series of Rect's on the Canvas and drawing a scaled bitmap to each one.

It honestly wouldn't be that hard. Just get the width/height of the view, and create an array of Rect's based on this data that you will draw the Bitmap to.

Alternatively, if you don't need to make modifications to the actual tiled background on the fly, just draw it as a background and draw the SurfaceView on top of it. That post you linked provided multiple solutions to tiling a BitmapDrawable that you could implement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文