1. 程式人生 > >Android圖片載入框架最全解析(四),玩轉Glide的回撥與監聽(筆記)

Android圖片載入框架最全解析(四),玩轉Glide的回撥與監聽(筆記)

參考原文:Android圖片載入框架最全解析(四),玩轉Glide的回撥與監聽

回撥的原始碼實現

的Target物件傳入到GenericRequest當中,而Glide在圖片載入完成之後又會回撥GenericRequest的onResourceReady()方法,onResourceReady()方法中處理了圖片展示,還有GIF播放的邏輯,那麼一張圖片也就顯示出來了

into()方法

通常只需要在兩種Target的基礎上去自定義就可以了,一種是SimpleTarget,一種是ViewTarget。
SimpleTarget

SimpleTarget<
GlideDrawable>
simpleTarget = new SimpleTarget<GlideDrawable>() { @Override public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) { imageView.setImageDrawable(resource); } }; public void loadImage(View view) { String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg"
; Glide.with(this) .load(url) .into(simpleTarget); }

viewTarget

public class MyLayout extends LinearLayout {

    private ViewTarget<MyLayout, GlideDrawable> viewTarget;

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        viewTarget =
new ViewTarget<MyLayout, GlideDrawable>(this) { @Override public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) { MyLayout myLayout = getView(); myLayout.setImageAsBackground(resource); } }; } public ViewTarget<MyLayout, GlideDrawable> getTarget() { return viewTarget; } public void setImageAsBackground(GlideDrawable resource) { setBackground(resource); } }
public class MainActivity extends AppCompatActivity {

    MyLayout myLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myLayout = (MyLayout) findViewById(R.id.background);
    }

    public void loadImage(View view) {
        String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
        Glide.with(this)
             .load(url)
             .into(myLayout.getTarget());
    }

}

preload()方法

Glide載入圖片雖說非常智慧,它會自動判斷該圖片是否已經有快取了,如果有的話就直接從快取中讀取,沒有的話再從網路去下載。但是如果我希望提前對圖片進行一個預載入,等真正需要載入圖片的時候就直接從快取中讀取,不想再等待慢長的網路載入時間了,這該怎麼辦呢?

Glide.with(this)
     .load(url)
     .diskCacheStrategy(DiskCacheStrategy.SOURCE)
     .preload();

PreloadTarget的原始碼非常簡單,obtain()方法中就是new了一個PreloadTarget的例項而已,而onResourceReady()方法中也沒做什麼事情,只是呼叫了Glide.clear()方法。
這裡的Glide.clear()並不是清空快取的意思,而是表示載入已完成,釋放資源的意思,因此不用在這裡產生疑惑。

downloadOnly()方法

downloadOnly(int width, int height)
downloadOnly(Y target)

public void downloadImage(View view) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
                final Context context = getApplicationContext();
                FutureTarget<File> target = Glide.with(context)
                                                 .load(url)
                                                 .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
                final File imageFile = target.get();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, imageFile.getPath(), Toast.LENGTH_LONG).show();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}
public void loadImage(View view) {
    String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
    Glide.with(this)
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(imageView);
}

這樣一個最簡單的DownloadImageTarget就定義好了,使用它也非常的簡單,我們不用再考慮什麼執行緒的問題了,而是直接把它的例項傳入downloadOnly(Y target)方法中即可,如下所示:

public void downloadImage(View view) {
    String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
    Glide.with(this)
            .load(url)
            .downloadOnly(new DownloadImageTarget());
}

listener()方法

public void loadImage(View view) {
    String url = "http://cn.bing.com/az/hprichbg/rb/TOAD_ZH-CN7336795473_1920x1080.jpg";
    Glide.with(this)
            .load(url)
            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target,
                    boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model,
                    Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    return false;
                }
            })
            .into(imageView);
}
public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallback,
        ResourceCallback {

    private RequestListener<? super A, R> requestListener;
    ...

    private void onResourceReady(Resource<?> resource, R result) {
        boolean isFirstResource = isFirstReadyResource();
        status = Status.COMPLETE;
        this.resource = resource;
        if (requestListener == null || !requestListener.onResourceReady(result, model, target,
                loadedFromMemoryCache, isFirstResource)) {
            GlideAnimation<R> animation = animationFactory.build(loadedFromMemoryCache, isFirstResource);
            target.onResourceReady(result, animation);
        }
        notifyLoadSuccess();
    }
    ...
}