android - 使用 Glide 加载图像时的进度条

标签 android android-glide

在使用 Glide 加载图像之前,我可以在占位符中加载带有旋转动画的微调器吗?

我正在尝试使用 .placeholder(R.Drawable.spinner) 来做到这一点,没有动画出现?

如果有人能帮帮我就好了?

谢谢!

最佳答案

编辑: 现在使用 CircularProgressDrawable 非常简单

build.gradle

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

MyGlideModule.kt

@GlideModule
class MyGlideModule : AppGlideModule()

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_main)

  val circularProgressDrawable = CircularProgressDrawable(this)
  circularProgressDrawable.strokeWidth = 5f
  circularProgressDrawable.centerRadius = 30f
  circularProgressDrawable.start()

  GlideApp.with(applicationContext)
      .load("https://raw.githubusercontent.com/bumptech/glide/master/static/glide_logo.png")
      .placeholder(circularProgressDrawable)
      .into(a_main_image)
}

这些是其他一些 Glide snippets


老答案:你也可以创建一个普通的ProgressBar,然后隐藏在Glide的onResourceReady()上。

The method that will be called when the resource load has finished.


示例:

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ImageView imageView = (ImageView) findViewById(R.id.img_glide);
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress);

    Glide.with(this)
            .load("https://raw.githubusercontent.com/bumptech/glide/master/static/glide_logo.png")
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    progressBar.setVisibility(View.GONE);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    progressBar.setVisibility(View.GONE);
                    return false;
                }
            })
            .into(imageView);
}

activity_main.xml(布局):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:visibility="visible" />

    <ImageView
        android:id="@+id/img_glide"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</RelativeLayout>

结果:

enter image description here

关于android - 使用 Glide 加载图像时的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35305875/

相关文章:

java - 无法使用 Apache POI 写入 Excel 文件

android - 弧焊机无法通过代理连接互联网

android - 模拟器 Admob 广告

android - 如何使用Android-Volley设置动画图像直到图像加载

android - Recyclerview Adapter 和 Glide - 每 4-5 行相同的图像

android - 如何使用 Glide 检查图像是否已经缓存在 android 手机中

Android WIFI ADB 插件不适用于某些 Android 真实设备?

java - 使用 MVP 模式实现 Glide 回调

android - Glide : onError Callback

android - 共享过渡元素和 Glide 策略