// Path: imageloadpk/src/main/java/com/example/imageloadpk/adapter/watcher/Drawables.java
// public class Drawables {
//     public static Drawable sPlaceholderDrawable;
//     public static Drawable sErrorDrawable;
// 
//     private Drawables() {
//     }
// 
//     public static void init(final Resources resources) {
//         if (sPlaceholderDrawable == null) {
//             sPlaceholderDrawable = resources.getDrawable(R.drawable.placeholder, null);
//         }
//         if (sErrorDrawable == null) {
//             sErrorDrawable = resources.getDrawable(R.drawable.error, null);
//         }
//     }
// }
// 
// Path: imageloadpk/src/main/java/com/example/imageloadpk/adapter/watcher/WatchImageView.java
// public class WatchImageView extends ImageView implements WatchInterface {
//     private final WatchImpl mWatcher;
// 
//     public WatchImageView(Context context) {
//         super(context);
//         //设置图片填充样式为按比例填满控件
//         setScaleType(ScaleType.CENTER_CROP);
//         mWatcher = new WatchImpl(this);
//     }
// 
// 
//     @Override
//     protected void onDraw(Canvas canvas) {
//         super.onDraw(canvas);
//         //绘制结果会用到，通过mWatcher转发
//         mWatcher.onDraw(canvas);
//     }
// 
// //    禁用这个方法，防止框架使用它显示图片，影响我们测试显示
//     @Override
//     public void setImageURI(Uri uri) {
//         throw new UnsupportedOperationException();
//     }
// 
//     @Override
//     public void initWatcher(String tag, WatchListener watchListener) {
//         mWatcher.init(tag, watchListener);
//         mWatcher.onStart(); //初始化
//     }
// 
//     @Override
//     public void setImageDrawable(Drawable drawable) {
// 
//         Preconditions.checkNotNull(drawable);
//         if (drawable == Drawables.sPlaceholderDrawable) {
//             //不再这里调用，在初始化加载器，Glide.build()实现调用更好
//         } else if (drawable == Drawables.sErrorDrawable) {
//             mWatcher.onFailure();//加载失败
//         } else {
//             mWatcher.onSuccess();//加载成功
//         }
//         super.setImageDrawable(drawable);
//     }
// }
// 
// Path: imageloadpk/src/main/java/com/example/imageloadpk/adapter/watcher/WatchListener.java
// public class WatchListener {
//     private long mRequestTotalTime;
//     private long mStartRequests;
//     private long mSuccessedRequests;
//     private long mFailedRequests;
//     private long mCancelledRequests;
// 
//     public void reportSuccess(long requestTime) {
//         mRequestTotalTime += requestTime;
//         mSuccessedRequests++;
//     }
// 
//     public void initData() {
//         mRequestTotalTime = 0;
//         mStartRequests = 0;
//         mSuccessedRequests = 0;
//         mFailedRequests = 0;
//         mCancelledRequests = 0;
//     }
// 
//     public void reportStart() {
//         mStartRequests++;
//     }
// 
//     public void reportCancelltion(long requestTime) {
//         mCancelledRequests++;
//         mRequestTotalTime += requestTime;
//     }
// 
//     public void reportFailure(long requestTime) {
//         mFailedRequests++;
//         mRequestTotalTime += requestTime;
//     }
// 
// 
//     public long getAverageRequestTime() {
//         final long completedRequests = getCompletedRequests();
//         return completedRequests > 0 ? mRequestTotalTime / completedRequests : 0;
//     }
// 
//     public long getCancelledRequests() {
//         return mCancelledRequests;
//     }
// 
//     public long getCompletedRequests() {
//         return mSuccessedRequests + mCancelledRequests + mFailedRequests;
//     }
// 
//     public long getTotalRequests() {
//         return mStartRequests;
//     }
// }


import android.content.Context;
import android.util.Log;
import android.view.View;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.imageloadpk.adapter.watcher.Drawables;
import com.example.imageloadpk.adapter.watcher.WatchImageView;
import com.example.imageloadpk.adapter.watcher.WatchListener;
package com.example.imageloadpk.adapter.holders;



/**
 * Created by Nevermore on 16/7/3.
 */
public class GlideHolder extends BaseHolder<WatchImageView> {

    public GlideHolder(WatchImageView imageView, WatchListener watchListener, View parentView, Context context) {
        super(imageView, watchListener, parentView, context);
    }

