简体   繁体   English

为什么Glide不会将图像从资源ID加载到我的ImageView?

[英]Why won't Glide load an image from resource ID to my ImageView?

I've got all of the images I need for my app, and I'm trying to speed it up and make it silky smooth, so I have been using Glide to load the images and it works in other areas of my app but not in my viewpager. 我已经拥有了我的应用程序所需的所有图像,并且我正在尝试加速它并使其柔滑,所以我一直在使用Glide加载图像,它可以在我的应用程序的其他区域工作但不是在我的viewpager中。

In my fragment I'm trying to load my image like so 在我的片段中,我试图像这样加载我的图像

Glide.with(this).load(R.drawable.my_drawable).into(myImageView);

and nothing happens or appears and I don't get an exception or any sort of error message from Glide. 没有任何反应或出现,我没有得到Glide的异常或任何错误消息。

I know this question is long since closed but I found a good solution for this by using the placeholder or error image instead. 我知道这个问题早已关闭但我通过使用占位符或错误图像找到了一个很好的解决方案。 So you might try 所以你可能会尝试

Glide.with(<<MyActivity>>)
    .load("")
    .placeholder(<<myDrawable>>)
    .into(imageView);

Not sure this is what you wanted but if there are others having the same issue, at least this seems to work with a minimum of work. 不确定这是你想要的,但如果有其他人有同样的问题,至少这似乎与最少的工作。 :) :)

Check out the Debugging and Error Handling wiki page to enable logging. 查看调试和错误处理 Wiki页面以启用日志记录。 That should tell you if the load is failing and why. 这应该告诉你负载是否失败以及原因。

If you see no error logs, it's possible that your View doesn't have a size, either because its size is 0, or because it hasn't gone through layout (visibility set to View.GONE etc). 如果您没有看到任何错误日志,那么您的View可能没有大小,因为它的大小为0,或者因为它没有经过布局(可见性设置为View.GONE等)。 If so, you can test check by adding a call to override() with any valid width and height (try 400, 400 or something just to test). 如果是这样,您可以通过添加对override()的调用以及任何有效的宽度和高度来测试检查(尝试400, 400或其他只是为了测试)。

If calling override() fixes the issue, take a look at your xml and see if you can change your view to either have a fixed size, or to at least eventually end up with a valid size. 如果调用override()修复了问题,请查看您的xml并查看是否可以将视图更改为具有固定大小,或者至少最终以有效大小结束。 If all else fails, you can always call override() with Target.SIZE_ORIGINAL as the width and height. 如果所有其他方法都失败了,您始终可以使用Target.SIZE_ORIGINAL作为宽度和高度调用override()

如果您使用的是Vector或SVG,则您的Glide版本可能不支持此功能并且无法加载。

Tobias Reich's way generates a bunch of warning logs cause load url is "". Tobias Reich的方式会产生一堆警告日志,导致加载网址为“”。 If you don't want warning logs, 如果您不想要警告日志,

Try this: 试试这个:

int drawableIdentifier = activity.getResources().getIdentifier("my_drawable_image_name", "drawable", activity.getPackageName());
Glide.with(activity)
     .load(drawableIdentifier)
     .into(imageView);

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

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