Drawing in the middle of the screen with the android canvas

1

How to draw in the middle of the screen, in all the attempts I made I was able to just stretch the drawing and not move it to the center of the screen.

public class Carta {

private static int ALTURA_DA_CARTA= 200;
private static int LARGURA_DA_CARTA=90;
private Tela tela;
private int posicao;
private int alturaDaCarta;
private static final int DISTANCIA_PRIMEIRA_CARTA=90;
private static final Paint VERDE= Cores.corDaCarta();


public Porta(Tela tela, int posicao){
    this.tela=tela;
    this.posicao=posicao-DISTANCIA_PRIMEIRA_CARTA;
    this.alturaDaPorta=(tela.getAltura()-ALTURA_DA_CARTA)+MEIO_DA_TELA;

}

public void desenhaNo(Canvas canvas){ desenhaCarta(canvas);}



private void desenhaCarta(Canvas canvas){
   canvas.drawRect(this.posicao,this.alturaDaCarta,this.posicao+LARGURA_DA_CARTA,this.tela.getAltura(),VERDE);
}

}

public class Cartas {
    List<Carta> cartas = new ArrayList<Carta>();
    private Tela tela;
    private static final int QUANTIDADE_DE_CARTAS=3;
    private static final int DISTANCIA_ENTRE_CARTAS=150;

    public Cartas(Tela tela){
        int posicao=-10;

        for(int i=0; i<QUANTIDADE_DE_CARTAS;i++){
            posicao+=DISTANCIA_ENTRE_CARTAS;
            this.cartas.add(new Carta(tela,posicao));
        }

    }

    public void desenhaNo(Canvas canvas){
        for(Carta carta: this.cartas){
            carta.desenhaNo(canvas);
        }
    }
}



public class Game extends SurfaceView implements  Runnable, View.OnTouchListener {

private Bitmap background;
private  Tela tela;
private Cartas cartas;
private final SurfaceHolder holder= getHolder();
private boolean estarodando=true;


public void cancela(){
    this.estarodando=false;
}

public void inicia(){
    this.estarodando=true;
}
private void inicializaElementos(){
    Bitmap back = BitmapFactory.decodeResource(getResources(),
            R.drawable.fundobranco);
    this.background = Bitmap.createScaledBitmap(back,
            back.getWidth(), this.tela.getAltura(), false);
    this.cartas= new Cartas(this.tela);

}
public Game(Context context){
    super(context);
    this.tela=new Tela(context);
    inicializaElementos();
}


@Override
public void run() {
    while(this.estarodando){
        if(!this.holder.getSurface().isValid()) continue;
        Canvas canvas= getHolder().lockCanvas();
        canvas.drawBitmap(this.background,0,0,null);
        this.cartas.desenhaNo(canvas);
        this.getHolder().unlockCanvasAndPost(canvas);

    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return false;
}

}

    
asked by anonymous 12.11.2018 / 00:34

0 answers