使用 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;
}