Extended Surfaceview's Ondraw() Method Never Called

Extended SurfaceView's onDraw() method never called

Found it on the android-developers Google group. You simply have to add :

setWillNotDraw(false)

To the constructor. Now if someone could explain me why, that would be greatly appreciated.

Android nothing being drawn when onDraw is called?

Try this:

Change your method:

    @Override
protected void onDraw(Canvas canvas) {
canvas.drawRGB(255, 255, 0);
}

By this:

    @Override
protected void onDraw(Canvas canvas) {
canvas.drawRGB(255, 255, 0);
super.onDraw(canvas);
}

Also, try change this lines:

    //do the game here
canvas = null;
canvas = surfaceHolder.lockCanvas();
gameSurface.onDraw(canvas);
surfaceHolder.unlockCanvasAndPost(canvas);

By this ones:

    Canvas canvas = null;

try {
canvas = surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
gameSurface.onDraw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null)
surfaceHolder.unlockCanvasAndPost(canvas);
}

Also keep note that the while loop it's running really fast no giving chance to draw the canvas. The thread sleep isn't working very well. Look at this resources, there is explained how do the game loop properly. And if you wanna test it right now, just erase the while.

Android game loop explained
Android game basic structure

Hope this helps :]

Android: SurfaceView ignoring postInvalidate()?

You bloody legend Karan!

That pointed me in the right way.
I found a few posts that mentioned it had to be done in the surfaceCreated() method rather than constructor and it got the postInvalidate() working.

However, enabling that would disable any drawing done by my custom DrawThread.
Good thing is it should be easy to find a nice point in time to slot in that call.

Thanks again, View.setWillNotDraw() did the trick.

Android SurfaceView draws only twice

Try to extend your own class from View like this, and invalidate in onDraw() method:

public class ScreenView extends View {

private Context context;
private Paint paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);

public ViewStats(Context context) {
super(context);
this.context = context;
init();
}

public ViewStats(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}

public ViewStats(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init();
}

private void init() {
paintLine.setStyle(Paint.Style.FILL_AND_STROKE);
paintLine.setColor(0xFFE4455B);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
}

@Override
protected void onDraw(Canvas canvas) {
Random random = new Random();
canvas.drawRGB(random.nextInt(255), random.nextInt(255), random.nextInt(255));
invalidate();
}
}


Related Topics



Leave a reply



Submit