【问题标题】:How to fit image into the imageview perfectly without losing image quality android如何在不丢失图像质量的情况下完美地将图像放入 imageview android
【发布时间】:2021-09-20 23:34:19
【问题描述】:

我试图在imageView 中完美地拟合图像,但是当我将centerCropadjustViewBounds 一起使用时,图像正确拟合,但是当我将fitXYadjustViewBounds 一起使用时,图像非常适合 imageView 但现在图像质量变得最差,我的 imageView heightwidthmatch_parent 是的,我也使用了 fitCenter 但没用

截图//目前我使用centerCropadjustViewBounds

这是我的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:weightSum="5">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/Card_View"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="4"
        app:shapeAppearanceOverlay="@style/RoundedCornerHome">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/imagePostHome"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                android:contentDescription="@string/todo"
                app:shapeAppearanceOverlay="@style/RoundedCornerHome" />

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

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:background="@color/grey"
        app:cardBackgroundColor="@color/grey"
        app:shapeAppearanceOverlay="@style/RoundedCornerHome">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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


    </com.google.android.material.card.MaterialCardView>


</LinearLayout>

想知道什么是layout material_cardview_snipet,这里是它的代码

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="25dp"
        android:src="@drawable/ic_baseline_attach_money_24"
        tools:ignore="RtlHardcoded"
        android:contentDescription="@string/todo" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Ian Somerhalder"
        android:textColor="@color/white"
        android:layout_marginTop="5dp"
        android:textSize="20sp"
        android:textStyle="bold"
        tools:ignore="SmallSp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        tools:ignore="RtlHardcoded"
        android:textSize="17sp"
        android:layout_marginLeft="10dp"
        android:textColor="@color/white"
        android:text=".............."/>



</FrameLayout>

【问题讨论】:

    标签: android android-imageview screen-resolution image-scaling adjustviewbounds


    【解决方案1】:

    如果您是图像大小,我的意思是高度和宽度比与您的图像视图不匹配高度和宽度比滑动将使图像适合您的图像视图的中心,如果比例不匹配,则部分图像会被剪切'不匹配。 如果您强制图像适合图像视图,即使比率不匹配,它将被压缩以适合 x 轴或 y 轴。所以你的图片看起来不像原来的。

    所以像这样使用你的图像视图属性 android:layout_width="match_parent" android:layout_height="wrap_content"

    这里宽度是固定的,高度会根据图片高度计算。

    【讨论】:

      【解决方案2】:

      您可以在 firebase 中调整图像大小,无需编写代码 https://firebase.google.com/products/extensions/storage-resize-images 你可以使用这个很酷的 firebase 扩展。它将上传到 Cloud Storage 的图像调整为指定大小,并可选择保留或删除原始图像。

      它需要一个 blaze 计划,为此使用它的试用版,或者如果您是学生,您可以使用您的 .edu id 并从 google 获取它。

      【讨论】:

        【解决方案3】:

        如果您从服务器或什至从 drawable 获取图像,请使用 glide 库 将图像设置为图像视图。 Google 还有recommendsGlide library 来处理android 中的图像。 这很容易快速理解。见Doc

        【讨论】:

        • 是的,兄弟我同时使用 firebase 和 glide,但它对我解决这个问题没有帮助
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-22
        • 1970-01-01
        • 2016-02-04
        • 2019-10-26
        • 2012-03-07
        • 2021-10-13
        • 1970-01-01
        相关资源
        最近更新 更多