如何在android中进行blit()?

发布于 2024-08-28 15:20:39 字数 268 浏览 4 评论 0原文

我习惯于使用老式库(allegro、GD、pygame)处理图形,如果我想将位图的一部分复制到另一个位图中......我只需使用 blit。

我试图弄清楚如何在 android 中做到这一点,但我感到非常困惑。 那么...我们有这些只写的画布和只读的位图吗?这似乎太愚蠢了,不太真实,一定是我错过了一些东西,但我真的想不出来。

编辑:更准确地说... 如果位图是只读的,而画布是只写的,我不能将 A 传输到 B,然后将 B 传输到 C?

I'm used to handle graphics with old-school libraries (allegro, GD, pygame), where if I want to copy a part of a bitmap into another... I just use blit.

I'm trying to figure out how to do that in android, and I got very confused.
So... we have these Canvas that are write-only, and Bitmaps that are read-only? It seems too stupid to be real, there must be something I'm missing, but I really can't figure it out.

edit: to be more precise... if bitmaps are read only, and canvas are write only, I can't blit A into B, and then B into C?

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

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

发布评论

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

评论(2

冷情 2024-09-04 15:20:39

将一个位图复制到另一个位图的代码如下所示:

Rect src = new Rect(0, 0, 50, 50); 
Rect dst = new Rect(50, 50, 200, 200);  
canvas.drawBitmap(originalBitmap, src, dst, null);

这指定您要复制位图的左上角(50x50),然后将其拉伸为 150x150 位图,并将其写入距位图左上角偏移 50px 的位置。帆布。

您可以通过 invalidate() 触发绘图,但如果您正在制作动画,我建议使用 SurfaceView。 invalidate 的问题在于,它仅在线程空闲时绘制,因此您不能在循环中使用它 - 它只会绘制最后一帧。以下是我回答过的有关图形的其他问题的一些链接,它们可能有助于解释我的意思。


为了回应评论,这里有更多信息:
如果您从 SurfaceHolder.lockCanvas() 获取画布,那么我认为您无法将其中的残留数据复制到位图中。但这不是该控件的用途 - 仅当您整理好所有内容并准备好绘制时才使用。

您想要做的是创建一个画布,使用它绘制位图,

Canvas canvas = new Canvas(yourBitmap)

然后您可以执行您想要的任何转换和绘制操作。 yourBitmap 将包含所有最新信息。 然后你可以像这样使用表面持有者:

Canvas someOtherCanvas = surfaceHolder.lockCanvas()
someOtherCanvas.drawBitmap(yourBitmap, ....)

这样你就总能得到你的位图,其中包含你想要保留的任何信息。

The code to copy one bitmap into another is like this:

Rect src = new Rect(0, 0, 50, 50); 
Rect dst = new Rect(50, 50, 200, 200);  
canvas.drawBitmap(originalBitmap, src, dst, null);

That specifies that you want to copy the top left corner (50x50) of a bitmap, and then stretch that into a 150x150 Bitmap and write it 50px offset from the top left corner of your canvas.

You can trigger drawing via invalidate() but I recommend using a SurfaceView if you're doing animation. The problem with invalidate is that it only draws once the thread goes idle, so you can't use it in a loop - it would only draw the last frame. Here are some links to other questions I've answered about graphics, they might be of use to explain what I mean.


In response to the comments, here is more information:
If you get the Canvas from a SurfaceHolder.lockCanvas() then I don't think you can copy the residual data that was in it into a Bitmap. But that's not what that control is for - you only use than when you've sorted everything out and you're ready to draw.

What you want to do is create a canvas that draws into a bitmap using

Canvas canvas = new Canvas(yourBitmap)

You can then do whatever transformations and drawing ops you want. yourBitmap will contain all the newest information. Then you use the surface holder like so:

Canvas someOtherCanvas = surfaceHolder.lockCanvas()
someOtherCanvas.drawBitmap(yourBitmap, ....)

That way you've always got yourBitmap which has whatever information in it you're trying to preserve.

怀里藏娇 2024-09-04 15:20:39

在 android 中,您绘制到画布,当您希望它更新时,您调用 invalidate ,这会将画布重新绘制到屏幕上。所以我猜你已经重写了视图的 onDraw 方法,所以只需添加 invalidate();

@Override
public void onDraw(Canvas canvas) {
    // Draw a bitmap to the canvas at 0,0
    canvas.drawBitmap(mBitmap, 0, 0, null);
    // Add in your drawing functions here
    super.onDraw(canvas);
    // Call invalidate to draw to screen
    invalidate();
}

上面的代码只是不断地重绘位图,当然您想添加额外的东西来绘制并考虑使用调用 invalidate 的计时函数,以便它不会不断运行。我建议您查看一下lunarlander 的来源。

In android you draw to the canvas, and when you want it to update you call invalidate which will the redraw this canvas to the screen. So I'm guessing you have overridden the onDraw method of your view so just add invalidate();

@Override
public void onDraw(Canvas canvas) {
    // Draw a bitmap to the canvas at 0,0
    canvas.drawBitmap(mBitmap, 0, 0, null);
    // Add in your drawing functions here
    super.onDraw(canvas);
    // Call invalidate to draw to screen
    invalidate();
}

The above code simply redraws the bitmap constantly, of course you want to add in extra thing to draw and consider using a timing function that calls invalidate so that it is not constantly running. I'd advice having a look at the lunarlander sources.

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