package com.sakebook.android.trunksimplenews.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.sakebook.android.trunksimplenews.network.ImageLoader;

public class AsyncImageView extends ImageView implements LoaderManager.LoaderCallbacks<Bitmap> {

    private String mUrl;

    private ImageLoadCallback mImageLoadCallback;

    public AsyncImageView(Context context) {
        super(context);
    }

    public AsyncImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AsyncImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setImageFromUrl(String url, int id, LoaderManager loaderManager, ImageLoadCallback callback) {
        this.mUrl = url;
        this.mImageLoadCallback = callback;
        loaderManager.initLoader(id, null, this);
    }

    @Override
    public Loader onCreateLoader(int id, Bundle args) {
        ImageLoader loader = new ImageLoader(getContext(), mUrl);
        return loader;
    }

    @Override
    public void onLoadFinished(Loader<Bitmap> loader, Bitmap data) {
        mImageLoadCallback.success(data);
    }

    @Override
    public void onLoaderReset(Loader loader) {

    }
}

@@ -46,16 +46,57 @@
 views;%0A%0A
+import android.animation.ObjectAnimator;%0A
 import a
@@ -170,17 +170,80 @@
 oid.
-os.Bundle
+graphics.Canvas;%0Aimport android.os.Bundle;%0Aimport android.os.SystemClock
 ;%0Aim
@@ -361,16 +361,141 @@
 uteSet;%0A
+import android.util.Log;%0Aimport android.view.animation.AlphaAnimation;%0Aimport android.view.animation.DecelerateInterpolator;%0A
 import a
@@ -586,16 +586,42 @@
 oader;%0A%0A
+import java.util.Random;%0A%0A
 public c
@@ -734,16 +734,89 @@
 ng mUrl;
+%0A    private long startTimeMillis;%0A    private boolean canAnimate = true;
 %0A%0A    pr
@@ -1028,24 +1028,136 @@
 xt, attrs);%0A
+        startTimeMillis = SystemClock.uptimeMillis();%0A        Log.d(%22AsyncImageView%22, %22AsyncImageView init 2%22);%0A
     %7D%0A%0A    p
@@ -1279,16 +1279,74 @@
 eAttr);%0A
+        Log.d(%22AsyncImageView%22, %22AsyncImageView init 3%22);%0A
     %7D%0A%0A 
@@ -1561,22 +1561,1169 @@
 der(
-id, null, this
+mUrl.hashCode(), null, this);%0A    %7D%0A%0A    @Override%0A    public void setImageAlpha(int alpha) %7B%0A        super.setImageAlpha(alpha);%0A    %7D%0A%0A    @Override%0A    protected void onDraw(Canvas canvas) %7B%0A        super.onDraw(canvas);%0A//        if (!canAnimate) %7B%0A//            super.onDraw(canvas);%0A//        %7D else %7B%0A//            float normalized = (SystemClock.uptimeMillis() - startTimeMillis) / 200f;%0A//            Log.d(%22%22, %22normalized: %22 + normalized);%0A//            if (normalized %3E= 1f) %7B%0A//                canAnimate = false;%0A//                super.draw(canvas);%0A//            %7D else %7B%0A//                int partialAlpha = (int) (0xff * normalized);%0A//                super.setImageAlpha(partialAlpha);%0A//                super.draw(canvas);%0A//            %7D%0A//        %7D%0A    %7D%0A%0A    @Override%0A    public void setImageBitmap(Bitmap bm) %7B%0A        super.setImageBitmap(bm);%0A//        AlphaAnimation fadeImage = new AlphaAnimation(0, 1);%0A//        fadeImage.setDuration(1000);%0A//        fadeImage.setInterpolator(new DecelerateInterpolator());%0A//        this.startAnimation(fadeImage);%0A        ObjectAnimator.ofFloat(this, %22alpha%22, 0.0f, 1.0f).setDuration(100).start(
 );%0A 
@@ -3016,16 +3016,22 @@
 ess(data
+, mUrl
 );%0A    %7D

Add image fade in animation