微光效果不适用于高度 wrap_content [英] Shimmer Effect not working on Height wrap_content
问题描述
我正在尝试使用 wrap_content 的高度实现微光效果,但图像没有加载,我知道为什么它不加载图像,因为 imageView 有 wrap_content 并且微光也有 wrap_content 但我想要高度应该是 wrap_content 而不是固定的.
I'm trying to implement the shimmer effect with the Height of wrap_content but the images are not loading, I know why it is not loading the images because the imageView has wrap_content and the shimmer also has wrap_content but I want the Height Should be wrap_content and not fixed.
在微光中实现例如 200dp 的固定高度后,它可以工作,但之后图像无法加载
After implementing a fixed height of eg 200dp in shimmer it works but after that images are not loading
我想让它像 Pinterest 一样,根据图像调整高度
I want to make it like Pinterest where the height is adjusted according to the image
XML 文件
post_item_container_search.xml
<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imagePostSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="6dp"
android:contentDescription="@string/todo"
app:shapeAppearanceOverlay="@style/RoundedCorner" />
post_item_containe_shimmer.xml
<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imageShimmer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:layout_marginTop="11dp"
android:background="#E7E7E7"
android:contentDescription="@string/todo"
app:shapeAppearanceOverlay="@style/RoundedCorner" />
这是将 minHeight 添加到两者或实际 imageView 后的样子
this how it looks like after adding minHeight to both or in actual imageView
推荐答案
在微光中实现例如 200dp 的固定高度后,它可以工作,但之后图像无法加载
After implementing a fixed height of eg 200dp in shimmer it works but after that images are not loading
在这种情况下,您应该首先为 ImageView
设置绝对宽度/高度.稍后,如果您确实需要,可以将其重新设置为 WARP_CONTENT
.但首先你需要一个估计/绝对宽度/高度的视图.
In that case, you should probably set an absolute width/height for the ImageView
first. Later, you can set it back to WARP_CONTENT
if you really need to. But first you'll need an estimated/absolute width/height for the view.
int height = bitmap.getHeight();
int width = bitmap.getWidth();
ShapeableImageView siv = findViewById(R.id.imagePostSearch);
ViewGroup.LayoutParams params = siv.getLayoutParams();
params.height = height;
params.width = width;
//To set it back to warp content or match parent:
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
这篇关于微光效果不适用于高度 wrap_content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!