【问题标题】:Stretch image in ImageView在 ImageView 中拉伸图像
【发布时间】:2013-10-18 13:31:46
【问题描述】:

我有一个 ImageView,它的高度和宽度设置为“fill_parent”,而相对布局设置了相同的值。Set Image scaleType="fitXY"

这里是 XML 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY"
    android:src="@drawable/background_image" />


</RelativeLayout>

适合宽度和高度,但图像被拉伸。

【问题讨论】:

标签: android imageview


【解决方案1】:

这就是 fitXY 应该做的。如果您不想裁剪图像,可以尝试使用 centerInside,如果您想填充所有空间(并裁剪图像),可以尝试使用 centerCrop .

Here 是一个很好的列表,其中包含示例以更好地理解所有scaleTypes

【讨论】:

    【解决方案2】:

    这两个属性为我解决了问题:

    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    

    【讨论】:

      【解决方案3】:
      package utils;
      
      import android.content.Context;
      import android.graphics.drawable.Drawable;
      import android.util.AttributeSet;
      import android.widget.ImageView;
      
      /**
       * Created by Navneet Boghani on 7/7/16.
       */
      public class ProportionalImageView extends ImageView {
      
          public ProportionalImageView(Context context) {
              super(context);
          }
      
          public ProportionalImageView(Context context, AttributeSet attrs) {
              super(context, attrs);
          }
      
          public ProportionalImageView(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
          }
      
          @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
              Drawable d = getDrawable();
              if (d != null) {
               //   int w = MeasureSpec.getSize(widthMeasureSpec);
                //  int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
                  int h = MeasureSpec.getSize(heightMeasureSpec);
                  int w = h * d.getIntrinsicWidth() / d.getIntrinsicHeight();
                  setMeasuredDimension(w, h);
              }
              else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
          }
      }
      

      【讨论】:

      • 一点解释??
      【解决方案4】:

      这就是 fitXY 要做的。它不会尊重图像的纵横比并将图像填充到您的 imageview 而不保持图像的原始纵横比。如果您想将图像的大小与您的视图相匹配,您可以自己缩放图像,或者您可以查看 glide 库为您执行此操作。对于 android 中的图像,Glide 是一个非常有用的库

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-07
        相关资源
        最近更新 更多