Search in sources :

Example 6 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class StreamLoader method loadBitmap.

@Override
public Future<BitmapInfo> loadBitmap(final Context context, final Ion ion, final String key, final String uri, final int resizeWidth, final int resizeHeight, final boolean animateGif) {
    final SimpleFuture<BitmapInfo> ret = new SimpleFuture<BitmapInfo>();
    // Log.d("FileLoader", "Loading file bitmap " + uri + " " + resizeWidth + "," + resizeHeight);
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            InputStream in = null;
            try {
                in = getInputStream(context, uri);
                BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(in, resizeWidth, resizeHeight);
                StreamUtility.closeQuietly(in);
                Point size = new Point(options.outWidth, options.outHeight);
                BitmapInfo info;
                in = getInputStream(context, uri);
                if (animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
                    info = loadGif(key, size, in, options);
                } else {
                    Bitmap bitmap = IonBitmapCache.loadBitmap(in, options);
                    if (bitmap == null)
                        throw new Exception("Bitmap failed to load");
                    info = new BitmapInfo(key, options.outMimeType, bitmap, size);
                }
                info.servedFrom = ResponseServedFrom.LOADED_FROM_CACHE;
                ret.setComplete(info);
            } catch (OutOfMemoryError e) {
                ret.setComplete(new Exception(e), null);
            } catch (Exception e) {
                ret.setComplete(e);
            } finally {
                StreamUtility.closeQuietly(in);
            }
        }
    });
    return ret;
}
Also used : Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) Point(android.graphics.Point) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 7 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class PackageIconLoader method loadBitmap.

@Override
public Future<BitmapInfo> loadBitmap(Context context, final Ion ion, final String key, final String uri, int resizeWidth, int resizeHeight, boolean animateGif) {
    if (uri == null || !uri.startsWith("package:"))
        return null;
    final SimpleFuture<BitmapInfo> ret = new SimpleFuture<BitmapInfo>();
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            try {
                final URI request = URI.create(uri);
                String pkg = request.getHost();
                PackageManager pm = ion.getContext().getPackageManager();
                Bitmap bmp = ((BitmapDrawable) pm.getPackageInfo(pkg, 0).applicationInfo.loadIcon(pm)).getBitmap();
                if (bmp == null)
                    throw new Exception("package icon failed to load");
                BitmapInfo info = new BitmapInfo(key, null, bmp, new Point(bmp.getWidth(), bmp.getHeight()));
                info.servedFrom = ResponseServedFrom.LOADED_FROM_CACHE;
                ret.setComplete(info);
            } catch (Exception e) {
                ret.setComplete(e);
            }
        }
    });
    return ret;
}
Also used : Bitmap(android.graphics.Bitmap) PackageManager(android.content.pm.PackageManager) Point(android.graphics.Point) URI(java.net.URI) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 8 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class VideoLoader method loadBitmap.

@Override
public Future<BitmapInfo> loadBitmap(Context context, Ion ion, final String key, final String uri, final int resizeWidth, final int resizeHeight, boolean animateGif) {
    if (!uri.startsWith(ContentResolver.SCHEME_FILE))
        return null;
    final MediaFile.MediaFileType type = MediaFile.getFileType(uri);
    if (type == null || !MediaFile.isVideoFileType(type.fileType))
        return null;
    final SimpleFuture<BitmapInfo> ret = new SimpleFuture<BitmapInfo>();
    Ion.getBitmapLoadExecutorService().execute(new Runnable() {

        @Override
        public void run() {
            final File file = new File(URI.create(uri));
            if (ret.isCancelled()) {
                // Log.d("VideoLoader", "Bitmap load cancelled (no longer needed)");
                return;
            }
            try {
                Bitmap bmp;
                if (mustUseThumbnailUtils() || Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD_MR1)
                    bmp = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
                else
                    bmp = createVideoThumbnail(file.getAbsolutePath());
                if (bmp == null)
                    throw new Exception("video bitmap failed to load");
                // downsample this if its obscenely large
                Point originalSize = new Point(bmp.getWidth(), bmp.getHeight());
                if (bmp.getWidth() > resizeWidth * 2 && bmp.getHeight() > resizeHeight * 2) {
                    float xratio = (float) resizeWidth / bmp.getWidth();
                    float yratio = (float) resizeHeight / bmp.getHeight();
                    float ratio = Math.min(xratio, yratio);
                    if (ratio != 0)
                        bmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() * ratio), (int) (bmp.getHeight() * ratio), true);
                }
                BitmapInfo info = new BitmapInfo(key, type.mimeType, bmp, originalSize);
                info.servedFrom = ResponseServedFrom.LOADED_FROM_CACHE;
                ret.setComplete(info);
            } catch (OutOfMemoryError e) {
                ret.setComplete(new Exception(e));
            } catch (Exception e) {
                ret.setComplete(e);
            }
        }
    });
    return ret;
}
Also used : Bitmap(android.graphics.Bitmap) Point(android.graphics.Point) File(java.io.File) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo) SimpleFuture(com.koushikdutta.async.future.SimpleFuture)

