在横屏模式下,ImageView会被拉伸

4
我希望即使在横屏模式下,我的ImageView的长宽比仍然保持不变。即使我使用了scaleType:fitXY和adjustBounds,它看起来也非常拉伸。这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="24dp"
    android:layout_gravity="center">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:id="@+id/image" />
        />
   </FrameLayout>

 </LinearLayout>

更新: 我使用Glide设置了我的ImageView。它主要是一张640 x 330的图片。

       Glide.with(this)
            .load("http://www.web-cars.com/lambo_sb/IMG_3410.jpg")
            .into(imageView);

将ImageView的宽度设置为wrap_content。android:layout_width="wrap_content" - Komal12
“FitXY”会始终拉伸,因为在横向模式下的X轴总是大于纵向模式下的X轴。 - user2756345
FitXY is supposed to stretch the image to fill the screen.Try overriding onConfigurationChanged - Sunil Sunny
FitXY独立缩放X和Y,因此可能会改变宽高比。请使用FitCenter。 - Mehmet K
我尝试使用fitCenter。它被对齐到中心,但是ImageView的宽度没有被拉伸。 - luckysing_noobster
显示剩余2条评论
4个回答

0

这与编程无关,但如果您不想在AndroidManifest.xml文件中设置为活动android:screenOrientation="portrait",则可以帮助您完全不进入横向模式


0

使用PercentFrameLayout https://developer.android.com/reference/android/support/percent/PercentFrameLayout.html

为纵向和横向配置创建两个布局文件

纵向

<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

  <ImageView
      app:layout_aspectRatio="136%"
      app:layout_widthPercent="100%"
      android:layout_gravity="center"
      tools:ignore="ContentDescription"
      tools:src="@color/accent"
      />

</android.support.percent.PercentFrameLayout>

横向

<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

  <ImageView
      app:layout_aspectRatio="136%"
      app:layout_heightPercent="100%"
      android:layout_gravity="center"
      tools:ignore="ContentDescription"
      tools:src="@color/accent"
      />

</android.support.percent.PercentFrameLayout>

如何使用 widthPercent 100 可以帮助他。 - Jitesh Mohite

0

试试这个,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:padding="24dp">

            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/placeholder" />
            />
        </FrameLayout>

    </LinearLayout>

0

我认为这将解决您在不确定服务器上图像大小时遇到的问题。

Glide.with(this)
                .load("http://www.web-cars.com/lambo_sb/IMG_3410.jpg")
                .centerCrop()
                .override(200, 200)
                .into(imgView);

-图像调整大小和裁剪 如果您不确定从远程服务器加载的图像的大小

that what will be the size of the image . so in this code snippet image is resized to 200X200 and center cropped.


Glide.with(this)
        .load("IMAGE URL HERE")
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.imagenotfound)
        .override(200, 200);
        .centerCrop();
        .into(imageView);

享受编程,分享知识

链接:

[http://www.androidtutorialshub.com/glide-image-loading-library-for-android-tutorial/][1]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接