Android 网络url设置View背景图

imgstr为url网络图片地址,topllay是要设置背景的控件;

方法1.Android Glide设置View背景图

  Glide.with(this).load(imgStr).asBitmap()//签到整体 背景
                    .into(new SimpleTarget<Bitmap>() {        //设置宽高
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            Drawable drawable = new BitmapDrawable(resource);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                topllay.setBackground(drawable);    //设置背景
                            }
                        }
                    });

方法2.


            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream is = (InputStream) new URL(imgStr).getContent();
                        final Drawable d = Drawable.createFromStream(is, "src");
                        is.close();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                topllay.setBackground(d);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
原文地址:https://www.cnblogs.com/wzqnxd/p/10250453.html