Example 9 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class LollipopTransition method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lollipop_list);
    ImageView[] ivs = new ImageView[] { (ImageView) findViewById(R.id.one), (ImageView) findViewById(R.id.two), (ImageView) findViewById(R.id.three), (ImageView) findViewById(R.id.four) };
    for (int i = 1; i <= ivs.length; i++) {
        final int thumb = i;
        ImageView iv = ivs[i - 1];
        Ion.with(iv).centerCrop().load("https://raw.githubusercontent.com/koush/SampleImages/master/" + i + ".thumb.jpg");
        iv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ImageView iv = (ImageView) v;
                BitmapInfo bi = Ion.with(iv).getBitmapInfo();
                Intent intent = new Intent(LollipopTransition.this, LollipopTransitionFullscreen.class);
                intent.putExtra("bitmapInfo", bi.key);
                intent.putExtra("thumb", thumb);
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(LollipopTransition.this, iv, "photo_hero").toBundle());
            }
        });
    }
}
Also used : Intent(android.content.Intent) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) View(android.view.View) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Example 10 with BitmapInfo

use of com.koushikdutta.ion.bitmap.BitmapInfo in project ion by koush.

the class LollipopTransitionFullscreen method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lollipop_fullscreen);
    final ImageView iv = (ImageView) findViewById(R.id.image);
    String bitmapKey = getIntent().getStringExtra("bitmapInfo");
    BitmapInfo bi = Ion.getDefault(this).getBitmapCache().get(bitmapKey);
    iv.setImageBitmap(bi.bitmap);
    final int thumb = getIntent().getIntExtra("thumb", 1);
    getWindow().getEnterTransition().addListener(new Transition.TransitionListener() {

        @Override
        public void onTransitionStart(Transition transition) {
        }

        @Override
        public void onTransitionCancel(Transition transition) {
        }

        @Override
        public void onTransitionPause(Transition transition) {
        }

        @Override
        public void onTransitionResume(Transition transition) {
        }

        @Override
        public void onTransitionEnd(Transition transition) {
            getWindow().getEnterTransition().removeListener(this);
            // load the full version, crossfading from the thumbnail image
            Ion.with(iv).crossfade(true).load("https://raw.githubusercontent.com/koush/SampleImages/master/" + thumb + ".jpg");
        }
    });
}
Also used : Transition(android.transition.Transition) ImageView(android.widget.ImageView) BitmapInfo(com.koushikdutta.ion.bitmap.BitmapInfo)

Aggregations

BitmapInfo (com.koushikdutta.ion.bitmap.BitmapInfo)19 Bitmap (android.graphics.Bitmap)8 Point (android.graphics.Point)8 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)5 File (java.io.File)5 GifFrame (com.koushikdutta.ion.gif.GifFrame)4 ImageView (android.widget.ImageView)3 GifDecoder (com.koushikdutta.ion.gif.GifDecoder)3 Paint (android.graphics.Paint)2 Rect (android.graphics.Rect)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 CancellationException (java.util.concurrent.CancellationException)2 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 BitmapRegionDecoder (android.graphics.BitmapRegionDecoder)1 Transition (android.transition.Transition)1 View (android.view.View)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 FutureCallback (com.koushikdutta.async.future.FutureCallback)1