android - 将位图设置为我的布局背景

标签 android android-bitmap setbackground

我正在从 URL 下载图像并将此位图用于我的布局背景。我看不到我的图像。

过去两天我一直在努力解决这个问题。现在我仍然无法找到解决方案。我用谷歌搜索了很多。请帮助我犯下的错误。

提前非常感谢。

我的代码:

Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");


        if (android.os.Build.VERSION.SDK_INT >= 16){
            setBackgroundV16Plus( myImage);
        }
        else{
            setBackgroundV16Minus( myImage);
        }

@TargetApi(16)
    private void setBackgroundV16Plus(  Bitmap bitmap) {
        linearLayout.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));

    }

    @SuppressWarnings("deprecation")
    private void setBackgroundV16Minus(  Bitmap bitmap) {
        linearLayout.setBackgroundDrawable(new BitmapDrawable(bitmap));
    }

    public Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

最佳答案

RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);

关于android - 将位图设置为我的布局背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383426/

相关文章:

android - 如何使我的 TabLayout 标题位于 ToolBar 下方?

android - Android Studio 布局编辑器中标尺的单位是什么?

java - 自定义 Circlular seek arc 以将位图放置在给定的进度级别

android - 通过 xml 文件以编程方式设置背景

button - 如何使用我自己的自定义按钮并使用 wxpython 进行设计

java - Android HTML Jsoup

安卓磨损 : Programmatically stop my app's notifications from appearing on watch

java - 如何设置 MEDIASTORE 以从 android 上的特定文件夹获取图像

android - 使用 ImageView ScaleType 设置 TransitionDrawable

Java setBackground 不工作