java.lang.IllegalArgumentException:x width必须是< = bitmap.width()

时间:2013-11-01 03:54:16

标签: android bitmap

创建800 * 480的背景资源。如果另一张照片的宽度< = height.whatever这张照片的像素越大或越大!总是让这张照片适合背景资源,照片必须在背景资源中移动!我怎么能得到它?谢谢

private Bitmap scale(Bitmap origin) {
    Bitmap bitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(
            getResources(), R.drawable.example_bg), 0, 0, 800, 480);
    Canvas canvas = new Canvas(bitmap);
    Rect target = null;
    int orgWidth = origin.getWidth();
    int orgHeight = origin.getHeight();
    int bgHeight = bitmap.getHeight();
    int bgWidth = bitmap.getWidth();
    if (orgWidth * bgHeight > orgHeight * bgWidth) {
        int newWidth = bgWidth, newHeight = newWidth * orgHeight / orgWidth;
        target = new Rect(0, (bgHeight - newHeight) / 2, newWidth,
                (bgHeight + newHeight) / 2);
    } else {
        int newHeight = bgHeight, newWidth = newHeight * orgWidth
                / orgHeight;
        target = new Rect((bgWidth - newWidth) / 2, 0,
                (bgWidth + newWidth) / 2, newHeight);
    }
    canvas.drawBitmap(origin, null, target, new Paint(Paint.DITHER_FLAG
            | Paint.FILTER_BITMAP_FLAG));
    return bitmap;

}

1 个答案:

答案 0 :(得分:0)

只需使用此代码,您就可以得到答案......

   canvas.drawBitmap(Bitmap.createScaledBitmap(origin, 480, 800, true), 0,0, new Paint(Paint.DITHER_FLAG
        | Paint.FILTER_BITMAP_FLAG));