【问题标题】:How can you use a loading placeholder that is an animated spinner with image loaders like Glide, Picasso etc?你如何使用一个加载占位符,它是一个带有图像加载器(如 Glide、Picasso 等)的动画微调器?
【发布时间】:2026-01-03 20:20:06
【问题描述】:

我正在尝试在使用 Glide 或 Picasso 等图像加载器库加载图像时插入一个通用加载圆作为占位符。

我这辈子都不知道你应该如何从 xml 创建一个可旋转的drawable。

我尝试通过创建动画列表在 XML 中使用 AnimationDrawable,但它甚至没有显示(甚至不是静态的)。

我只想有一个简单的圈子,它自己旋转,全部在一个 xml 可绘制对象中,所以我可以将 id 作为占位符传递给我的图像加载器。如果那不可能,请现在告诉我,这样我可以节省大量研究:)

编辑:一些代码让它更清楚,我需要一个用于这个 Glide 命令的旋转绘图:

Glide.with(context)
.load(imageUrl)
.into(imageView)
.placeHolder(R.drawable.spinning_loading_placeholder);

在加载图像时,将在图像稍后的位置显示占位符可绘制对象。我需要一个可以自己旋转的drawable。

【问题讨论】:

标签: android animation spinner drawable android-glide


【解决方案1】:

您可以将 ProgressBar 元素添加到布局 XML,然后根据需要显示/隐藏此元素。 ProgressBar 视图是内置的并且已经动画化,不需要自定义 AnimationDrawable。

【讨论】:

  • 谢谢,但这不是我需要的。我正在使用 bumbtech 的 glide 图像加载库从服务器加载图像列表,而图像正在加载一个占位符可绘制的占位符,用于显示稍后将出现真实图像的每个图像。我希望这个占位符是一个动画旋转圆圈,以指示图像正在加载,为此我需要这个旋转圆圈是一个可绘制的,没有像对象动画师或代码中的动画这样的额外代码。我添加了一些代码以使其更清晰。
  • 你有没有得到解决这个问题的方法?也在寻找这个......
【解决方案2】:

你可以这样使用:

Glide.with(mContext).load(imageUrl).asBitmap().centerCrop().into(new BitmapImageViewTarget(vh.avatarImageView) {

        @Override
        public void onLoadStarted(Drawable placeholder) {
                vh.avatarImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_placeholder));
        }

        @Override
        public void onLoadFailed(Exception e, Drawable errorDrawable) {
            vh.avatarImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_failed_download));
        }

        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            vh.avatarImageView.setImageDrawable(circularBitmapDrawable);
        }
    });

【讨论】:

    最近更新 更多