Android:在画布上绘制位图时的图像分辨率问题

发布于 2025-01-03 07:34:41 字数 526 浏览 5 评论 0原文

在画布上绘图时,如果我使用此代码来绘制位图,那么它会被拉伸。

代码1:

photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true); // original

但是如果我使用下面的代码来绘制位图,那么位图不会被拉伸,但我只能得到图像左上角的某些部分。

代码2:

photoBitmap = Bitmap.createBitmap(tempBitmap);

现在,上面的两个代码都是用于创建/获取位图。并在画布上绘制位图,我使用下面的代码:

canvas.drawBitmap (photoBitmap,0,  0, null);// Original Without ImageView

现在,我应该做什么才能看到完整图像并且它不应该被拉伸。

谢谢。

On drawing on canvas if i use this code to draw the bitmap then it get stratched.

Code1:

photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true); // original

But if i use below code to draw the bitmap then the Bitmap not getting stratched but i m only getting the some portion of top left corner of the Image.

Code2:

photoBitmap = Bitmap.createBitmap(tempBitmap);

Now, Above both code is for creating/getting the bitmap. and to draw the Bitmap on canvas i use below code:

canvas.drawBitmap (photoBitmap,0,  0, null);// Original Without ImageView

Now, What should i do to saw full image and it should not get stratch.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独留℉清风醉 2025-01-10 07:34:41

听起来您要求做的是在两个方向上均匀拉伸位图,以便它适合显示器。试试这个:

        float xscale = (float)display.getWidth() / (float)tempBitmap.getWidth();
        float yscale = (float)display.getHeight() / (float)tempBitmap.getHeight();
        if (xscale > yscale) // make sure both dimensions fit (use the smaller scale)
           xscale = yscale;
        float newx = (float)tempBitmap.getWidth() * xscale;
        float newy = (float)tempBitmap.getHeight() * xscale; // use the same scale for both dimensions
        // if you want it centered on the display (black borders)
        float borderx = ((float)display.getWidth() - newx) / 2.0;
        float bordery = ((float)display.getHeight() - newy) / 2.0;
        photoBitmap = Bitmap.createScaledBitmap(tempBitmap, newx, newy, true);

        // your drawing code will now look like this
        canvas.drawBitmap (photoBitmap, borderx,  bordery, null); 

It sounds like what you're asking to do is to stretch the bitmap in both directions equally so that it fits on the display. Try this:

        float xscale = (float)display.getWidth() / (float)tempBitmap.getWidth();
        float yscale = (float)display.getHeight() / (float)tempBitmap.getHeight();
        if (xscale > yscale) // make sure both dimensions fit (use the smaller scale)
           xscale = yscale;
        float newx = (float)tempBitmap.getWidth() * xscale;
        float newy = (float)tempBitmap.getHeight() * xscale; // use the same scale for both dimensions
        // if you want it centered on the display (black borders)
        float borderx = ((float)display.getWidth() - newx) / 2.0;
        float bordery = ((float)display.getHeight() - newy) / 2.0;
        photoBitmap = Bitmap.createScaledBitmap(tempBitmap, newx, newy, true);

        // your drawing code will now look like this
        canvas.drawBitmap (photoBitmap, borderx,  bordery, null); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文