【问题标题】:How to draw a bitmap on a canvas on the whole screen?如何在整个屏幕上的画布上绘制位图?
【发布时间】:2015-02-23 12:14:09
【问题描述】:

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

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

【问题讨论】:

    标签: java android canvas bitmap


    【解决方案1】:

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

    这是你的代码的修改。

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

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

    【讨论】:

    • 我知道已经有一段时间了,但我确实需要解决我遇到的问题。由于某种原因,当我发送位图操作并显示它时,它只显示图片的左上角。
    • 你试过这个代码吗?您在 xml 文件中为画布提供了什么宽度和高度参数?
    • 我在我的代码中发现了问题,我不得不将 tmp 设置为相同的大小,我唯一的问题是我的图片的底部被切断了(在我的 Moto G2 上)
    • 请评论此行tmp.setDensity(c.getDensity());,然后重试。
    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多