【问题标题】:ImageView without scaling不缩放的 ImageView
【发布时间】:2013-05-09 10:26:31
【问题描述】:

我有一个显示行的 ListView

这是xml:

 ...
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:orientation="vertical" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".30">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/face"
                android:scaleType="centerCrop" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".70"
            android:background="@color/white"
            android:padding="4dp" >

            // Other views...

        </LinearLayout>
    </LinearLayout>
    …

并得到这个结果:

这是原图

需要的是没有缩放的图片,就像这样:

我用的是android:ScaleType = "centerCrop" 我用android测试过:adjustViewBounds = "true" 和许多其他参数,但从来没有成功

想法?

提前致谢

【问题讨论】:

    标签: android imageview scaling


    【解决方案1】:

    将您的图像设置为android:src 而不是android:background

    <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:src="@drawable/face"
                    android:scaleType="centerCrop" />
    

    所有视图都可以拍摄背景图片

    srcImageView 具有附加功能:

    • 不同的scaling types
    • adjustViewBounds 用于设置边界以匹配图像尺寸
    • 一些转换,例如 alpha 设置

    您可以在the docs 中找到更多内容。

    【讨论】:

      【解决方案2】:

      android:background="@drawable/face" 更改为android:src="@drawable/face" 比例类型仅适用于android:src

      【讨论】:

        【解决方案3】:

        您可以使用android:src 而不是android:background 来设置您的drawable。比如:

        <ImageView  android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:src="@drawable/face"
                    android:scaleType="centerCrop" />
        

        另外,为什么不直接使用 Photoshop 等图像编辑软件裁剪图像并使用该图像呢?应避免动态缩放和操作图像,以尽可能避免大量使用系统资源。

        【讨论】:

          【解决方案4】:

          我认为,您的问题是在 LinearLayout 和 FrameLayout 中插入了权重。请使用无权重的 RelativeLayout。系统无法裁剪您的图片,因为系统无法测量 ImageView 并且无法将他与您的图片进行比较。总而言之,系统认为“这是图片和 ImageView 相等的”。 我已经这样做了:

           <RelativeLayout        
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" 
                  android:padding="5dp">
          
                <ImageView 
                    android:id="@+id/image"
                    android:layout_height="50dp"
                    android:layout_width="match_parent"
                    android:src="@drawable/photo"
                    android:scaleType="centerCrop"
                    android:contentDescription="@string/app_name"
                    android:layout_alignParentTop="true"
                    />
                <TextView android:layout_height="150dp"
                    android:layout_width="match_parent"
                    android:text="@string/text"
                    android:layout_below="@id/image"
                    android:maxLength="350"
                    android:layout_marginTop="5dp"/>
          
          </RelativeLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-25
            • 2011-09-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多