0

I'm building a simple Android game which does not require frame-rate speed. Think of it as a simple chess game.

I have created a custom view, where I draw the whole game using onDraw():

public class GameView extends View {
   public GameView (Context context, AttributeSet attrs) {
      super(context, attrs);
      ...
   }
   protected void onDraw(Canvas canvas) {
      ...
   }
}

Currently in prototyping stage, I'm just drawing lines, circles, and rectangles, but not any bitmaps.

I'm drawing each time a player makes a move using invalidate() (just 1 frame per player move).

The problem is that when the view is already drawn, I notice a frame-rate drop in the user interface, when for example I'm displaying a dialog or a toast to the user. It's happening when the view is already drawn, not the moment I'm drawing.

That's strange, isn't it? Since the view is already drawn, why do I notice this kind of latency in the user interface?

steliosf
  • 3,669
  • 2
  • 31
  • 45

1 Answers1

0

I think you should take a look at SurfaceView, which might be what you are looking for.

Views are all drawn on the same GUI thread which is also used for all user interaction.

So you should use SurfaceView, which dedicates a thread to actually draw on the canvas.

I also recommend Difference between SurfaceView and View?

Community
  • 1
  • 1
James
  • 3,580
  • 3
  • 25
  • 36
  • Thanks, I'll try that. I've read the official documentation and it's written that "If your application does not require a significant amount of processing or frame-rate speed (perhaps for a chess game, a snake game, or another slowly-animated application), then you should consider creating a custom View component". Why the official documentation suggests the custom view instead of the SurfaceView? Are there any drawbacks on using the SurfaceView? – steliosf Feb 11 '17 at 19:24
  • Yea, the are a few cons against using SurfaceView. Whilst you gain the advantages of using a different thread to draw to the canvas, you cannot gain the advantages using Hardware Acceleration. These are topics that have been previously debated, and I suggest for you to check it out. Please check my answer as correct please. :) http://stackoverflow.com/a/15995353/1928275 – James Feb 11 '17 at 19:28
  • 1
    And I suppose that OpenGL ES is best of all solutions? – steliosf Feb 11 '17 at 19:32
  • Indeed it is for spacial and matrix object drawing. Simple games I'd consider using SurfaceView. – James Feb 11 '17 at 19:40