-1

我有一个图像视图,稍后将包含一些随机大小的图像我想用图像填充图像视图但保持比例(所以不要使用比例类型 FITXY 或 centercrop,因为它会修剪我的图像的一部分)

我怎样才能做到这一点 ?

在这个测试代码中,我的图像左右两边都有空格

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"


     >

   <ImageView
        android:id="@+id/imgTest"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"

        android:src="@drawable/test"
      />

4

2 回答 2

0

用矩阵和屏幕的高度/宽度解决

private Bitmap resize(Bitmap bm, int w, int h)
    {
        int width = bm.getWidth();
        int height = bm.getHeight();
        int newWidth = w;
        int newHeight = h;
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

        return resizedBitmap;
    }
于 2013-01-06T22:46:11.077 回答
0

试试这个:

<ImageView
    android:id="@+id/imgTest"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:paddingRight="20dp"
    android:scaleType="fitStart"
    android:src="@drawable/test"/>
于 2013-02-27T14:19:54.050 回答