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

时间:2012-02-10 05:25:55

标签: android image bitmap

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

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

如何解决此问题?

4 个答案:

答案 0 :(得分:2)

也许你应该试试这个:

int newHeight=900;
int newWidth= bg.getWidth;
Bitmap bg1=Bitmap.createScaledBitmap(bg, newWidth, newHeight, true);
总是值得一试

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

试试这段代码......

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