英文:
ImageButton call setImageDrawable() but get wrong size
问题
以下是翻译好的部分:
我正在编写一个包含 RecyclerView 的 Android 应用程序,它位于我的 ActivityMain 中。每一行都有一个 ImageButton 和一个文本标签。在 onBindViewHolder() 方法中,我从 URL 下载一张图片,然后在使用 setImageDrawable 方法后,我将图片更换为刚刚下载的图片。
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.textAuthor.setText(posts.get(position).getAuthorName());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// 从我的 Posts 数组列表中获取 URL 图片(其中包含 ImageButton 和 textLabel)
URL url = new URL(posts.get(position).getImageView());
// 从 URL 获取输入流
InputStream inputStream = (InputStream) url.getContent();
// 创建一个包含下载图片的可绘制对象
Drawable drawable = Drawable.createFromStream(inputStream, null);
// 使用新图片替换现有图片
holder.imageButton.setImageDrawable(drawable);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
所有的 ImageButton 都可以正确地更换图片,并从它们的 URL 下载图片,但问题是在我滚动下来之前,它们的大小是错误的,直到我使用手指滚动在 RecyclerView 内部滚动一段时间后。这听起来就像我需要刷新我的视图,但我不知道该如何做。
注意:所有的图片大小都不同。
我附上了一些图片来更好地解释我的问题:
其余的代码在这里:MainActivty MyAdapter PostClass <br>
包含 ImageButton 的 XML 文件:我的 RecyclerView 行
希望您能像往常一样帮助我,非常感谢!
附注:我已经使用 Glide 修复了下载图片的问题!
英文:
I was writing an android application which contains a RecyclerView inside my ActivityMain. Each rows has an imageButton and a textLabel. In onBindViewHolder() method, I download an image from an URL and after using setImageDrawable method, I change the image with the one that I just downloaded.
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.textAuthor.setText(posts.get(position).getAuthorName());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// Get URL Image from my ArrayList Posts(which contains imageButton and textLabel)
URL url = new URL(posts.get(position).getImageView());
// Get inputStream from URL
InputStream inputStream = (InputStream) url.getContent();
// Create a drawable which contains my downloaded image
Drawable drawable = Drawable.createFromStream(inputStream, null);
// Change the image with the new one
holder.imageButton.setImageDrawable(drawable);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
All my ImageButton change image correctly downloading from their URL, but the problem is that they have wrong size until I scroll down and after a while I scroll up inside of my RecyclerView using my finger. It sounds like if I have to refresh my View but I don't know how to do this.
Note: all images have different size
I let you some images to explain better my problem:
When I open my application and it starts to download image from URL
When the download is completed the images have wrong size
If I scroll down and after I scroll up I get the images with the correct size
The rest of my codes is here: MainActivty MyAdapter PostClass <br>
XML File which contains ImageButton: my row of RecyclerView
Hope you can help me as always, thank you a lot!
EDIT: I have fixed downloading image using Glide!
答案1
得分: 0
尝试以下代码,这对我有效:
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="25dp"
android:layout_height="25dp"
app:srcCompat="@drawable/about" />
为您的图像按钮设置固定的高度和宽度,然后它将起作用。
英文:
Try the following code it worked for me
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="25dp"
android:layout_height="25dp"
app:srcCompat="@drawable/about" />
give your image button fixed height and width then it will work
专注分享java语言的经验与见解,让所有开发者获益!
评论