drawBitmap不是全屏

Gab*_*lle 3 android image fullscreen ondraw

我使用onDraw方法显示图像,如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

我想将此图像设置为背景,但它仅显示在屏幕的一部分上.如何将其设置为全屏?

有一种方法可以将图像设置为xml的背景,并从onDraw方法中在此图像上绘制其他图像?

Pal*_*asz 15

试试这个:

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

这将位图的一部分("null"表示整个位图)呈现给dest指定的屏幕区域(在这种情况下是视图的整个区域).

请注意,这可能/可能会更改宽高比,具体取决于背景,您可能需要修复它.