1. 程式人生 > >Android之Glide獲取圖片Path和Glide獲取圖片Bitmap

Android之Glide獲取圖片Path和Glide獲取圖片Bitmap

今天主要研究了Glide獲取圖片Path、Bitmap用法,相信也困擾了大家很久,我在網上也找了很久,基本沒有,後來研究了下,也參考了下api文件,總結了以下幾個方式:

  1. 獲取Bitmap:
    1)在圖片下載快取好之後獲取
Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    image.setImageBitmap(resource);
                }
            }); //方法中設定asBitmap可以設定回撥型別

上面是簡單方法,下面有全面的方法,可以完美控制:

Glide.with(mContext).load(url).asBitmap().into(new Target<Bitmap>() {
                @Override
                public void onLoadStarted(Drawable placeholder) {
                    
                }
 
                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {
 
                }
 
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                     //TODO set bitmap
                }
 
                @Override
                public void onLoadCleared(Drawable placeholder) {
 
                }
 
                @Override
                public void getSize(SizeReadyCallback cb) {
 
                }
 
                @Override
                public void setRequest(Request request) {
 
                }
 
                @Override
                public Request getRequest() {
                    return null;
                }
 
                @Override
                public void onStart() {
 
                }
 
                @Override
                public void onStop() {
 
                }
 
                @Override
                public void onDestroy() {
 
                }
            });

2)通過url獲取

Bitmap myBitmap = Glide.with(applicationContext)
    .load(yourUrl)
    .asBitmap() //必須
    .centerCrop()
    .into(500, 500)
    .get()

  1. 獲取圖片快取路徑
FutureTarget<File> future = Glide.with(mContext)
                    .load("url")
                    .downloadOnly(500, 500);
            try {
                File cacheFile = future.get();
                String path = cacheFile.getAbsolutePath();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

轉自:http://blog.csdn.net/qq_19711823/article/details/50856236