Vishwas Shukla
Vishwas Shukla

Reputation: 29

Image on Canvas not showing

I am fairly new to Android, but why isn't my image showing on the canvas? I know that it is working properly because the background color is black, which I changed in the same same method, onDraw . Can anyone help me? Thanks in advance!

  public PongView(Context context) {
    super(context);
    paddle1 = BitmapFactory.decodeResource(getResources(), R.drawable.pongpaddle);
    paddle2 = BitmapFactory.decodeResource(getResources(), R.drawable.pongpaddle);
}
protected void onDraw(Canvas canvas) {
    xp1 = canvas.getWidth()/2;
    xp2 = canvas.getWidth()/2;
    yp1 = 25;
    yp2 = 760;
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(paddle1, xp1,yp1, null);
    canvas.drawBitmap(paddle2,xp2,yp2, null);
    Paint white = new Paint();
    white.setColor(Color.WHITE);
    canvas.drawText("Score P1:"+ p1Score +" P2: " + p2Score , 700, 20,white );  
}

Upvotes: 0

Views: 1867

Answers (1)

sanjeev mk
sanjeev mk

Reputation: 4336

Based on your comment above, I think what's happening is, android run time is drawing to the canvas, and your onDraw is not being called. You can avoid this by calling this.setWillNotDraw(false) in your class' constructor. Once you clear this flag, your onDraw() will be called.

Source: Android developer docs says if you override View's onDraw(), you have to clear this flag. Check setWillNotDraw

Upvotes: 2

Related Questions