缩小图像并完全显示

时间:2013-08-20 07:58:43

标签: android image imageview

我需要缩小图片的尺寸(+ - 2500px一面),以便在我的应用中显示在我的设备上。但我有一个问题。如果我使用我的代码缩小图像(稍微向下),则显示图像但是侧面被切断。使用我的代码,图像的高度非常完美。 我用这段代码缩小了我的图像:

static public Bitmap scaleToFill(Bitmap b, int width, int height) {
    float factorH = height / (float) b.getWidth();
    float factorW = width / (float) b.getWidth();
    float factorToUse = (factorH > factorW) ? factorW : factorH;
    return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorToUse), (int) (b.getHeight() * factorToUse), false);
}

我得到的高度和宽度:

        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

使用ScaleType CENTER_CROP在ImageView中查看图像。我希望有人可以帮助我!提前谢谢!

2 个答案:

答案 0 :(得分:0)

在布局文件中 - 内部图像视图

<ImageView
 android:scaleType="fitXY"
/>

try with thsi code.

答案 1 :(得分:0)

这个问题比你想象的要普遍得多。它与降尺度或安卓没有任何关系。

如果您希望保持纵横比,则必须选择空格或仅显示图像的一部分。

同样的问题出现在电视上,屏幕的宽高比与发送给它的宽高比不同。

还有一种实验方法来调整图像大小(seam carving),但它并不总是很好用,并且取决于图像本身。

顺便说一下,缩放的方式是非常低效的,因为它使用大量内存(首先解码原始位图,然后创建一个新的原始位图的缩小版本)。 你应该尝试google's waymy way