繁体   English   中英

如何在不损失图像质量的情况下完美地将图像放入图像视图中

[英]How to fit image into the imageview perfectly without losing image quality android

我试着去完美契合图像的imageView ,但图像dosent合身当我使用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>

对于那些想知道什么是布局 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>

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

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

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

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

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

如果您从服务器或什至从 drawable 获取图像,请使用glide 库将图像设置为图像视图。 Google 还推荐Glide 库来处理 android 中的图像。 很容易快速理解。 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM