Android之界面

Glide4(URL转File,URL转Drawable)

2018-01-29  本文已影响2170人  楷桐

原文链接

直接获取File / Bitmap / File / Gif

public void downloadImage() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String url = "http://www.guolin.tech/book.png";
                final Context context = getApplicationContext();
                FutureTarget<File> target = Glide.with(context)
                        .asFile()
                        .load(url)
                        .submit();
                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();
}

URL转Drawable

SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
    @Override
    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
        imageView.setImageDrawable(resource);
    }
};

public void loadImage(View view) {
    Glide.with(this)
         .load("http://guolin.tech/book.png")
         .into(simpleTarget);
}

4.0以下的怎么获取bitmap ??

Bitmap myBitmap = Glide.with(applicationContext)  
        .load(yourUrl)  
        .asBitmap()  
        .centerCrop()  
        .into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)  
        .get()  ;
上一篇下一篇

猜你喜欢

热点阅读