如何块复制()中的机器人? [英] How to blit() in android?

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

问题描述

我已经习惯了处理与老派的图形库(快板,GD,pygame的),在那里,如果我想要一个位图的一部分复制到另一个......我只是用块传送。

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.

修改的:更precise ... 如果位图是只读的,和帆布是只写,我不能块复制阿成B,然后B插入C 2

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?

推荐答案

在code复制一个位图到另一个是这样的:

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

这指定要复制一个位图的左上角(50×50),然后伸了成150×150点阵,并写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.

您可以通过触发无效()画,但我建议使用SurfaceView如果你正在做动画。与无效的问题是,它只能绘制一旦线程进入闲置状态,因此你不能用它在一个循环 - 它只能绘制的最后一帧。这里有一些链接到其他的问题我已经回答了有关图形,他们可能会使用来解释我的意思。

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.

  • <一个href="http://stackoverflow.com/questions/2539396/new-to-android-drawing-a-view-at-runtime/2539549#2539549">How绘制一个矩形(空或填充,以及一些其他选项)
  • <一个href="http://stackoverflow.com/questions/2554871/android-how-to-position-view-off-screen/2560731#2560731">How创建一个自定义的SurfaceView动画
  • <一个href="http://stackoverflow.com/questions/2564171/view-animation-resizing-a-ball/2564483#2564483">Links为一个应用程序的code ,并在屏幕上随机弹跳球,还包括触摸控制
  • <一个href="http://stackoverflow.com/questions/2438733/how-to-do-animations-in-android-chess-game/2438763#2438763">Some更多信息有关SurfaceView对的Invalidate()
  • <一个href="http://stackoverflow.com/questions/2439301/animating-and-rotating-an-image-in-a-surface-view/2439548#2439548">Some与手动旋转的东西
  • 困难
  • 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获得画布(),那么我不认为你可以复制那是在它变成一个位图的剩余数据。但是,这并不是该控制什么 - 你只比当你排序的一切使用了,你准备好绘制

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)

您就可以做任何的转换和绘图OPS你想要的。 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, ....)

这样,你总是在不知不觉yourBitmap有任何信息在里面你想preserve。

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

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

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