/**
* Copyright 2016 bingoogolapple
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.bingoogolapple.photopicker.imageloader;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import cn.bingoogolapple.photopicker.util.BGAPhotoPickerUtil;
/**
* 作者:王浩 邮件:bingoogolapple@gmail.com
* 创建时间:16/6/25 下午4:40
* 描述:
*/
public class BGAGlideImageLoader extends BGAImageLoader {
@Override
public void display(final ImageView imageView, String path, @DrawableRes int loadingResId, @DrawableRes int failResId, int width, int height, final DisplayDelegate delegate) {
final String finalPath = getPath(path);
Activity activity = getActivity(imageView);
Glide.with(activity).load(finalPath).placeholder(loadingResId).error(failResId).override(width, height).dontAnimate().listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
if (delegate != null) {
delegate.onSuccess(imageView, finalPath);
}
return false;
}
}).into(imageView);
}
@Override
public void download(String path, final DownloadDelegate delegate) {
final String finalPath = getPath(path);
Glide.with(BGAPhotoPickerUtil.sApp).load(finalPath).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (delegate != null) {
delegate.onSuccess(finalPath, resource);
}
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
if (delegate != null) {
delegate.onFailed(finalPath);
}
}
});
}
@Override
public void pause(Activity activity) {
Glide.with(activity).pauseRequests();
}
@Override
public void resume(Activity activity) {
Glide.with(activity).resumeRequestsRecursive();
}
}