1

如何解决这个问题?是否有任何其他组件比 ImageView 更好地用于显示来自服务器的图像? 目的:在我的应用中显示广告 2 秒。当间隔设置为 3 秒时,图像加载正常。但我减少到 2 秒,然后它会显示一个白屏,然后出现图像。

来自 url 的透析图像的 MainCode。

 Glide.with(this)
                .load(imageUri)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(adImage);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadToResumeActivity();
                //adImage.setVisibility(View.GONE);
                isAdEnabled = false;
                resume_layout.setVisibility(View.VISIBLE);
            }
        }, 2000);
4

3 回答 3

0

我认为您不需要延迟,Handler只需将延迟代码放入onResourceReadyGlide不能保证每次您的图像都会在 2 秒内加载,因此最佳做法是,仅在图像加载时可见。

Java 代码

Glide.with(context)
            .load(imageUri)
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false; // important to return false so the error placeholder can be placed
                }

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

Kotlin 代码

Glide.with(this)
        .load(imageUri)
        .listener(object : RequestListener<Drawable> {
          override fun onLoadFailed(e: GlideException?, model: Any?, target: com.bumptech.glide.request.target.Target<Drawable>?, isFirstResource: Boolean): Boolean {

            return false
          }

          override fun onResourceReady(resource: Drawable?, model: Any?, target: com.bumptech.glide.request.target.Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {

            return false
          }
        })
        .apply(RequestOptions()
                .dontTransform()
                .centerCrop()
        .into(adImage)
于 2019-08-30T05:02:43.263 回答
0

每当 UI 自行更新或网络调用时显示占位符或进度条的最佳方式,直到网络调用成功。有关占位符的更多信息,请查看此。

于 2019-08-30T05:07:17.707 回答
0

显示图像加载占位符的最佳方式。

Glide.with(this).load(photoToLoad.getPath()).placeholder(imageView.getDrawable()).into(imageView);
于 2019-08-30T05:11:37.983 回答