drawBitmap is not fullscreen

Gabrielle picture Gabrielle · Oct 20, 2011 · Viewed 7.7k times · Source

I display an image using onDraw Method like this :

public void onDraw(Canvas canvas) {
     super.onDraw(canvas);
     Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.photo0);
     canvas.drawColor(Color.BLACK);
     canvas.drawBitmap(background, 0, 0, null);

I want to set this image as background, but it is displayed only on a part of the screen. How to set it as fullscreen?

There is a way to set the image as background from xml and to draw other images on this image from onDraw Method?

Answer

Pal Szasz picture Pal Szasz · Oct 20, 2011

try this:

...
Rect dest = new Rect(0, 0, getWidth(), getHeight());
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(background, null, dest, paint);

This renders a part of the bitmap ("null" means the whole bitmap) to the screen area specified by dest (which is the whole area of the view in this case).

Note that this will/might change the aspect ratio, depending on the background, you might need to fix that.