Java Code Examples for com.bumptech.glide.RequestBuilder#listener()

The following examples show how to use com.bumptech.glide.RequestBuilder#listener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TinyGifDrawableLoader.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 加载不一定是本地图片资源
 * (即也可以是网络资源)
 * 并且也可以不是gif图片
 * @param context
 * @param model
 * @param iv
 * @param playTimes
 */
public void loadMaybeGifDrawable(Context context,Object model,ImageView iv,int playTimes) {
    if (context == null || iv == null) {
        return;
    }
    iv.setVisibility(View.VISIBLE);
    theDisPlayImageView = new WeakReference<>(iv);
    this.playTimes = playTimes;
    RequestBuilder builder = Glide.with(context.getApplicationContext())
            .asGif()
            ;
    if (loadCallback != null || playTimes >=1) {//指定了播放次数,则需要监听动画执行的结束
        builder.listener(this);
    }
    RequestOptions options = new RequestOptions();
    options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
    builder.apply(options)
            .load(model)
            .into(iv)
    ;
}
 
Example 2
Source File: ImageUtil.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
public static void loadImage(Context context, String picUrl, int newWidth, int newHeight, Drawable holderDrawable,
                             Drawable errorDrawable, ImageView targetIv
        ,  RequestListener callback) {
    RequestBuilder requestBuilder = loadImageRequest(context, picUrl);
    RequestOptions options = new RequestOptions();
    if (newWidth > 0 && newHeight > 0) {
        options.override(newWidth,newHeight).centerCrop();
    }
    else{
        //            loadRequest.fit();
    }
    if (holderDrawable != null) {
        options.placeholder(holderDrawable);
    }
    else{
//        loadRequest.noPlaceholder();
    }
    if (errorDrawable != null) {
        options.error(errorDrawable);
    }
    options.dontAnimate();
    if (callback != null) {
        requestBuilder.listener(callback);
    }
    requestBuilder.apply(options)
            .into(targetIv);
}
 
Example 3
Source File: TinyGifDrawableLoader.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
public void loadGifDrawable(Context context, @RawRes @DrawableRes int gifDrawableResId, ImageView iv, int playTimes) {
        if (context == null || iv == null) {
            return;
        }
        iv.setVisibility(View.VISIBLE);
        theDisPlayImageView = new WeakReference<>(iv);//added by fee 2019-07-08: 将当前要显示的ImageView控件引用起来,但不适用本类用于给不同的ImageView加载
        this.playTimes = playTimes;
        //注:如果不是gif资源,则在asGif()时会抛异常
        RequestBuilder<GifDrawable> requestBuilder =
                Glide.with(context.getApplicationContext())
                        .asGif()
//                        .load(gifDrawableResId)
                ;
        if (
                playTimes >= 1 ||
                loadCallback != null) {//指定了播放次数,则需要监听动画执行的结束
            requestBuilder.listener(this)
            ;
        }
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        requestBuilder.apply(options)
//        listener(this)
                .load(gifDrawableResId)
                .into(iv)
        ;
    }
 
Example 4
Source File: ImageUtil.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
public static void loadGifModel(Context context, Object mayBeGifModel, @RawRes @DrawableRes int defHolderPicRes, ImageView ivTarget, final int needPlayTime) {
    if (mayBeGifModel == null) {
        return;
    }
    RequestBuilder<GifDrawable> gifDrawableBuilder = null;
    try {
          gifDrawableBuilder = Glide.with(context).asGif()
                ;
    } catch (Exception e) {
        //java.lang.IllegalArgumentException You cannot start a load for a destroyed activity
        gifDrawableBuilder = null;
        e.printStackTrace();
    }
    if (gifDrawableBuilder == null) {
        return;
    }
    if (defHolderPicRes != 0) {
        gifDrawableBuilder.placeholder(defHolderPicRes)
                .error(defHolderPicRes);
    }
    RequestListener<GifDrawable> loadGifDrawableListener = null;
    if (needPlayTime >= 1) {
        loadGifDrawableListener = new RequestListener<GifDrawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
                resource.setLoopCount(needPlayTime);
                return false;
            }
        };
        gifDrawableBuilder.listener(loadGifDrawableListener);
    }
    if (mayBeGifModel instanceof Integer) {//还有:load(Bitmap xx);load(byte[]xxx);loadDrawable(Drawable xx);有差异
        Integer gifResId = (Integer) mayBeGifModel;
        if (gifResId != 0) {
            gifDrawableBuilder.load(gifResId);
        }
        else {
            return;
        }
    }
    else{
        gifDrawableBuilder.load(mayBeGifModel);
    }
    gifDrawableBuilder.into(ivTarget);
}