如何在android中blit()? [英] How to blit() in android?

查看:22
本文介绍了如何在android中blit()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我试图弄清楚如何在 android 中做到这一点,但我很困惑.所以......我们有这些只写的画布和只读的位图?这似乎太愚蠢了,不可能是真的,一定是我遗漏了什么,但我真的想不通.

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.

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

推荐答案

复制一个位图到另一个位图的代码是这样的:

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

这指定您要复制位图的左上角 (50x50),然后将其拉伸为 150x150 位图,并将其写入距画布左上角 50 像素的偏移处.

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.

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

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.

  • How to draw a rectangle (empty or filled, and a few other options)
  • How to create a custom SurfaceView for animation
  • Links to the code for an app with randomly bouncing balls on the screen, also including touch control
  • Some more info about SurfaceView versus Invalidate()
  • Some difficulties with manually rotating things

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

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)

然后您可以进行任何您想要的转换和绘图操作.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.

这篇关于如何在android中blit()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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