Skip to content

Instantly share code, notes, and snippets.

@alashow
Created November 11, 2016 23:17
Show Gist options
  • Save alashow/2e131591994d8499be02a4c0bb648cfb to your computer and use it in GitHub Desktop.
Save alashow/2e131591994d8499be02a4c0bb648cfb to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2016 Alashov Berkeli
* It is licensed under GNU GPL v. 2 or later. For full terms see the file LICENSE.
*/
package tm.asmanoky.aziada.util.glide;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.widget.ImageView;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.ViewPropertyAnimation;
import com.bumptech.glide.request.target.Target;
import java.util.Locale;
import tm.asmanoky.aziada.Config;
import tm.asmanoky.aziada.model.Photo;
import tm.asmanoky.aziada.rest.ApiHelper;
import tm.asmanoky.aziada.util.U;
/**
* Created by alashov on 13/12/15.
*/
public class ImageUtils {
private static final ViewPropertyAnimation.Animator ANIMATOR =
view->{
view.setAlpha(0f);
view.animate().alpha(1f);
};
public static void networkImage(Activity activity, Photo photo, ImageView targetView) {
networkImage(activity, photo, targetView, null);
}
public static void networkImage(Activity activity, Photo photo, ImageView targetView, GlideDrawableListener requestListener) {
networkImage(activity, photo, targetView, requestListener, null);
}
public static void networkImage(Activity activity, Photo photo, ImageView targetView, GlideDrawableListener requestListener, Drawable imagePlaceholder) {
networkImage(activity, photo, targetView, requestListener, null, false, imagePlaceholder);
}
public static void networkImage(Activity activity, Photo photo, ImageView targetView, GlideDrawableListener requestListener, GlideDrawableListener requestListenerThumnail, boolean drawableTarget, Drawable imagePlaceholder) {
if (activity != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
return;
}
networkImage(Glide.with(activity), photo, targetView, requestListener, requestListenerThumnail, drawableTarget, imagePlaceholder);
}
}
public static void networkImage(Fragment fragment, Photo photo, ImageView targetView) {
networkImage(fragment, photo, targetView, null);
}
public static void networkImage(Fragment fragment, Photo photo, ImageView targetView, GlideDrawableListener requestListener) {
networkImage(fragment, photo, targetView, requestListener, null);
}
public static void networkImage(Fragment fragment, Photo photo, ImageView targetView, GlideDrawableListener requestListener, Drawable imagePlaceholder) {
if (fragment != null) {
if (fragment.getActivity() != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && fragment.getActivity().isDestroyed()) {
return;
}
networkImage(Glide.with(fragment), photo, targetView, requestListener, null, false, imagePlaceholder);
}
}
}
public static void networkImage(Context context, Photo photo, ImageView targetView, GlideDrawableListener requestListener) {
networkImage(context, photo, targetView, requestListener, null);
}
public static void networkImage(Context context, Photo photo, ImageView targetView, GlideDrawableListener requestListener, Drawable imagePlaceholder) {
if (context != null) {
if (context instanceof Activity) {
networkImage((Activity) context, photo, targetView, requestListener, imagePlaceholder);
return;
}
networkImage(Glide.with(context), photo, targetView, requestListener, null, false, imagePlaceholder);
}
}
public static void networkImage(RequestManager requestManager, Photo photo, ImageView targetView, GlideDrawableListener requestListener, GlideDrawableListener requestListenerThumbnail, boolean drawableTarget, Drawable imagePlaceholder) {
if (requestManager != null) {
String imageUrl = ! photo.isLoadOnlyThumbnail() ? ApiHelper.getMediaUrl(photo.getImagePath()) : photo.getThumbnailUrl();
DrawableRequestBuilder<String> requestBuilder = requestManager.load(imageUrl);
requestBuilder.dontAnimate();
requestBuilder.diskCacheStrategy(DiskCacheStrategy.ALL);
if (requestListener != null) {
requestBuilder.listener(requestListener);
}
if (imagePlaceholder != null) {
requestBuilder.placeholder(imagePlaceholder);
}
if (photo.isThumbnailEnabled() && ! photo.isForceDisableBlurThumb()) {
photo.setThumbnailWidth(Config.PHOTO_BLUR_WIDTH);
photo.setThumbnailHeight(0);
DrawableRequestBuilder<String> requestBuilderThumbnail = requestManager.load(photo.getThumbnailUrl())
.bitmapTransform(new BlurTransformation(targetView.getContext(), 5, 1))
.animate(ANIMATOR)
.diskCacheStrategy(DiskCacheStrategy.ALL);
if (requestListenerThumbnail != null) {
requestBuilderThumbnail.listener(requestListenerThumbnail);
}
if (imagePlaceholder != null) {
requestBuilderThumbnail.placeholder(imagePlaceholder);
}
requestBuilder.thumbnail(requestBuilderThumbnail);
}
if (drawableTarget) {
requestBuilder.into(new GlideDrawableTarget(targetView));
} else {
requestBuilder.into(targetView);
}
}
}
public static void networkImage(Activity activity, Photo photo, final MollyProgressTarget targetView, GlideDrawableListenerPhoto requestListener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
return;
}
DrawableRequestBuilder<Photo> requestBuilder = Glide.with(activity).load(photo);
targetView.setModel(photo);
requestBuilder.dontAnimate();
requestBuilder.diskCacheStrategy(DiskCacheStrategy.ALL);
if (requestListener != null) {
requestBuilder.listener(requestListener);
}
requestBuilder.placeholder(U.imagePlaceholder());
if (photo.isThumbnailEnabled() && ! photo.isForceDisableBlurThumb()) {
photo.setThumbnailWidth(Config.PHOTO_BLUR_WIDTH);
photo.setThumbnailHeight(0);
DrawableRequestBuilder<String> requestBuilderThumbnail = Glide.with(activity).load(photo.getThumbnailUrl())
.bitmapTransform(new BlurTransformation(targetView.getImage().getContext(), 8, 1))
.animate(ANIMATOR)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.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) {
targetView.setIgnoreNext(true);
return false;
}
});
requestBuilder.thumbnail(requestBuilderThumbnail);
}
requestBuilder.into(targetView);
}
//end networkImage
public static void loadPhotoThumb(Activity activity, Photo photo, final ImageView image, final GlideDrawableListener imageLoadingListener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
return;
}
Glide.with(activity)
.load(photo.getThumbnailUrl())
.dontAnimate()
.placeholder(U.imagePlaceholder())
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.thumbnail(Glide.with(activity)
.load(ApiHelper.getMediaUrl(photo.getImagePath(), Config.PHOTO_BLUR_WIDTH, 0))
.animate(ANIMATOR)
.bitmapTransform(new BlurTransformation(activity, 8, 1))
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if (imageLoadingListener != null) {
imageLoadingListener.onFail(e, model);
}
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
if (imageLoadingListener != null) {
imageLoadingListener.onSuccess(resource, model);
}
return false;
}
}))
.into(new GlideDrawableTarget(image));
}
public static void loadPhotoFull(Activity activity, final Photo photo, final MollyProgressTarget targetView, final GlideDrawableListenerPhoto listener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
return;
}
String thumbUrl = photo.getThumbnailUrl();
targetView.setModel(photo);
Glide.with(activity)
.load(photo)
.dontAnimate()
.placeholder(targetView.getImage().getDrawable())
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.thumbnail(Glide.with(activity)
.load(thumbUrl)
.animate(ANIMATOR)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.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) {
targetView.setIgnoreNext(true);
return false;
}
}))
.listener(new RequestListener<Photo, GlideDrawable>() {
@Override
public boolean onException(Exception e, Photo model, Target<GlideDrawable> target, boolean isFirstResource) {
if (listener != null) {
listener.onFail(e, model);
}
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, Photo model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
if (listener != null) {
listener.onSuccess(resource, model);
}
return false;
}
})
.into(targetView);
}
public static String randomImage(int width, int height) {
if (width <= 0 && height <= 0) {
width = U.randInteger(500, 1000);
height = U.randInteger(500, 1000);
}
return String.format(Locale.ENGLISH, "https://unsplash.it/%d/%d/?random&image=%d", width, height, U.randInteger(1, 911));
}
public static String randomImage() {
return randomImage(0, 0);
}
}
@jt-gilkeson
Copy link

Here's a backward compatible isDestroyed that you may find useful:

public static boolean isDestroyed(Activity activity)
{
	if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1)
	{
		return activity.isDestroyed();
	}

	return activity.isFinishing();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment