ImageButton调用setImageDrawable(),但获取的尺寸不正确。

huangapple 未分类评论44阅读模式
英文:

ImageButton call setImageDrawable() but get wrong size

问题

以下是翻译好的部分:

我正在编写一个包含 RecyclerView 的 Android 应用程序,它位于我的 ActivityMain 中。每一行都有一个 ImageButton 和一个文本标签。在 onBindViewHolder() 方法中,我从 URL 下载一张图片,然后在使用 setImageDrawable 方法后,我将图片更换为刚刚下载的图片。

  1. @Override
  2. public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
  3. holder.textAuthor.setText(posts.get(position).getAuthorName());
  4. Thread thread = new Thread(new Runnable() {
  5. @Override
  6. public void run() {
  7. try {
  8. // 从我的 Posts 数组列表中获取 URL 图片(其中包含 ImageButton 和 textLabel)
  9. URL url = new URL(posts.get(position).getImageView());
  10. // 从 URL 获取输入流
  11. InputStream inputStream = (InputStream) url.getContent();
  12. // 创建一个包含下载图片的可绘制对象
  13. Drawable drawable = Drawable.createFromStream(inputStream, null);
  14. // 使用新图片替换现有图片
  15. holder.imageButton.setImageDrawable(drawable);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. });
  21. thread.start();
  22. }

所有的 ImageButton 都可以正确地更换图片,并从它们的 URL 下载图片,但问题是在我滚动下来之前,它们的大小是错误的,直到我使用手指滚动在 RecyclerView 内部滚动一段时间后。这听起来就像我需要刷新我的视图,但我不知道该如何做。

注意:所有的图片大小都不同。

我附上了一些图片来更好地解释我的问题:

当我打开应用程序并开始从 URL 下载图片时

当下载完成后,图片的大小不正确

如果我向下滚动,然后用手指向上滚动,我会得到正确大小的图片

其余的代码在这里: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.

  1. @Override
  2. public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
  3. holder.textAuthor.setText(posts.get(position).getAuthorName());
  4. Thread thread = new Thread(new Runnable() {
  5. @Override
  6. public void run() {
  7. try {
  8. // Get URL Image from my ArrayList Posts(which contains imageButton and textLabel)
  9. URL url = new URL(posts.get(position).getImageView());
  10. // Get inputStream from URL
  11. InputStream inputStream = (InputStream) url.getContent();
  12. // Create a drawable which contains my downloaded image
  13. Drawable drawable = Drawable.createFromStream(inputStream, null);
  14. // Change the image with the new one
  15. holder.imageButton.setImageDrawable(drawable);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. });
  21. thread.start();
  22. }

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

尝试以下代码,这对我有效:

  1. <ImageButton
  2. android:id="@+id/imageButton2"
  3. android:layout_width="25dp"
  4. android:layout_height="25dp"
  5. app:srcCompat="@drawable/about" />

为您的图像按钮设置固定的高度和宽度,然后它将起作用。

英文:

Try the following code it worked for me

  1. &lt;ImageButton
  2. android:id=&quot;@+id/imageButton2&quot;
  3. android:layout_width=&quot;25dp&quot;
  4. android:layout_height=&quot;25dp&quot;
  5. app:srcCompat=&quot;@drawable/about&quot; /&gt;

give your image button fixed height and width then it will work

huangapple
  • 本文由 发表于 2020年4月5日 18:16:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61041035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定