2
I have a problem where I need to make a ball appear in the middle of the screen getHeight()/2, the real problem is in the function init() where the value of getHeight() return me 0, which should return me the value of the screen height of my device therefore the ball appears at the top when I draw it...
The question is, How to get getHeight() Value before starting draw() ??
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 to getHeight()/2 within the facility draw() but i really need to do this OUT of the draw method. Is there any possibility of doing this? Thank you all.
Using the second method I put lagura = getWidth(); within the onMeasure() right after the comment and tried to print her inside the init() with the log Log. d("Getwidth()",""+lagura); but still printing 0
– Dannark
ah, now it’s working, but it seems it takes a while before this method is called ;)
– Dannark