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
How Do We Distinguish Never-Asked from Stop-Asking in Android M's Runtime Permissions
Asynctask: Where Does the Return Value of Doinbackground() Go
Mediastore - Uri to Query All Types of Files (Media and Non-Media)
Android Hello, Gallery Tutorial -- "R.Styleable Cannot Be Resolved"
Android Studio 4.1.1 MACos Full Screen Error
How to Make an Intent with Multiple Actions
How to Close All the Activities of My Application
How to Pause Flash Content in an Android Webview When My Activity Isn't Visible
Android Httpclient Hangs on Second Request to the Server (Connection Timed Out)
Cannot Lower Case Button Text in Android Studio
How to Show Snackbar at Top of the Screen
Official "Boost Library" Support for Android and iOS
Android Location Manager, Get Gps Location ,If No Gps Then Get to Network Provider Location
Ontouchevent Not Working on Child Views
How to Display Long Messages in Logcat