【问题标题】:Imageview not scaling pictureImageview不缩放图片
【发布时间】:2017-09-30 18:37:21
【问题描述】:

我有以下图像视图,其图像小于包含此 ImageView 的 RelativeLayout。我希望图像缩放以适合 imageView 的宽度,但我无法使其正常工作。例如我 有这个 350dp 宽度的 relativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_button_grey"
    android:clickable="true"
    android:padding="5dp">

    <ImageView
        android:id="@+id/image_selector"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:background="@color/black"
        android:src="@drawable/Nachos"
         />

    <TextView
        android:id="@+id/image_selector_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/image_selector"
        android:gravity="center_horizontal|center_vertical"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"

        android:textSize="18sp" />


</RelativeLayout>

正如您在图片中看到的那样,图像位于中心。 fitXY 的问题是宽度有效,但不改变高度。我想要类似的东西,fitX。我也试过 centerInside,但看起来一样。

【问题讨论】:

    标签: android android-layout imageview


    【解决方案1】:

    我也会使用centerCrop或类似的。如果这对您不起作用,请检查此图像,看看您是否能找到任何有趣的东西:

    如果这不适合您,请考虑查看 Android 中的图像自定义: https://developer.android.com/guide/topics/graphics/2d-graphics.html

    【讨论】:

      【解决方案2】:

      因为您的相对布局和 Imageview 都具有包装内容高度,所以您必须使它们匹配父级并使 scaletype fitXY..

      如下代码:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="350dp"
          android:layout_height="match_parent"
          android:background="@drawable/selector_button_grey"
          android:clickable="true"
          android:padding="5dp">
      
      <ImageView
          android:id="@+id/image_selector"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:adjustViewBounds="true"
          android:scaleType="fitXY"
          android:background="@color/black"
          android:src="@drawable/Nachos"
           />
      
      <TextView
          android:id="@+id/image_selector_text"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentStart="true"
          android:layout_below="@+id/image_selector"
          android:gravity="center_horizontal|center_vertical"
          android:paddingBottom="5dp"
          android:paddingLeft="5dp"
          android:textSize="18sp" />
      </RelativeLayout>
      

      【讨论】:

        【解决方案3】:

        您应该将 android:scaleType 属性更改为您的 ImageView。

        如果要按比例显示图片,可以使用centerCrop

        如果不想按比例显示图片,可以使用fitXY

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="350dp"
                        android:layout_height="wrap_content"
                        android:background="@drawable/selector_button_grey"
                        android:clickable="true"
                        android:padding="5dp">
        
            <ImageView
                android:id="@+id/image_selector"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:background="@color/black"
                android:scaleType="fitXY"
                android:src="@drawable/Nachos" />
        
            <TextView
                android:id="@+id/image_selector_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/image_selector"
                android:gravity="center_horizontal|center_vertical"
                android:paddingBottom="5dp"
                android:paddingLeft="5dp"
                android:textSize="18sp"/>
        
        </RelativeLayout>
        

        【讨论】:

        • 但是 centerCrop 会裁剪图像。我希望图像更大,以适应相对布局的宽度。
        • 如果你完全使用你的宽度和高度,你需要使用fitXYcenterCrop
        • centerCrop也被缩放
        【解决方案4】:

        希望这会有所帮助! 就我而言,它工作正常。

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="350dp"
            android:layout_height="match_parent"
            android:background="@drawable/Nachos">
        </RelativeLayout>
        

        【讨论】:

          【解决方案5】:

          RelativeLayout 上设置了一些填充,它们也可能是导致ImageView 不接触布局中的侧面RelativeLayout 的原因:

          删除这个:

          android:padding="5dp"
          

          如果你真的想在顶部和底部设置一些填充,但NOT在右侧和左侧,那么如果你想要填充顶部和底部添加这些:

          android:paddingTop="5dp"
          android:paddingBottom="5dp"
          

          如果您根本不想要填充,请不要添加它们!

          【讨论】:

            【解决方案6】:

            我使用了另一种方法。首先,我在没有从内存中加载的情况下获得了尺寸:

               BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                            bitmapOptions.inJustDecodeBounds = true;
                            try {
                                BitmapFactory.decodeStream(new FileInputStream(f), null, bitmapOptions);
                                BitmapFactory.decodeFile(f.getName(), bitmapOptions);
                                float scale = (bitmapOptions.outHeight * 1f) / (bitmapOptions.outWidth);
                                cache.put(image, value);
                            } catch (Throwable e) {
                                e.printStackTrace();
                            }
            

            然后使用该值 scale

                imageView.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(IMAGE_SIZE, Math.round(IMAGE_SIZE * imageData.getScale())));
                imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                imageView.setAdjustViewBounds(true);
                imageView.setCropToPadding(true);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-06-18
              • 1970-01-01
              • 1970-01-01
              • 2015-02-27
              • 1970-01-01
              • 2015-06-12
              相关资源
              最近更新 更多