猿问

缩放图像以填充ImageView宽度并保持宽高比

我有一个GridView。的数据GridView是来自服务器的请求。


这是中的项目布局GridView:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:background="@drawable/analysis_micon_bg"

    android:gravity="center_horizontal"

    android:orientation="vertical"

    android:paddingBottom="@dimen/half_activity_vertical_margin"

    android:paddingLeft="@dimen/half_activity_horizontal_margin"

    android:paddingRight="@dimen/half_activity_horizontal_margin"

    android:paddingTop="@dimen/half_activity_vertical_margin" >


    <ImageView

        android:id="@+id/ranking_prod_pic"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:adjustViewBounds="true"

        android:contentDescription="@string/app_name"

        android:scaleType="centerCrop" />


    <TextView

        android:id="@+id/ranking_rank_num"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


    <TextView

        android:id="@+id/ranking_prod_num"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />


    <TextView

        android:id="@+id/ranking_prod_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>

我从服务器请求数据,获取图像网址并将图像加载到 Bitmap


public static Bitmap loadBitmapFromInputStream(InputStream is) {

    return BitmapFactory.decodeStream(is);

}


public static Bitmap loadBitmapFromHttpUrl(String url) {

    try {

        return loadBitmapFromInputStream((InputStream) (new URL(url).getContent()));

    } catch (Exception e) {

        Log.e(TAG, e.getMessage());

        return null;

    }

}

而且getView(int position, View convertView, ViewGroup parent)适配器中有方法的代码


Bitmap bitmap = BitmapUtil.loadBitmapFromHttpUrl(product.getHttpUrl());

prodImg.setImageBitmap(bitmap);

图片大小为210*210。我在Nexus 4上运行我的应用程序。图像可以填充ImageView宽度,但ImageView高度不能缩放。ImageView不显示整个图像。


我该如何解决这个问题?


跃然一笑
浏览 832回答 3
3回答

RISEBY

不使用任何自定义类或库:<ImageView&nbsp; &nbsp; android:id="@id/img"&nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:adjustViewBounds="true"&nbsp; &nbsp; android:scaleType="fitCenter" />scaleType="fitCenter" (省略时为默认)将使其宽度达到父级允许的范围,并根据需要向上/向下缩放以保持宽高比。scaleType="centerInside"如果的固有宽度src小于父级宽度,则会使图像水平居中如果的固有宽度src大于父级宽度,则会使其达到父级允许的宽度,并缩小比例并保持宽高比。不管您使用android:src还是ImageView.setImage*方法,密钥都可能是adjustViewBounds。

白衣非少年

我曾经有过类似的问题。我通过制作自定义ImageView解决了它。public class CustomImageView extends ImageView然后覆盖imageview的onMeasure方法。我相信我做了这样的事情:&nbsp; &nbsp; @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Drawable drawable = getDrawable();&nbsp; &nbsp; &nbsp; &nbsp; if (drawable == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMeasuredDimension(0, 0);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float imageSideRatio = (float)drawable.getIntrinsicWidth() / (float)drawable.getIntrinsicHeight();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float viewSideRatio = (float)MeasureSpec.getSize(widthMeasureSpec) / (float)MeasureSpec.getSize(heightMeasureSpec);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (imageSideRatio >= viewSideRatio) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Image is wider than the display (ratio)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = MeasureSpec.getSize(widthMeasureSpec);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = (int)(width / imageSideRatio);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMeasuredDimension(width, height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Image is taller than the display (ratio)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int height = MeasureSpec.getSize(heightMeasureSpec);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width = (int)(height * imageSideRatio);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMeasuredDimension(width, height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; super.onMeasure(widthMeasureSpec, heightMeasureSpec);&nbsp; &nbsp; }这将拉伸图像以适合屏幕,同时保持宽高比。
随时随地看视频慕课网APP

相关分类

Android
我要回答