Java 如何用正确比例类型的图像填充RecyclerView(StaggedGridLayoutManager)?
我对我的回收视图有这样的问题 我需要向用户展示我在毕加索图书馆的帮助下从网上下载的图片到我的RecyclerView。最终看起来是这样的Java 如何用正确比例类型的图像填充RecyclerView(StaggedGridLayoutManager)?,java,android,css,image,android-recyclerview,Java,Android,Css,Image,Android Recyclerview,我对我的回收视图有这样的问题 我需要向用户展示我在毕加索图书馆的帮助下从网上下载的图片到我的RecyclerView。最终看起来是这样的 <ImageView android:id="@+id/imageViewMainCard" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY"
<ImageView
android:id="@+id/imageViewMainCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
/>
正如你们所看到的,我需要抓取我的图像,当然是在适当的纵横比下
我转到我的XML文件,在我的ImageView
android:scaleType=“fitXY”
现在我的ImageView
如下所示
<ImageView
android:id="@+id/imageViewMainCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
/>
这是我得到的结果
正如您现在看到的,图像填充了所有可用的空间,但它们不会以适当的高宽比划伤。图像划伤宽度大于高度,看起来不好看
这里还有我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:paddingBottom="3dp"
android:paddingEnd="3dp"
android:paddingStart="3dp"
android:paddingTop="3dp"
>
<LinearLayout
android:id="@+id/cardMainActivityLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/rippleInbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mrl_rippleColor="@color/ntz_background_light_grey"
app:mrl_rippleOverlay="true"
>
<ImageView
android:id="@+id/imageViewMainCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
/>
</com.balysv.materialripple.MaterialRippleLayout>
<RelativeLayout
android:id="@+id/rlCardMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:padding="4dp"
>
<TextView
android:id="@+id/tvBrandName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textColor="@color/black_color"
android:textSize="10dp"
/>
<TextView
android:id="@+id/tvItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvBrandName"
android:textColor="@color/black_color"
android:textSize="10dp"
/>
<TextView
android:id="@+id/tvPreviousPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvItemName"
android:textColor="@color/black_color"
android:textSize="10dp"
/>
<TextView
android:id="@+id/tvDivider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/tvPreviousPrice"
android:layout_toEndOf="@+id/tvPreviousPrice"
android:text=" / "
android:textSize="10dp"
android:visibility="gone"
/>
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/tvDivider"
android:layout_toEndOf="@+id/tvDivider"
android:textColor="@color/black_color"
android:textSize="10dp"
/>
<Button
android:id="@+id/bAction"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:textSize="8dp"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
我做错了什么
Edit1
有一个android:scaleType=“centerCrop”属性的结果
女人没有头也不太好
Edit2
这是android:scaleType=“centerInside”
fitXY
使用FILL
,它被描述为“在X和Y方向上独立缩放,以便src与dst精确匹配。这可能会改变src的纵横比”
您应该使用
centerInside
居中、调整和缩放,同时保持纵横比要回答您的问题,请使用centerCrop将图像绝对居中于其父图像。还要添加这个属性,android:adjustViewBounds=“true”。这样,图像的纵横比将被保留。好的,问题是您的布局不尊重图像的边界。要解决此问题,请将图像视图包装在单独的父布局中,使用layout\u weight=“1”将父权重添加到布局中。然后使用android设置相对于布局的宽度:layout\u width=“0px”,以避免撕裂和适当的边界协调设置android:adjustViewBounds=“true”。要居中并在图像中心裁剪图像,请使用android:scaleType=“centerCrop”“@PratikButani根据您的建议添加了有问题的屏幕截图,其中哪部分不起作用?它看起来就像你想要的:当然不是,你没看到细胞的左右边界和图像的左右边界不一样吗??试着将我以前截图中的图片和我根据你们的建议得到的图片进行比较。你们看到图像宽度的不同了吗?实际上它和我的第一张截图并没有区别当然是的。当您询问图像匹配问题时,centerInside解决了您的图像匹配问题。它们可以完美缩放,而不会破坏纵横比,这正是您想要的。浅灰色背景与图像或其适合性无关。))好的,让我向你解释更多细节)我需要缩放我的图像,所以,我需要图像占据单元格中所有可用的空间,但我需要比例与适当的纵横比。。。看看我的第二张截图。我需要完全相同,但适当的长宽比。你明白我的意思了吗?明白!我不知道如何否决你的答案,但这正是我需要的!谢谢Caspain你好吗?)为什么你对同一个问题回答了两次?