美文网首页Android之界面
Glide4(URL转File,URL转Drawable)

Glide4(URL转File,URL转Drawable)

作者: 楷桐 | 来源:发表于2018-01-29 15:00 被阅读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()  ;
    

    相关文章

      网友评论

        本文标题:Glide4(URL转File,URL转Drawable)

        本文链接:https://www.haomeiwen.com/subject/eibxzxtx.html