如何在整个屏幕上的画布上绘制位图?

时间:2015-02-23 12:14:10

标签: java android canvas bitmap

在我的应用程序中,我需要在整个屏幕上绘制一个位图。出于某种原因,当我绘制位图时,只有部分位图正在加载到屏幕的一部分上。换句话说,并非整个画面显示在屏幕上。这是我绘制Bitmap时的一段代码:

byte[]byteArray=getIntent().getByteArrayExtra("image");
Bitmap tmp=BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);

operation = Bitmap.createBitmap(tmp.getWidth(), tmp.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(operation);
Paint paint = new Paint();
tmp.setDensity(c.getDensity());

c.drawBitmap(tmp, 0f, 0f, paint);
tmp.recycle();

private void drawOverlays() {
    Canvas c = null;
    try {
        c = holder.lockCanvas(null);
        synchronized (holder) {
            if (c != null)
                c.drawBitmap(operation, 0, 0, null); 
        }
    } catch (Exception e) {
        Log.e("SurfaceView", "Error drawing frame", e);
    } finally {
        // do this in a finally so that if an exception is thrown
        // during the above, we don't leave the Surface in an
        // inconsistent state
        if (c != null) {
            holder.unlockCanvasAndPost(c);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

为此,您需要获得设备的宽度和高度,然后将图像缩放到该尺寸并在画布上显示。

以下是对代码的修改。

//add this code before your code
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

//your code with modification
byte[]byteArray=getIntent().getByteArrayExtra("image");
Bitmap tmp=BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
operation = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(operation);
Paint paint = new Paint();
tmp.setDensity(c.getDensity());
c.drawBitmap(tmp, 0f, 0f, paint);
tmp.recycle();

现在它将填满整个屏幕,确保您将画布应用到整个屏幕。