我正在使用 glide 在回收站 View 的 ImageView 中加载图像 URL,它工作正常。
现在,我想从动态图像 URL 设置 ActionBar 图标。 如何使用 glide 实现它?
我引用了以下链接:How to load a circular appcompat actionbar logo using glide . 但是,我没有得到结果。而且我不想切换到毕加索。
谁能帮帮我?
我的代码:
Glide.with(this)
.load(receiver1.profilepic)
.placeholder(R.drawable.user)
.into(new Target<GlideDrawable>()
{
@Override
public void onLoadStarted(Drawable placeholder)
{
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable)
{
}
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation)
{
Bitmap bitmap1=((GlideBitmapDrawable) resource).getBitmap();
Drawable drawable = new BitmapDrawable(getResources(), bitmap1);
actionBar.setIcon(drawable);
}
@Override
public void onLoadCleared(Drawable placeholder)
{
}
@Override
public void getSize(SizeReadyCallback cb) {
}
@Override
public void setRequest(com.bumptech.glide.request.Request request)
{
}
@Override
public com.bumptech.glide.request.Request getRequest()
{
return null;
}
@Override
public void onStart()
{
}
@Override
public void onStop()
{
}
@Override
public void onDestroy()
{
}
});
最佳答案
首先,您应该启用如下两个设置:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
然后你可以使用.asDrawable()
连同 CustomTarget<Drawable>()
像这样:
Glide.with(this).asDrawable().load(imageUrl).into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
getSupportActionBar().setLogo(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
注意事项:
- 确保替换上下文 (
this
) 和imageUrl
与您的数据。 - 不要使用
SimpleTarget
因为它现在从 4.8.0 版开始被弃用。
关于android - 使用 glide 在 ActionBar 上设置图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48515066/