滑行图像有时未加载 [英] Glide images sometimes are not loading

查看:55
本文介绍了滑行图像有时未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个问答游戏,答案可以是文本或图像,我正在使用Gilde图像进行加载,但是对于某些用户,即使互联网良好且图像质量较低,也无法加载图像.

Is a quiz game and the answer can be text or image, i'm using Gilde image to load it but for some users the images can't load, even if the internet is good and the images are in low quality.

我将复制所有代码,因为我不知道是否是Gilde问题.

I'll copy all the code because i have no idea if is Gilde problem or not.

if (AppConstant.arQuestionList.get(questionid).getArAnswer().get(i).getAtype()
                    .equalsIgnoreCase("image")) {
                txtOption = new ImageView(this);

                try {
                    Glide.with(QuestionActivity.this).load(AppConstant.arQuestionList.get(questionid)
                            .getArAnswer().get(i).getAnswer()).override(60, 60).into((ImageView)
                            txtOption);
                }catch (OutOfMemoryError e){
                    e.printStackTrace();
                }
                txtOption.setPadding(5, 5, 5, 5);

            } else {
                txtOption = new TextView(this);

                ((TextView) txtOption).setText(AppConstant.arQuestionList.get(questionid)
                        .getArAnswer().get
                                (i).getAnswer());
                ((TextView) txtOption).setTextColor(getResources().getColor(R.color.answer_color));
                ((TextView) txtOption).setTypeface(tf, Typeface.BOLD);
                ((TextView) txtOption).setGravity(Gravity.CENTER);
                txtOption.setPadding(10, 20, 10, 20);
            }

布局

    <LinearLayout
        android:id="@+id/answerwrap"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_30"
        android:layout_marginRight="@dimen/dp_30"
        android:layout_marginTop="@dimen/dp_30"
        android:orientation="vertical">

    </LinearLayout>

推荐答案

我在这里解释的最佳滑行方法.

the best way to use glide I am explaining here.

//crete this method into your Utils class and call this method wherever you want to use.
//you can set these placeHolder() and error() image static as well. I made it as comment inside this method, then no need to use [placeHolderUrl and errorImageUrl] parameters. remove it from this method.
public static void loadImage(final Activity context, ImageView imageView, String url, int placeHolderUrl, int errorImageUrl) {
    if (context == null || context.isDestroyed()) return;

    //placeHolderUrl=R.drawable.ic_user;
    //errorImageUrl=R.drawable.ic_error;
        Glide.with(context) //passing context 
                .load(getFullUrl(url)) //passing your url to load image.
                .placeholder(placeHolderUrl) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                .error(errorImageUrl)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
                .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
                .animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
                .fitCenter()//this method help to fit image into center of your ImageView 
                .into(imageView); //pass imageView reference to appear the image.
}

fade_in.xml (将其放入res-> anim)

fade_in.xml (put it into res->anim)

<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--THIS ANIMATION IS USING FOR FADE IN -->

<alpha
    android:duration="800"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toAlpha="1.0" />

最后调用此方法

您的活动中.

from your Activity.

Utils.loadImage(YourClassName.this,mImageView,url,R.drawable.ic_user,R.drawable.ic_error);

OR

来自您的片段

from your fragment

Utils.loadImage(getActivity,mImageView,url,R.drawable.ic_user,R.drawable.ic_error);

这篇关于滑行图像有时未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