0

我有一个比屏幕尺寸大的位图图像。我想以像素为单位将图像缩小到屏幕尺寸?我怎样才能做到这一点 ?我想缩放它保持纵横比而不仅仅是调整它的大小?

亲切的问候

4

2 回答 2

0

使用此函数,参数为 screenWidth 和 screenHeight

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();

// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger 
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);

// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;

// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;

// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);

return dest;
}

有关如何获取屏幕宽度和高度的信息,请参阅

于 2013-08-16T07:01:10.343 回答
0

获取屏幕大小并将这些参数传递给以下函数:

Bitmap.createScaledBitmap(originalimage, width, height, true);
于 2013-08-16T07:08:04.010 回答