0

使用 800*480 创建背景资源。如果另一张照片的宽度 <= 高度。无论这张照片的像素是更大还是更大!总是让这张照片适合背景资源,照片必须在背景资源中移动!我怎么能得到?谢谢

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;

}
4

1 回答 1

0

只需使用此代码,您可能会得到答案...

   canvas.drawBitmap(Bitmap.createScaledBitmap(origin, 480, 800, true), 0,0, new Paint(Paint.DITHER_FLAG
        | Paint.FILTER_BITMAP_FLAG));
于 2013-11-01T04:17:07.173 回答