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();


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM