I’m simply not getting this to work.
I saw that this network isn’t for just simple code correcting posts.
But I think this is in the interest of others to because there werent any good tutorials.
So I’m calling a canvas and I can draw on it in the ondraw() method but when I try to draw out of the game loop it draws nothing. I even don’t get an error messasge.
App :
public class App extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw d = new draw(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(d);
}
public void end()
{
App.this.finish();
}
}
draw.java :
public class draw extends View {
Canvas ca;
View v;
Paint paint;
int width;
int height;
static final int MAX_GAME_SPEED=33;
static int fps;
boolean running=true;
int pw=0,ph=0;
public draw(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas c){
super.onDraw(c);
paint = new Paint(); //Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
//get screen size
WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
width = display.getWidth(); // deprecated
height = display.getHeight(); // deprecated
// make the entire canvas white
paint.setColor(Color.WHITE);
c.drawPaint(paint);
ca = c;
paint.setColor(Color.BLACK);
c.drawRect(Math.round(width/3),Math.round(height/3),Math.round((width/3)*2),Math.round((height/3)*2), paint); //position width, position height,width,height
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(30);
paint.setColor(Color.GREEN);
c.drawText(String.valueOf(width)+"x"+String.valueOf(height)+","+Math.round(width/3)+"x"+Math.round(height/3), 30, 200, paint);
paint.setColor(Color.GREEN);
c.drawText(String.valueOf(width)+"x"+String.valueOf(height)+","+Math.round(width/3)+"x"+Math.round(height/3), 30, 200, paint);
Thread myThread = new Thread(new UpdateThread());
myThread.start();
}
public void paint(Canvas c)
{
paint.setColor(Color.GREEN);
c.drawRect(20, 5, 50, 100, paint);
pw++;
ph++;
/*if (pw >= width || ph >= height)
{
pw=0;
ph=0;
}
*/
}
public Handler updateHandler = new Handler(){
/** Gets called on every message that is received */
// @Override
public void handleMessage(Message msg) {
paint(ca);
super.handleMessage(msg);
}
};
public class UpdateThread implements Runnable {
@Override
public void run() {
while(true){
draw.this.updateHandler.sendEmptyMessage(0);
}
}
}
}
Firstly:
You are sending update messages too often. I belive you are overloading your message queue with that.
Secondly:
You are using canvas instance saved at onDraw method. I think this is not right.
Instead, you can change your UpdateThread.run() method to
This will send message to invalidate and repaint your View and thus call to your View.onDraw(Canvas) method with proper Canvas.
Also:
If you want to render your View as much as possible you must to implement next sequence:
and instead of scheduling thread (UpdateThread) you need to just send first MSG_REDRAW message.