【问题标题】:How to Scale images to imageview perfectly with fixed width and height without distorting the image in Android如何以固定的宽度和高度完美地将图像缩放到 imageview 而不会在 Android 中扭曲图像
【发布时间】:2015-10-22 14:26:49
【问题描述】:

我正在创建一个应用程序来访问画廊或相机中的图像。为此,我有一个尺寸为 280/280dp 的图像视图。如何在不扭曲 Android 中的图像的情况下将相机/图库(更大的图像)中的图像缩放到具有固定宽度和高度的图像视图下方:

<ImageView
    android:layout_width="280dp"
    android:layout_height="280dp"
    android:id="@+id/mainImage"
    android:background="@drawable/profile_settings_main_image"
    android:layout_marginTop="19dp"
    android:layout_below="@+id/container_toolbar"
    android:layout_centerHorizontal="true"
    android:focusable="false"
    android:adjustViewBounds="true"
    />

activity_main.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"

tools:context="in.ABC.activity.ProfileSettings"
android:background="#93BFB6">

<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />



</LinearLayout>


<TextView android:layout_width="230dp"
    android:layout_height="50dp"
    android:textSize="22sp"
    android:background="@drawable/profile_settings_name"
    android:id="@+id/nameView"
    android:textColor="#000000"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/mainImage"
    android:layout_alignLeft="@+id/mainImage"
    android:layout_alignStart="@+id/mainImage"
    android:layout_marginRight="1dp"
    android:gravity="center"/>

<ImageView
    android:layout_width="280dp"
    android:layout_height="280dp"
    android:id="@+id/mainImage"
    android:background="@drawable/profile_settings_main_image"
    android:layout_marginTop="19dp"
    android:layout_below="@+id/container_toolbar"
    android:layout_centerHorizontal="true"
    android:focusable="false"
    android:adjustViewBounds="true"
    />

<ImageButton
    android:layout_width="50dp"
    android:layout_height="100dp"
    android:id="@+id/editName"
    android:background="@drawable/profile_settings_image"
    android:layout_alignTop="@+id/nameView"
    android:layout_toRightOf="@+id/nameView"
    android:layout_toEndOf="@+id/nameView"
    android:layout_alignBottom="@+id/nameView" />

    <ImageButton
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/editImage"
    android:background="@drawable/profile_settings_image_2"
    android:layout_alignBottom="@+id/mainImage"
    android:layout_alignRight="@+id/mainImage"
    android:layout_alignEnd="@+id/mainImage"
    android:focusable="true" />

</RelativeLayout>

【问题讨论】:

    标签: android image


    【解决方案1】:

    如果图像的比例因子与ImageView 的比例因子不匹配,那么您无法在不牺牲清晰度的情况下匹配高度/宽度。您必须妥协并决定是否要截断一些图像,或者让整个图像适合内部但承认会有一些剩余空间。

    有关android:scaleTypehttp://www.techrepublic.com/article/clear-up-ambiguity-about-android-image-view-scale-types-with-this-guide/的更多信息,请参见此处

    【讨论】:

      【解决方案2】:

      你可以试试Android ImageView.ScaleType:

      <ImageView
          android:layout_width="280dp"
          android:layout_height="280dp"
          android:id="@+id/mainImage"
          android:background="@drawable/profile_settings_main_image"
          android:layout_marginTop="19dp"
          android:layout_below="@+id/container_toolbar"
          android:layout_centerHorizontal="true"
          android:focusable="false"
          android:adjustViewBounds="true"
      
          android:scaleType="fitXY"
          />
      

      代码结果:

      Result of the code

      原图:

      Original Picture

      【讨论】:

      • 当我使用 android:scaleType="fitXY" 时,我的图像会被拉伸以填补图像视图中的额外空白
      • 你试过 android:scaleType="CENTER_INSIDE" 吗?另请阅读link
      猜你喜欢
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多