简体   繁体   中英

How to create a bigger bitmap from an existing bitmap

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?

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM