【问题标题】:Load a remote image in a MenuItem using Glide使用 Glide 在 MenuItem 中加载远程图像
【发布时间】:2016-05-09 12:47:54
【问题描述】:

通常,如果我想用 Glide 加载图像,我会写以下内容:

Glide.with(context)
     .load(theURLOftheImage)
     .error(R.drawable.ic_error_image)
     .into(theImageView);

但是如果我需要将该 URL 的图像加载到必须实时更改的 MenuItem 中怎么办?

以下是不可能的,因为方法into不接受参数:

@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);
}

【问题讨论】:

    标签: android android-glide uimenuitem


    【解决方案1】:

    使用this question 的回复中建议的方法有效

    @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);
    }
    

    【讨论】:

      【解决方案2】:

      我的一个编码填充BottomNavigationView的另一种方法:

          ...
          bottomNavigationView = findViewById(R.id.bottomNavigationView);
      
          if(bottomNavigationView != null) {
      
              bottomNavigationView.inflateMenu(R.menu.bottom_main_menu);
      
              Menu bottomMenu = bottomNavigationView.getMenu();
      
              //bottomMenu.removeItem(0);
      
              final MenuItem menuItem = bottomMenu.add("Test 95");
      
              Glide
                      .with(this)
                      .load("https:// <add your image resource link here>")
                      .into(new SimpleTarget<Drawable>() {
                          @Override
                          public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                              menuItem.setIcon(resource);
                          }
                      });
      
          }
          ...
      

      记得在 app gradle 中添加正确的 Glide 版本,4.7.1 应该可以使用这个:

         implementation 'com.github.bumptech.glide:glide:4.7.1'
      

      【讨论】:

      【解决方案3】:

      由于不推荐使用 SimpleTarget,因此针对此页面的用户还有其他解决方案。

       Glide
              .with(activity)
              .load(path)
              .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
              .centerCrop()
              .listener(new RequestListener<Drawable>() {
               @Override
               public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
               
                return false;
               }
      
               @Override
               public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
               menuItem.setIcon(resource);
                return false;
               }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-23
        • 2017-12-17
        • 2016-03-30
        相关资源
        最近更新 更多