Android中高效的2D绘图 [英] Efficient 2D drawing in Android

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

问题描述

我已经搜索了好几个小时,却找不到确切的答案。我有一个需要在屏幕上绘制运动场(包括所有沥青线)的应用程序。到目前为止,我已经扩展了SurfaceView并几乎复制了LunarLander演示的其余部分。
应用程序需要从插座接收所有将间距绘制为正确尺寸所需的数据,该插座也能正常工作。但是,在onDraw()函数的最后一刻,我正在每帧绘制所有线条,这在模拟器中导致相当慢的帧速率(例如〜10fps)。这是我的onDraw()函数:

I have searched for quite a few hours and have not been able to find a concise definite andswer to my question. I have an application where I need to draw a sports field (including all pitch lines) to the screen. So far, I have extended the SurfaceView and pretty much copied the rest of the LunarLander demo as well. All the data the application requires to draw the pitch to the correct dimensions is being received from a socket which works fine too. However, at the minute in the onDraw() function, I am drawing all lines each frame which is causing a fairly slow framerate in the emulator (e.g. ~10fps). Here is my onDraw() function:

@Override
public void onDraw(Canvas canvas) {
canvas.drawARGB(255,0,144,0);
canvas.drawLine(canvas, getFirstLine(), mPaint);
canvas.drawRect(canvas, getFirstRect(), mPaint);
canvas.drawRect(canvas, getSecondRect(), mPaint);
...
canvas.drawRect(canvas, getSecondRect(), mPaint);
drawAnimatedObjects();
}

然后我在此背景上绘制圆圈和不同位置。我的问题是如何使它更有效?有没有一种方法可以在应用程序初始化时画线,而不必在每一帧都重新画线?

I then draw circles and different positions over this background. My question is how do I make this more efficient? Is there a way that I can draw the lines at the application initialisation and not have to redraw them every frame?

感谢您的帮助。

推荐答案

您绝对应该缓存在初始化时不会更改为位图的所有画布图形,然后在onDraw()中绘制该位图。这将大大改善渲染时间。

You should definitely be caching any canvas drawing which will not change to a bitmap at initialization time and then draw that bitmap in onDraw(). That will help render times a lot. Something like:

Bitmap mField = null;

void init()
{
  mField = new Bitmap(...dimensions...);
  Canvas c = new Canvas(mField);
  c.drawRect(...);
  ...
}

void onDraw(Canvas c)
{
  c.drawBitmap(mField);
}

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

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