本文介绍了使用Glide在MenuItem中加载远程图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常,如果要使用Glide加载图像,我会写以下内容:
Usually if I want to load an image with Glide I would write the following:
Glide.with(context)
.load(theURLOftheImage)
.error(R.drawable.ic_error_image)
.into(theImageView);
但是如果我需要将该URL的图像加载到必须实时更改的MenuItem中,该怎么办?
but what if I need to load the image of that URL into a MenuItem that has to be changed in real time?
以下是不可能的,因为方法into
不接受参数:
The following is not possible because the method into
does not accept the parameter:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.actionbar_menu_profile_actions);
if (changeImage) {
Glide.with(this).load(theURLOftheImage).error(R.drawable.ic_error_image).into(settingsItem);
}
return super.onPrepareOptionsMenu(menu);
}
推荐答案
使用针对有效
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.actionbar_menu_profile_actions);
if (changeImage) {
Glide.with(this).asBitmap().load(theURLOfTheImage).into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
settingsItem.setIcon(new BitmapDrawable(getResources(), resource));
}
});
}
return super.onPrepareOptionsMenu(menu);
}
这篇关于使用Glide在MenuItem中加载远程图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!