为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

跃然一笑 2019-11-05 14:51:40
我有一个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>我从服务器请求数据,获取图像网址并将图像加载到 Bitmappublic 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不显示整个图像。我该如何解决这个问题?
查看完整描述

3 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

不使用任何自定义类或库:


<ImageView

    android:id="@id/img"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:adjustViewBounds="true"

    android:scaleType="fitCenter" />

scaleType="fitCenter" (省略时为默认)


将使其宽度达到父级允许的范围,并根据需要向上/向下缩放以保持宽高比。

scaleType="centerInside"


如果的固有宽度src小于父级宽度,

则会使图像水平居中

如果的固有宽度src大于父级宽度,

则会使其达到父级允许的宽度,并缩小比例并保持宽高比。

不管您使用android:src还是ImageView.setImage*方法,密钥都可能是adjustViewBounds。


查看完整回答
反对 回复 2019-11-05
?
白衣非少年

TA贡献1155条经验 获得超0个赞

我曾经有过类似的问题。我通过制作自定义ImageView解决了它。


public class CustomImageView extends ImageView

然后覆盖imageview的onMeasure方法。我相信我做了这样的事情:


    @Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    try {

        Drawable drawable = getDrawable();


        if (drawable == null) {

            setMeasuredDimension(0, 0);

        } else {

            float imageSideRatio = (float)drawable.getIntrinsicWidth() / (float)drawable.getIntrinsicHeight();

            float viewSideRatio = (float)MeasureSpec.getSize(widthMeasureSpec) / (float)MeasureSpec.getSize(heightMeasureSpec);

            if (imageSideRatio >= viewSideRatio) {

                // Image is wider than the display (ratio)

                int width = MeasureSpec.getSize(widthMeasureSpec);

                int height = (int)(width / imageSideRatio);

                setMeasuredDimension(width, height);

            } else {

                // Image is taller than the display (ratio)

                int height = MeasureSpec.getSize(heightMeasureSpec);

                int width = (int)(height * imageSideRatio);

                setMeasuredDimension(width, height);

            }

        }

    } catch (Exception e) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

这将拉伸图像以适合屏幕,同时保持宽高比。


查看完整回答
反对 回复 2019-11-05
  • 3 回答
  • 0 关注
  • 831 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信