簡體   English   中英

如何在整個屏幕上的畫布上繪制位圖?

[英]How to draw a bitmap on a canvas on the whole screen?

在我的應用程序中,我需要在整個屏幕上繪制一個位圖。 由於某些原因,當我繪制位圖時,只有一部分位圖正在加載在屏幕的一部分上。 換句話說,不是整個圖片都顯示在屏幕上。 這是我繪制位圖的一部分代碼:

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);
        }
    }
}

為此,您需要獲取設備的寬度和高度,然后將圖像縮放到該尺寸並將其顯示在畫布上。

這是您的代碼的修改。

//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();

現在它將填滿整個屏幕,確保將畫布應用到整個屏幕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM