Effective 2D drawing in Android

I searched for many hours and could not find a clear answer to my question. I have an application where I need to draw a sports field (including all tonal lines) on the screen. So far, I have expanded SurfaceView and pretty much copied the rest of the demo version of LunarLander. All the data that the application requires in order to pull the pitch to the right sizes comes from a socket that works great. However, per minute, in the onDraw () function, I draw all the lines of each frame, which causes a rather slow frame rate in the emulator (for example, ~ 10 frames per second). 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();
}

Then I draw circles and different positions against this background. My question is how to make this more efficient? Is there a way I can draw lines when initializing an application and not redraw them every frame?

Thanks for any help.

+3
source share
2 answers

You must be sure to cache any canvas drawing that will not be changed to a bitmap during initialization, and then draw this bitmap in onDraw (). This will help to do so many times. Sort of:

Bitmap mField = null;

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

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

? , , . : . , , G1 , .

0

Source: https://habr.com/ru/post/1758031/


All Articles