use of com.koushikdutta.ion.gif.GifFrame in project ion by koush.
the class IonDrawable method draw.
@Override
public void draw(Canvas canvas) {
if (info == null) {
// draw stuff
super.draw(canvas);
// see if we can fetch a bitmap
if (bitmapFetcher != null) {
if (bitmapFetcher.sampleWidth == 0 && bitmapFetcher.sampleHeight == 0) {
if (canvas.getWidth() != 1)
bitmapFetcher.sampleWidth = canvas.getWidth();
if (canvas.getHeight() != 1)
bitmapFetcher.sampleHeight = canvas.getHeight();
// now that we have final dimensions, reattempt to find the image in the cache
bitmapFetcher.recomputeDecodeKey();
BitmapInfo found = ion.bitmapCache.get(bitmapFetcher.bitmapKey);
if (found != null) {
// won't be needing THIS anymore
bitmapFetcher = null;
// found what we're looking for, but can't draw at this very moment,
// since we need to trigger a new measure.
callback.onCompleted(null, found);
return;
}
}
// no image found fetch it.
callback.register(ion, bitmapFetcher.bitmapKey);
// already in progress
if (BitmapFetcher.shouldDeferImageView(ion)) {
bitmapFetcher.defer();
} else {
bitmapFetcher.execute();
}
// won't be needing THIS anymore
bitmapFetcher = null;
}
// well, can't do anything else here.
return;
}
if (info.decoder != null) {
drawDeepZoom(canvas);
return;
}
if (info.drawTime == 0)
info.drawTime = SystemClock.uptimeMillis();
long destAlpha = this.alpha;
if (fadeIn) {
destAlpha = ((SystemClock.uptimeMillis() - info.drawTime) << 8) / FADE_DURATION;
destAlpha = Math.min(destAlpha, this.alpha);
}
// remove plaeholder if not visible
if (destAlpha == this.alpha) {
if (placeholder != null) {
placeholder = null;
setDrawableByLayerId(0, NULL_PLACEHOLDER);
}
} else {
// invalidate to fade in
if (placeholder != null)
invalidateSelf();
}
if (info.gifDecoder != null) {
super.draw(canvas);
GifFrame frame = gifDecoder.getCurrentFrame();
if (frame != null) {
paint.setAlpha((int) destAlpha);
canvas.drawBitmap(frame.image, null, getBounds(), paint);
paint.setAlpha(this.alpha);
invalidateSelf();
}
return;
}
if (info.bitmap != null) {
if (bitmapDrawable != null)
bitmapDrawable.setAlpha((int) destAlpha);
} else {
if (error != null)
error.setAlpha((int) destAlpha);
}
super.draw(canvas);
if (true)
return;
// stolen from picasso
canvas.save();
canvas.rotate(45);
paint.setColor(Color.WHITE);
canvas.drawRect(0, -10, 7.5f, 10, paint);
int sourceColor;
if (servedFrom == ResponseServedFrom.LOADED_FROM_CACHE)
sourceColor = Color.CYAN;
else if (servedFrom == ResponseServedFrom.LOADED_FROM_CONDITIONAL_CACHE)
sourceColor = Color.YELLOW;
else if (servedFrom == ResponseServedFrom.LOADED_FROM_MEMORY)
sourceColor = Color.GREEN;
else
sourceColor = Color.RED;
paint.setColor(sourceColor);
canvas.drawRect(0, -9, 6.5f, 9, paint);
canvas.restore();
}
use of com.koushikdutta.ion.gif.GifFrame in project ion by koush.
the class GifTests method testGif.
public void testGif() throws Exception {
byte[] bytes = Ion.with(getContext()).load("https://raw2.github.com/koush/ion/master/ion-sample/mark.gif").asByteArray().get();
GifDecoder decoder = new GifDecoder(bytes);
GifFrame frame = decoder.nextFrame();
frame = decoder.nextFrame();
}
use of com.koushikdutta.ion.gif.GifFrame in project ion by koush.
the class StreamLoader method loadGif.
protected BitmapInfo loadGif(String key, Point size, InputStream in, BitmapFactory.Options options) throws Exception {
GifDecoder gifDecoder = new GifDecoder(ByteBuffer.wrap(StreamUtility.readToEndAsArray(in)));
GifFrame frame = gifDecoder.nextFrame();
BitmapInfo info = new BitmapInfo(key, options.outMimeType, frame.image, size);
info.gifDecoder = gifDecoder;
return info;
}
use of com.koushikdutta.ion.gif.GifFrame in project ion by koush.
the class LoadBitmap method onCompleted.
@Override
public void onCompleted(Exception e, final Response<ByteBufferList> response) {
if (e == null)
e = response.getException();
if (e != null) {
report(e, null);
return;
}
final ByteBufferList result = response.getResult();
if (ion.bitmapsPending.tag(key) != this) {
result.recycle();
return;
}
Ion.getBitmapLoadExecutorService().execute(new Runnable() {
@Override
public void run() {
if (ion.bitmapsPending.tag(key) != LoadBitmap.this) {
result.recycle();
return;
}
ByteBuffer bb = null;
try {
bb = result.getAll();
Bitmap bitmap;
GifDecoder gifDecoder;
BitmapFactory.Options options = ion.bitmapCache.prepareBitmapOptions(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), resizeWidth, resizeHeight);
final Point size = new Point(options.outWidth, options.outHeight);
if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
// new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
gifDecoder = new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
GifFrame frame = gifDecoder.nextFrame();
bitmap = frame.image;
// the byte buffer is needed by the decoder
bb = null;
} else {
bitmap = IonBitmapCache.loadBitmap(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), options);
gifDecoder = null;
if (bitmap == null)
throw new Exception("failed to load bitmap");
}
BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
info.gifDecoder = gifDecoder;
info.servedFrom = response.getServedFrom();
report(null, info);
} catch (OutOfMemoryError e) {
report(new Exception(e), null);
} catch (Exception e) {
report(e, null);
} finally {
ByteBufferList.reclaim(bb);
}
}
});
}
use of com.koushikdutta.ion.gif.GifFrame in project ion by koush.
the class LoadDeepZoom method onCompleted.
@Override
public void onCompleted(Exception e, final Response<File> response) {
if (e == null)
e = response.getException();
if (e != null) {
report(e, null);
return;
}
final File tempFile = response.getResult();
if (ion.bitmapsPending.tag(key) != this) {
// Log.d("IonBitmapLoader", "Bitmap load cancelled (no longer needed)");
return;
}
Ion.getBitmapLoadExecutorService().execute(new Runnable() {
@Override
public void run() {
FileInputStream fin = null;
try {
File file;
// file cache will be null if the file is on the local file system already
if (fileCache != null) {
fileCache.commitTempFiles(key, tempFile);
file = fileCache.getFile(key);
} else {
// local file system, use the "temp" file as the source.
file = tempFile;
}
BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(file, 0, 0);
final Point size = new Point(options.outWidth, options.outHeight);
if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
fin = fileCache.get(key);
GifDecoder gifDecoder = new GifDecoder(ByteBuffer.wrap(StreamUtility.readToEndAsArray(fin)));
GifFrame frame = gifDecoder.nextFrame();
BitmapInfo info = new BitmapInfo(key, options.outMimeType, frame.image, size);
info.gifDecoder = gifDecoder;
report(null, info);
return;
}
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(file.toString(), false);
Bitmap bitmap = decoder.decodeRegion(new Rect(0, 0, size.x, size.y), options);
if (bitmap == null)
throw new Exception("unable to load decoder");
BitmapInfo info = new BitmapInfo(key, options.outMimeType, bitmap, size);
info.decoder = decoder;
info.decoderFile = file;
info.servedFrom = response.getServedFrom();
report(null, info);
} catch (Exception e) {
report(e, null);
} finally {
StreamUtility.closeQuietly(fin);
}
}
});
}
Aggregations