Skip to content

Instantly share code, notes, and snippets.

@gokhanbarisaker
Last active September 1, 2016 15:17
Show Gist options
  • Save gokhanbarisaker/c116bea7fc10e6eac25d to your computer and use it in GitHub Desktop.
Save gokhanbarisaker/c116bea7fc10e6eac25d to your computer and use it in GitHub Desktop.
Picasso decoder for subsampling-scale-image-view
/**
* Created by gokhanbarisaker on 8/30/15.
*/
public class PicassoDecoder implements ImageDecoder
{
private String tag;
private Picasso picasso;
public PicassoDecoder(String tag, Picasso picasso) {
this.tag = tag;
this.picasso = picasso;
}
@Override
public Bitmap decode(Context context, Uri uri) throws Exception {
return picasso
.load(uri)
.tag(tag)
.config(Bitmap.Config.ARGB_8888)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.get();
}
}
/**
* Created by gokhanbarisaker on 8/30/15.
*/
public class PicassoRegionDecoder implements ImageRegionDecoder {
private OkHttpClient client;
private BitmapRegionDecoder decoder;
private final Object decoderLock = new Object();
public PicassoRegionDecoder (OkHttpClient client) {
this.client = client;
}
@Override
public Point init(Context context, Uri uri) throws Exception {
OkHttpDownloader downloader = new OkHttpDownloader(client);
InputStream inputStream = downloader.load(uri, 0).getInputStream();
this.decoder = BitmapRegionDecoder.newInstance(inputStream, false);
return new Point(this.decoder.getWidth(), this.decoder.getHeight());
}
@Override
public Bitmap decodeRegion(Rect rect, int sampleSize) {
synchronized(this.decoderLock) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleSize;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = this.decoder.decodeRegion(rect, options);
if(bitmap == null) {
throw new RuntimeException("Region decoder returned null bitmap - image format may not be supported");
} else {
return bitmap;
}
}
}
@Override
public boolean isReady() {
return this.decoder != null && !this.decoder.isRecycled();
}
@Override
public void recycle() {
this.decoder.recycle();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment