Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.koushikdutta.ion;
  
  import  android.content.res.Resources;
  import  android.graphics.Canvas;
  import  android.graphics.Color;
  import  android.graphics.ColorFilter;
  import  android.graphics.Paint;
  import  android.graphics.PixelFormat;
  import  android.graphics.drawable.Drawable;
 import  android.os.Looper;
 import  android.os.SystemClock;
 import  android.text.TextUtils;
 import  android.view.animation.Animation;
 import  android.widget.ImageView;
 
 
Created by koush on 6/8/13.
 
 class IonDrawable extends Drawable {
     private Paint paint;
     private BitmapInfo info;
     private int placeholderResource;
     private Drawable placeholder;
     private int errorResource;
     private Drawable error;
     private Resources resources;
     private int loadedFrom;
     private IonDrawableCallback callback;
     private boolean disableFadeIn;
     private int resizeWidth;
     private int resizeHeight;
 
     public IonDrawable cancel() {
         ++;
         return this;
     }
 
     public SimpleFuture<ImageView> getFuture() {
         return .;
     }
     
     public void setDisableFadeIn(boolean disableFadeIn) {
         this. = disableFadeIn;
     }
 
     public void setInAnimation(Animation inAnimationint inAnimationResource) {
         . = inAnimation;
         . = inAnimationResource;
     }
 
     // create an internal static class that can act as a callback.
     // dont let it hold strong references to anything.
     static class IonDrawableCallback implements FutureCallback<BitmapInfo> {
         private WeakReference<IonDrawableionDrawableRef;
         private WeakReference<ImageView> imageViewRef;
         private String bitmapKey;
         private String parentKey;
         private SimpleFuture<ImageView> imageViewFuture = new SimpleFuture<ImageView>();
         private Animation inAnimation;
         private int inAnimationResource;
         private int requestId;
 
         public IonDrawableCallback(IonDrawable drawable, ImageView imageView) {
              = new WeakReference<IonDrawable>(drawable);
              = new WeakReference<ImageView>(imageView);
         }
 
         @Override
         public void onCompleted(Exception eBitmapInfo result) {
             assert Thread.currentThread() == Looper.getMainLooper().getThread();
             assert result != null;
 
             // see if the imageview is still alive and cares about this result
             ImageView imageView = .get();
             if (imageView == null)
                 return;
 
             IonDrawable drawable = .get();
             if (drawable == null)
                 return;
 
             if (imageView.getDrawable() != drawable)
                 return;
 
             // see if the ImageView is still waiting for the same request
             if (drawable.requestCount != )
                 return;
 
             drawable.requestCount++;
             imageView.setImageDrawable(null);
             drawable.setBitmap(resultresult.loadedFrom);
             imageView.setImageDrawable(drawable);
             IonBitmapRequestBuilder.doAnimation(imageView);
            .setComplete(eimageView);
        }
    }
    int requestCount;
    public void register(Ion ionString bitmapKey) {
        . = ++;
        String previousKey = .;
        if (TextUtils.equals(previousKeybitmapKey))
            return;
        . = bitmapKey;
        ion.bitmapsPending.add(bitmapKey);
        if (previousKey == null)
            return;
        Object owner = ion.bitmapsPending.removeItem(previousKey);
        if (owner instanceof BitmapToBitmapInfo) {
            BitmapToBitmapInfo info = (BitmapToBitmapInfo)owner;
            ion.bitmapsPending.removeItem(info.downloadKeyinfo);
        }
    }
    private static final int DEFAULT_PAINT_FLAGS = Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG;
    public IonDrawable(Resources resources, ImageView imageView) {
        this. = resources;
         = new Paint();
         = new IonDrawableCallback(thisimageView);
    }
    int currentFrame;
    private boolean invalidateScheduled;
    public IonDrawable setBitmap(BitmapInfo infoint loadedFrom) {
        this. = loadedFrom;
        if (this. == info)
            return this;
        invalidateSelf();
        this. = info;
         = 0;
         = false;
        if (info == null) {
            . = null;
            return this;
        }
        . = info.key;
        return this;
    }
    public IonDrawable setSize(int resizeWidthint resizeHeight) {
        if (this. == resizeWidth && this. == resizeHeight)
            return this;
        this. = resizeWidth;
        this. = resizeHeight;
        invalidateSelf();
        return this;
    }
    public IonDrawable setError(int resource, Drawable drawable) {
        if ((drawable != null && drawable == this.) || (resource != 0 && resource == ))
            return this;
        this. = resource;
        this. = drawable;
        invalidateSelf();
        return this;
    }
    public IonDrawable setPlaceholder(int resource, Drawable drawable) {
        if ((drawable != null && drawable == this.) || (resource != 0 && resource == ))
            return this;
        this. = resource;
        this. = drawable;
        invalidateSelf();
        return this;
    }
    @Override
    public void setFilterBitmap(boolean filter) {
        .setFilterBitmap(filter);
        invalidateSelf();
    }
    @Override
    public void setDither(boolean dither) {
        .setDither(dither);
        invalidateSelf();
    }
    @Override
    public int getIntrinsicWidth() {
        if ( != null && . != null)
            return .[0].getScaledWidth(.getDisplayMetrics().densityDpi);
        if ( > 0)
            return ;
        if ( != null)
            return .getIntrinsicWidth();
        if ( != null)
            return .getIntrinsicWidth();
        return -1;
    }
    @Override
    public int getIntrinsicHeight() {
        if ( != null && . != null)
            return .[0].getScaledHeight(.getDisplayMetrics().densityDpi);
        if ( > 0)
            return ;
        if ( != null)
            return .getIntrinsicHeight();
        if ( != null)
            return .getIntrinsicHeight();
        return -1;
    }
    public static final long FADE_DURATION = 200;
    private Runnable invalidate = new Runnable() {
        @Override
        public void run() {
             = false;
            ++;
            invalidateSelf();
        }
    };
    @Override
    public void draw(Canvas canvas) {
        if ( == null) {
            if ( == null &&  != 0)
                 = .getDrawable();
            if ( != null) {
                .setBounds(getBounds());
                .draw(canvas);
            }
            return;
        }
        if (. == 0)
            . = SystemClock.uptimeMillis();
        long destAlpha = 0xFF;
        if(!) {
            destAlpha = ((SystemClock.uptimeMillis() - .) << 8) / ;
            destAlpha = Math.min(destAlpha, 0xFF);
        }
        if (destAlpha != 255) {
            if ( == null &&  != 0)
                 = .getDrawable();
            if ( != null) {
                .setBounds(getBounds());
                .draw(canvas);
            }
        }
        if (. != null) {
            .setAlpha((int)destAlpha);
            canvas.drawBitmap(.[ % ..], nullgetBounds(), );
            .setAlpha(0xFF);
            if (. != null) {
                int delay = .[ % ..];
                if (!) {
                     = true;
                    unscheduleSelf();
                    scheduleSelf(, SystemClock.uptimeMillis() + Math.max(delay, 100));
                }
            }
        }
        else {
            if ( == null &&  != 0)
                 = .getDrawable();
            if ( != null) {
                .setAlpha((int)destAlpha);
                .setBounds(getBounds());
                .draw(canvas);
                .setAlpha(0xFF);
            }
        }
        if (destAlpha != 255)
            invalidateSelf();
        if (true)
            return;
        // stolen from picasso
        canvas.save();
        canvas.rotate(45);
        .setColor(Color.WHITE);
        canvas.drawRect(0, -10, 7.5f, 10, );
        int sourceColor;
        switch () {
            case ..:
                sourceColor = Color.CYAN;
                break;
                sourceColor = Color.YELLOW;
                break;
            case ..:
                sourceColor = Color.GREEN;
                break;
            default:
                sourceColor = Color.RED;
                break;
        }
        .setColor(sourceColor);
        canvas.drawRect(0, -9, 6.5f, 9, );
        canvas.restore();
    }
    @Override
    public void setAlpha(int alpha) {
       .setAlpha(alpha);
    }
    @Override
    public void setColorFilter(ColorFilter cf) {
        .setColorFilter(cf);
    }
    @Override
    public int getOpacity() {
        return ( == null || . == null || .[0].hasAlpha() || .getAlpha() < 255) ?
                PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
    }
    static IonDrawable getOrCreateIonDrawable(ImageView imageView) {
        Drawable current = imageView.getDrawable();
        IonDrawable ret;
        if (current == null || !(current instanceof IonDrawable)) {
            ret = new IonDrawable(imageView.getResources(), imageView);
            imageView.setImageDrawable(ret);
        }
        else {
            ret = (IonDrawable)current;
        }
        // invalidate self doesn't seem to trigger the dimension check to be called by imageview.
        // are drawable dimensions supposed to be immutable?
        imageView.setImageDrawable(null);
        return ret;
    }
New to GrepCode? Check out our FAQ X