【问题标题】:Glide does not display picture in a Fragment in AndroidGlide 在 Android 的 Fragment 中不显示图片
【发布时间】:2021-03-19 17:31:27
【问题描述】:

我刚开始使用 Glide 将图像加载到我的 Android 应用程序中。我在片段中使用它,我想将工具栏的背景设置为 URL 中的某个图形。不幸的是,滑翔不显示图片,而只是工具栏中的白色背景。工具栏本身不是问题,因为我可以用颜色改变它的背景,这很有效。还可以显示 App 文件夹中保存的图像(我将其存储在 drawable 文件夹中)。

这是我在片段的 onCreateView 方法中执行的代码:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentGenericBinding.inflate(inflater, container, false);




        ImageView image = new ImageView(getContext());

        image.setDrawingCacheEnabled(true);
        Glide.with(this).load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
                .into(image);
        Bitmap bmap = image.getDrawingCache();
        binding.toolbarGenericMenu.setBackground(new BitmapDrawable(getResources(), bmap));

        //This code works at creates a green background
        //ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(getContext(), R.color.colorGreen));
        //binding.toolbarGenericDrinkMenu.setBackground(colorDrawable);


       //This code also works and display an image from R.drawable on the toolbar
       // binding.toolbarGenericMenu.setBackgroundResource(R.drawable.test_foto);




        return binding.getRoot();
    }

知道为什么使用 Glide 加载图像不起作用吗?我会很感激每一条评论。

【问题讨论】:

  • 提供日志以及为什么 new ImageView(getContext());?
  • 你没有提到你使用的是什么类型的工具栏,所以检查这个post,具体如何使用.into()。
  • 感谢 Eyosiyas 的评论。好吧,我必须以某种方式实例化 ImageView。我也试过“ImageView image = null;”但它也确实有效。所以我使用了“new ImageView(getContext());”。你对我有什么建议
  • 感谢 javdromero 的评论。我使用 androidx.appcompat.widget.Toolbar

标签: java android android-glide


【解决方案1】:

如果你在 Kotlin 的布局中使用 androidx.appcompat.widget.Toolbar,你可以试试这个

val toolbar = findViewById<Toolbar>(R.id.mToolbar)

        Glide.with(this)
                .asBitmap()
                .load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
                .into(object : CustomTarget<Bitmap>() {
                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                        toolbar.background = BitmapDrawable(resources, resource)
                    }
                    override fun onLoadCleared(placeholder: Drawable?) {}
                })

Java

Toolbar toolbar = findViewById(R.id.mToolbar);
        Glide.with(this)
                .asBitmap()
                .load("https://images.unsplash.com/photo-1614907301762-405163ba662f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80")
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        toolbar.setBackground(new BitmapDrawable(getResources(), resource));

                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });

【讨论】:

  • 感谢基尚的回答。我使用 Java 而不是 Kotlin。你能用 Java 翻译这个吗?
  • 任何是的,我使用 androidx.appcompat.widget.Toolbar
  • Java 的更新答案
  • 感谢基尚的回答。现在它可以正常工作了:-)。我赞成并接受了你的回答。也许有一些后续问题:1)当我想通过 Glide 加载图像时,我可以总是使用你建议的代码吗? 2)为什么我需要 onLoadCleared 方法,为什么它是空的 3)你知道一个很好的滑翔教程,因为我使用的那个代码不起作用
  • 4) 为什么你使用 Bitmap 而不是 ImageView 5) 我是否总是必须为每个滑行操作使用 'onResourceReady' 和 'onLoadCleared' 方法?因为在许多教程中我根本没有看到它们。如果您有任何进一步的 cmets,我将不胜感激。
猜你喜欢
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 2017-08-01
  • 2020-11-20
  • 2018-10-29
  • 1970-01-01
  • 2022-12-08
  • 2021-08-24
相关资源
最近更新 更多