All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.koushikdutta.ion.TransformBitmap Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package com.koushikdutta.ion;

import android.graphics.Bitmap;

import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.bitmap.PostProcess;
import com.koushikdutta.ion.bitmap.Transform;

import java.util.ArrayList;

class TransformBitmap extends BitmapCallback implements FutureCallback {
    static class PostProcessNullTransform implements Transform {
        String key;
        public PostProcessNullTransform(String key) {
            this.key = key;
        }

        @Override
        public Bitmap transform(Bitmap b) {
            return b;
        }

        @Override
        public String key() {
            return key;
        }
    }

    ArrayList transforms;
    ArrayList postProcess;

    String downloadKey;
    public TransformBitmap(Ion ion, String transformKey, String downloadKey, ArrayList transforms, ArrayList postProcess) {
        super(ion, transformKey, true);
        this.transforms = transforms;
        this.downloadKey = downloadKey;
        this.postProcess = postProcess;
    }

    @Override
    public void onCompleted(Exception e, final BitmapInfo result) {
        if (e != null) {
            report(e, null);
            return;
        }

        if (ion.bitmapsPending.tag(key) != this) {
//            Log.d("IonBitmapLoader", "Bitmap transform cancelled (no longer needed)");
            return;
        }

        Ion.getBitmapLoadExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                if (ion.bitmapsPending.tag(key) != TransformBitmap.this) {
//            Log.d("IonBitmapLoader", "Bitmap transform cancelled (no longer needed)");
                    return;
                }

                BitmapInfo info;
                try {
                    Bitmap bitmap = result.bitmap;
                    for (Transform transform : transforms) {
                        bitmap = transform.transform(bitmap);
                        if (bitmap == null)
                            throw new Exception("failed to transform bitmap");
                    }
                    info = new BitmapInfo(key, result.mimeType, bitmap, result.originalSize);
                    info.loadedFrom = result.loadedFrom;

                    if (postProcess != null) {
                        for (PostProcess p: postProcess) {
                            p.postProcess(info);
                        }
                    }

                    report(null, info);
                }
                catch (OutOfMemoryError e) {
                    report(new Exception(e), null);
                }
                catch (Exception e) {
                    report(e, null);
                }
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy