更大的宽度图像到缩略图

时间:2016-03-25 06:04:11

标签: android imageview

enter image description here网址宽度为900 * 346的图片,我们使用相同的图片显示细节图像和缩略图。这些图像在Gridview中显示,有2列和200dp高度,但图像正在拉伸。有什么办法显示较大宽度的图像而不拉伸。提前谢谢。

GridView Xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView
    android:id="@+id/grid_img"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:horizontalSpacing="@dimen/portfolio_grid_space"
    android:verticalSpacing="@dimen/portfolio_grid_space"
    android:layout_marginTop="@dimen/portfolio_grid_space"
    android:layout_marginBottom="@dimen/portfolio_grid_space"
    android:numColumns="2"/>

Gridview Adapter xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/img"
    android:scaleType="fitCenter"
    android:layout_width="match_parent" android:layout_height="200dp">

</ImageView>

2 个答案:

答案 0 :(得分:0)

如果我没有误解,你想在拉伸图像的同时保持图像的宽高比。您可以在xml ImageView中添加以下行:

android:adjustViewBounds="true"
android:scaleType="centerCrop"

您可以使用scaleType来获取所需的图像。 如果您使用的是API 17以下的设备,则可以自定义ImageView

public class ScaleAspectFillImageView extends ImageView {

public ScaleAspectFillImageView(Context context) {
    super(context);
}

public ScaleAspectFillImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ScaleAspectFillImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // If API level <= 17,
    // Only need custom measurement for image whose size is smaller than imageView size.
    // http://developer.android.com/reference/android/widget/ImageView.html#setAdjustViewBounds(boolean)
    if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        float viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        float viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        // Calculate imageView.height based on imageView.width and drawable aspect ratio using scale-aspect-fill mode.
        Drawable drawable = getDrawable();
        if (drawable != null) {
            float drawableWidth = drawable.getIntrinsicWidth();
            float drawableHeight = drawable.getIntrinsicHeight();
            // Only need custom measurement for image whose size is smaller than imageView size.
            // Avoid divide by zero error.
            boolean needCustomMeasurement = drawableWidth < viewWidth || drawableHeight < viewHeight;
            if (needCustomMeasurement && drawableHeight > 0) {
                float drawableAspectRatio = drawableWidth / drawableHeight;
                // imageView.width and drawable aspect ratio must be greater than zero.
                if (drawableAspectRatio > 0 && viewWidth > 0) {
                    float targetHeight = viewWidth / drawableAspectRatio;
                    setMeasuredDimension((int) viewWidth, (int) targetHeight);
                    return; // Already set the measured dimension.
                }
            }
        }
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

答案 1 :(得分:0)

在布局android:scaleType="fitXY"的xml文件中输入 P.S:这适用于使用android:src="..."而不是android:background="..."设置图像时,默认情况下将背景设置为拉伸并适合视图。