    public final static String TAG = "GlideHolder";
    private RequestListener<String, GlideDrawable> mRequestListener = new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            Log.w(TAG, "onException: ", e);
            Log.d(TAG, "onException: " + model);
            Log.d(TAG, "onException: " + target.getRequest().isRunning());
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }
    };

    @Override
    protected void onBind(String url) {
        Glide.with(getContext())
                .load(url)
                .listener(mRequestListener)//配置监听器
// Path: imageloadpk/src/main/java/com/example/imageloadpk/adapter/watcher/Drawables.java
// public class Drawables {
//     public static Drawable sPlaceholderDrawable;
//     public static Drawable sErrorDrawable;
// 
//     private Drawables() {
//     }
// 
//     public static void init(final Resources resources) {
//         if (sPlaceholderDrawable == null) {
//             sPlaceholderDrawable = resources.getDrawable(R.drawable.placeholder, null);
//         }
//         if (sErrorDrawable == null) {
//             sErrorDrawable = resources.getDrawable(R.drawable.error, null);
//         }
//     }
// }
// 
// Path: imageloadpk/src/main/java/com/example/imageloadpk/adapter/watcher/WatchImageView.java
// public class WatchImageView extends ImageView implements WatchInterface {
//     private final WatchImpl mWatcher;
// 
//     public WatchImageView(Context context) {
//         super(context);
//         //设置图片填充样式为按比例填满控件
//         setScaleType(ScaleType.CENTER_CROP);
//         mWatcher = new WatchImpl(this);
//     }
// 
// 
//     @Override
//     protected void onDraw(Canvas canvas) {
//         super.onDraw(canvas);
//         //绘制结果会用到，通过mWatcher转发
//         mWatcher.onDraw(canvas);
//     }
// 
// //    禁用这个方法，防止框架使用它显示图片，影响我们测试显示
//     @Override
//     public void setImageURI(Uri uri) {
//         throw new UnsupportedOperationException();
//     }
// 
//     @Override
//     public void initWatcher(String tag, WatchListener watchListener) {
//         mWatcher.init(tag, watchListener);
//         mWatcher.onStart(); //初始化
//     }
// 
//     @Override
//     public void setImageDrawable(Drawable drawable) {
// 
//         Preconditions.checkNotNull(drawable);
//         if (drawable == Drawables.sPlaceholderDrawable) {
//             //不再这里调用，在初始化加载器，Glide.build()实现调用更好
//         } else if (drawable == Drawables.sErrorDrawable) {
//             mWatcher.onFailure();//加载失败
//         } else {
//             mWatcher.onSuccess();//加载成功
//         }
//         super.setImageDrawable(drawable);
//     }
// }
// 
// Path: imageloadpk/src/main/java/com/example/imageloadpk/adapter/watcher/WatchListener.java
// public class WatchListener {
//     private long mRequestTotalTime;
//     private long mStartRequests;
//     private long mSuccessedRequests;
//     private long mFailedRequests;
//     private long mCancelledRequests;
// 
//     public void reportSuccess(long requestTime) {
//         mRequestTotalTime += requestTime;
//         mSuccessedRequests++;
//     }
// 
//     public void initData() {
//         mRequestTotalTime = 0;
//         mStartRequests = 0;
//         mSuccessedRequests = 0;
//         mFailedRequests = 0;
//         mCancelledRequests = 0;
//     }
// 
//     public void reportStart() {
//         mStartRequests++;
//     }
// 
//     public void reportCancelltion(long requestTime) {
//         mCancelledRequests++;
//         mRequestTotalTime += requestTime;
//     }
// 
//     public void reportFailure(long requestTime) {
//         mFailedRequests++;
//         mRequestTotalTime += requestTime;
//     }
// 
// 
//     public long getAverageRequestTime() {
//         final long completedRequests = getCompletedRequests();
//         return completedRequests > 0 ? mRequestTotalTime / completedRequests : 0;
//     }
// 
//     public long getCancelledRequests() {
//         return mCancelledRequests;
//     }
// 
//     public long getCompletedRequests() {
//         return mSuccessedRequests + mCancelledRequests + mFailedRequests;
//     }
// 
//     public long getTotalRequests() {
//         return mStartRequests;
//     }
// }


// Path: imageloadpk/src/main/java/com/example/imageloadpk/adapter/holders/GlideHolder.java
import android.content.Context;
import android.util.Log;
import android.view.View;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.imageloadpk.adapter.watcher.Drawables;
import com.example.imageloadpk.adapter.watcher.WatchImageView;
import com.example.imageloadpk.adapter.watcher.WatchListener;

package com.example.imageloadpk.adapter.holders;



/**
 * Created by Nevermore on 16/7/3.
 */
public class GlideHolder extends BaseHolder<WatchImageView> {

    public GlideHolder(WatchImageView imageView, WatchListener watchListener, View parentView, Context context) {
        super(imageView, watchListener, parentView, context);
    }

    public final static String TAG = "GlideHolder";
    private RequestListener<String, GlideDrawable> mRequestListener = new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            Log.w(TAG, "onException: ", e);
            Log.d(TAG, "onException: " + model);
            Log.d(TAG, "onException: " + target.getRequest().isRunning());
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }
    };

    @Override
    protected void onBind(String url) {
        Glide.with(getContext())
                .load(url)
                .listener(mRequestListener)//配置监听器
                .placeholder(Drawables.sPlaceholderDrawable)