【问题标题】:LinearLayout setBackgroundDrawable bitmap from url来自 url 的 LinearLayout setBackgroundDrawable 位图
【发布时间】:2017-05-20 18:15:09
【问题描述】:

我正在尝试将背景图像从 URL 设置为带有位图的线性布局,但 BackgroundDrawable() 方法已被弃用,我找不到任何替代方法。

private LinearLayout linearLayout;
linearLayout = (LinearLayout) findViewById(R.id.layout);
Drawable drawableBitmap = new BitmapDrawable(getApplicationContext().getResources(), getBitmapFromURL(url));
linearLayout.setBackgroundDrawable(drawableBitmap);

【问题讨论】:

标签: android image bitmap android-linearlayout deprecated


【解决方案1】:

试试这个解决方案。你的位图放在这里,

LinearLayout bg = (LinearLayout) findViewById(R.id.linearlayout);
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)
bg.setBackground(ob);

【讨论】:

    【解决方案2】:

    使用毕加索图书馆

               private LinearLayout linearLayout;
               linearLayout = (LinearLayout) findViewById(R.id.layout);
               ImageView img = new ImageView(this);
               Picasso.with(this)
              .load(imageUri)
              .fit()
              .centerCrop()
              .into(img, new Callback() {
                        @Override
                        public void onSuccess() {                            
                        linearLayout.setBackgroundDrawable(img.getDrawable());
                        }
    
                        @Override
                        public void onError() {
    
                        }
                    });
    

    【讨论】:

    • FWIW,setBackgroundDrawable() 在 API 级别 16 中已替换为 setBackground()
    猜你喜欢
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2021-06-11
    相关资源
    最近更新 更多