I have a problem where I need to make a ball appear in the middle of the getHeight () / 2 screen, the real problem is in the init () function where the value of getHeight () return me 0, which should return me the height value of the screen of my device consequently the ball appears at the top when I draw it ...
The question is, How do I get the Value of getHeight () before starting the draw () method ??
public class GameView extends View implements Runnable {
private static final int INTERVALO = 10;
private boolean running = true;
private Paint paint;
Ball bola;
public GameView(Context context) {
super(context);
paint = new Paint();
Thread MinhaThread = new Thread(this);
MinhaThread.setPriority(Thread.MIN_PRIORITY);
MinhaThread.start();
init();
}
private void init() {
bola = new Ball(20,getHeight()/2,5,0,0); //Ball(x,y,size,forca,speed) x e y sao as coordenadas para desenhar na tela.
Log.e("daniel","Inicializando "+getHeight()); // <-- aqui me retorna0
}
public void draw(Canvas canvas){
super.draw(canvas);
canvas.drawColor(Color.rgb(100, 190, 230));
paint.setColor(Color.GREEN);
canvas.drawRect(0,getHeight()-25,getWidth(),getHeight(),paint); //desenha o chao
bola.draw(canvas); //AQUI eu desenho meu objeto no topo da tela, deveria ser no meio
bola.gravidade(); //simulo a gravidade... nada de importante aqui!
}
@Override
public void run() {
while(running){
try{
Thread.sleep(INTERVALO);
}catch (Exception e){
Log.e("ERRO", e.getMessage());
}
update();
}
}
private void update() {
//bola.gravidade();
//dispara o metodo draw (p/desenhar a tela)
postInvalidate();
}
public void release(){
running = false;
}
}
PS: I know I could write the getHeight () / 2 method in draw () but I really I need to do this OUTSIDE the draw method . Is there any possibility of doing this? Thank you all.