绘图画布 [英] Drawing to the canvas

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

问题描述

我正在写一个Android应用程序,吸引直接在视图的OnDraw的事件在画布上。

I'm writing an android application that draws directly to the canvas on the onDraw event of a View.

我画的东西,包括单独绘制每个像素,为了这个,我使用类似:

I'm drawing something that involves drawing each pixel individually, for this I use something like:

for (int x = 0; x < xMax; x++) {
  for (int y = 0; y < yMax; y++){
    MyColour = CalculateMyPoint(x, y);
    canvas.drawPoint(x, y, MyColour);
  }
}

这里的问题是,这需要很长的时间,以画作为CalculateMyPoint例程是相当昂贵的方法

The problem here is that this takes a long time to paint as the CalculateMyPoint routine is quite an expensive method.

有没有画到画布上,比如我要画一个位图,然后绘制整个位图上的OnDraw事件画布的更有效的方法?或者,也许我的评价的颜色和填写一个数组,该OnDraw中的方法可以用它来画在画布上?

Is there a more efficient way of painting to the canvas, for example should I draw to a bitmap and then paint the whole bitmap to the canvas on the onDraw event? Or maybe evaluate my colours and fill in an array that the onDraw method can use to paint the canvas?

我的应用程序的用户将能够更改影响在画布上绘制参数。这是目前慢得令人难以置信。

Users of my application will be able to change parameters that affect the drawing on the canvas. This is incredibly slow at the moment.

推荐答案

随着人指出,如果CalculateMyPixel()是昂贵的,具有所谓的150000(HVGA)或384,00倍(WVGA),只是要杀死你。

As people have pointed out, if CalculateMyPixel() is expensive, having that called 150,000 (HVGA) or 384,00 times (WVGA) is just going to kill you.

在最重要的是,尝试通过canvas.drawPoint提醒你的用户界面为单个像素(),每次有更新的只是效率最低的方式来做到这一点。

On top of that, trying to draw your UI as individual pixels via canvas.drawPoint() each time there is an update is just about the least efficient way to do this.

如果要绘制单个像素,你几乎可以肯定希望有某种含有像素,你画一个简单的Canvas.drawBitmap()离屏位图的。

If you are drawing individual pixels, you almost certainly want to have some kind of off-screen bitmap containing the pixels, which you draw with a simple Canvas.drawBitmap().

然后你可以决定要管理该位图的最佳方式。这简单的方法是只让所需大小的位图对象,并使用这些API那里填写入。

Then you can decide the best way to manage that bitmap. This simplest is to just make a Bitmap object of the desired size and use the APIs there to fill it in.

另外,还有一个版本drawBitmap(中),它的原始整数数组,这样你就可以直接填写,在与任何值你想要的,避免了要求每个像素的方法。

Alternatively, there is a version of drawBitmap() that takes a raw integer array, so you can fill that in directly with whatever values you want, avoiding a method call for each pixel.

现在你可以将你的像素计算出的OnDraw()方法,它需要快速有响应的用户界面,并填写您的像素别的地方。也许你在初始化时一次计算它们。也许你计算他们,这样做只有部分是调用无效()(例如,从(0,0)像素之前已经改变选择性更新 - (10,10)已经改变,所以拿现在的位图,只需要修改该区域)。如果计算像素真的只是本质慢,你可能会想创建一个单独的线程来完成这项工作(更新与新像素的位图,然后postInvalidate()上的用户界面,让他们绘制的)。

Now you can move your pixel calculation out of the onDraw() method, which needs to be fast to have a responsive UI, and fill your pixels in somewhere else. Maybe you compute them once at initialization time. Maybe you compute them, and do selective updates of only the parts that have changed before calling invalidate() (for example the pixels from (0,0)-(10,l0) have changed so take the current bitmap and just modify that area). If computing pixels is really just intrinsically slow, you will probably want to create a separate thread to do that work in (updating the bitmap with the new pixels, then postInvalidate() on the UI to get them drawn).

另外现在,你有你的像素的位图,你可以做花样喜欢做的位图较小的尺寸和绘图到屏幕时,需要提升它,让你拿少得多的时间更新这些像素,同时还填补了整个用户界面(虽然以较低的分辨率)。

Also now that you have your pixels in a bitmap, you can do tricks like make the size of the bitmap smaller and scale it when drawing to the screen, allowing you to take much less time updating those pixels while still filling the entire UI (albeit at a lower resolution).

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

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