如何从现有位图创建更大的位图

发布于 2025-01-04 05:24:37 字数 264 浏览 3 评论 0原文

我有一张位图图像。如何以编程方式创建更大的位图?我尝试创建scaleBitMap,但它没有显示更大。

int newHeight=900;
Bitmap bg1=Bitmap.createScaledBitmap(bg, width, newHeight, false);
canvas.drawBitmap(bg1, null, new Rect(0, 0, getWidth(), newHeight), null);

我该如何解决这个问题?

I have one Bitmap image. How do I create a bigger bitmap programmatically? I tried creating scaleBitMap, but it is not displaying bigger.

int newHeight=900;
Bitmap bg1=Bitmap.createScaledBitmap(bg, width, newHeight, false);
canvas.drawBitmap(bg1, null, new Rect(0, 0, getWidth(), newHeight), null);

How do I fix this problem?

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

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

发布评论

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

评论(4

疯到世界奔溃 2025-01-11 05:24:37

也许你应该尝试这个:

int newHeight=900;
int newWidth= bg.getWidth;
Bitmap bg1=Bitmap.createScaledBitmap(bg, newWidth, newHeight, true);

总是值得一试

Maybe you should try this instead:

int newHeight=900;
int newWidth= bg.getWidth;
Bitmap bg1=Bitmap.createScaledBitmap(bg, newWidth, newHeight, true);

always worth a try

春夜浅 2025-01-11 05:24:37
private Bitmap resizeBitmap(Bitmap bitmap, float scaleHeight, float scaleWidth)
{
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int newWidth = (int)(width * scaleWidth);
    int newHeight = (int)(height * scaleHeight);

    return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
}
private Bitmap resizeBitmap(Bitmap bitmap, float scaleHeight, float scaleWidth)
{
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int newWidth = (int)(width * scaleWidth);
    int newHeight = (int)(height * scaleHeight);

    return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
}
雄赳赳气昂昂 2025-01-11 05:24:37

Bitmap.createScaledBitmap() 正在执行您想要的操作,问题是当您尝试将其绘制到画布上时。 getWidth() 返回什么?将其余代码发布给我们一些背景信息,我们可以找到您的问题。

The Bitmap.createScaledBitmap() is doing what you want, the problem is when you're trying to draw it to the canvas. What is getWidth() returning? Post the rest of the code to give us some context and we can find your problem.

唠甜嗑 2025-01-11 05:24:37

尝试这个代码...

public Bitmap getResizeBitmap(Bitmap bitmap, int newHeight, int newWidth)
        {

            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            float scaleWidth = ((float)newWidth)/width;
            float scaleHeight = ((float)newHeight)/height;

            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap resizeBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

            return resizeBitmap;

        }

然后在 onCreate 方法中使用这个...

myImage = Bitmap.createBitmap(BitmapFactory
                    .decodeResource(this.getResources(), R.drawable.mypic));

            myImage.setDensity(1);

            imageview = (ImageView)findViewById(R.id.imageViewId);
            imageview.setImageBitmap(getResizeBitmap(myImage,195, 480));//as per as your requirement

Try this code...

public Bitmap getResizeBitmap(Bitmap bitmap, int newHeight, int newWidth)
        {

            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            float scaleWidth = ((float)newWidth)/width;
            float scaleHeight = ((float)newHeight)/height;

            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap resizeBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

            return resizeBitmap;

        }

then in onCreate method use this...

myImage = Bitmap.createBitmap(BitmapFactory
                    .decodeResource(this.getResources(), R.drawable.mypic));

            myImage.setDensity(1);

            imageview = (ImageView)findViewById(R.id.imageViewId);
            imageview.setImageBitmap(getResizeBitmap(myImage,195, 480));//as per as your requirement
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文