Created
March 30, 2015 23:06
-
-
Save cbeyls/af6eea045a02c9b2213d to your computer and use it in GitHub Desktop.
ImageLoader implementation for my own fork of the Cast Companion Library using the Picasso library. See: https://github.com/cbeyls/CastCompanionLibrary-android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.sample.castcompanionlibrary.cast.imageloader; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.drawable.Drawable; | |
import android.util.DisplayMetrics; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.Target; | |
/** | |
* ImageLoader implementation for the Cast Companion Library using the Picasso library. | |
* | |
* @author Christophe Beyls | |
*/ | |
public class PicassoImageLoader implements ImageLoader { | |
private final int mMaxImageSize; | |
private final Picasso mPicasso; | |
private static class PicassoRequest implements Target, ImageLoader.Request { | |
private final String mUrl; | |
private final Callbacks mCallbacks; | |
public PicassoRequest(String url, Callbacks callbacks) { | |
mUrl = url; | |
mCallbacks = callbacks; | |
} | |
@Override | |
public void onPrepareLoad(Drawable placeHolderDrawable) { | |
} | |
@Override | |
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { | |
mCallbacks.onResponse(bitmap); | |
} | |
@Override | |
public void onBitmapFailed(Drawable errorDrawable) { | |
mCallbacks.onResponse(null); | |
} | |
@Override | |
public String getUrl() { | |
return mUrl; | |
} | |
} | |
public PicassoImageLoader(Context context, Picasso picasso) { | |
// Use the device primary screen dimensions as max image size | |
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); | |
mMaxImageSize = Math.max(metrics.widthPixels, metrics.heightPixels); | |
mPicasso = picasso; | |
} | |
@Override | |
public Request load(String url, Callbacks callbacks) { | |
PicassoRequest picassoRequest = new PicassoRequest(url, callbacks); | |
mPicasso.load(url) | |
.resize(mMaxImageSize, mMaxImageSize) | |
.centerInside() | |
.onlyScaleDown() | |
.into(picassoRequest); | |
return picassoRequest; | |
} | |
@Override | |
public void cancelRequest(Request request) { | |
mPicasso.cancelRequest((Target) request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment