调整图像大小以适应Android中的屏幕大小和分辨率

时间:2013-10-09 06:37:47

标签: android

我有一个Android应用程序用viewPager显示图像,图像是固定大小的,因此在某些Android设备中根据屏幕大小和屏幕分辨率显示空白边框

有没有办法调整图像大小以适应屏幕? 在项目中放置多个尺寸是不可能的,因为我有超过500个图像。

2 个答案:

答案 0 :(得分:3)

Bitmap scaledBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true);

缩小方法

public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
        boolean filter) {
    float ratio = Math.min(
            (float) maxImageSize / realImage.getWidth(),
            (float) maxImageSize / realImage.getHeight());
    int width = Math.round((float) ratio * realImage.getWidth());
    int height = Math.round((float) ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
            height, filter);
    return newBitmap;
}

答案 1 :(得分:2)

您应该尝试使用不同的scaleTypes。 The documentation can be found here 和(我更喜欢)here is a very nice collection of examples这些scaleTypes的样子。

如果您只是希望延长图像并且不关心丢失宽高比,则可能需要使用scaleType: fitXY