相关疑难解决方法(0)

以编程方式为imageView设置宽高比

我在显示没有centerCrop的风景图像时遇到问题.我尝试过PercentFramelayout,并以编程方式设置宽高比,如下所示:

laParams.percentLayoutInfo.aspectRatio = img.width.toFloat() / img.height.toFloat()
Run Code Online (Sandbox Code Playgroud)

结果还可以 - 应用程序显示没有centerCrop的所有风景图像:

发布成功加载

但有时我会得到错误的宽高比:

宽高比错误1

错误的宽高比2

我试过android:adjustViewBounds ="true"但它对我没有帮助.我用过ConstraintLayout,用XML设置宽高比如下:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    >

    <android.support.v7.widget.AppCompatImageView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/photo"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="h,16:9"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

我得到了很好的结果,但我加载了不同大小的图像.图像应该没有CenterCropFitXY.我没有找到关于设置宽高比的任何好答案,因为Constraintlayout 我想显示像instagram或vk这样的图像.

android aspect-ratio android-constraintlayout

7
推荐指数
3
解决办法
5588
查看次数