动态设置线性布局背景

发布于 2024-12-08 14:20:51 字数 194 浏览 0 评论 0原文

我想通过以下方式动态设置线性布局背景:

  1. 通过 XML 解析从 Web url 获取图像,然后将该图像存储到 SD 卡中。

  2. 现在图像已保存到 SD 卡中。

  3. 将该图像设置为应用程序中的线性布局背景。

现在我陷入了第三步。有人可以帮忙吗?

I wanted to set Linear Layout background dynamically in the following way:

  1. Fetch image from web url through XML parsing and then store that image into sd card.

  2. Now the image saved into sd card.

  3. Set that image as a linear layout background in the app.

Now I am stuck in the third step. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冷…雨湿花 2024-12-15 14:20:51

使用这个:

Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(bmImg);
linearLayout.setBackgroundDrawable(background);

还要检查这个:如何将位图转换为可绘制的在安卓中?

Use this:

Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(bmImg);
linearLayout.setBackgroundDrawable(background);

Also check this: How to convert a Bitmap to Drawable in android?

热风软妹 2024-12-15 14:20:51

我这样做了:

private RelativeLayout relativeLayout;

onCreate:

relativeLayout= (RelativeLayout)findViewById(R.id.relativeLayout);

new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg",
            "androidfigure").execute();

AsyncTask to load image in background:

private class LoadBackground extends AsyncTask<String, Void, Drawable> {

    private String imageUrl , imageName;

    public LoadBackground(String url, String file_name) {
        this.imageUrl = url;
        this.imageName = file_name;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Drawable doInBackground(String... urls) {

        try {
            InputStream is = (InputStream) this.fetch(this.imageUrl);
            Drawable d = Drawable.createFromStream(is, this.imageName);
            return d;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    private Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        relativeLayout.setBackgroundDrawable(result);
    }
}

希望这会对您有所帮助。

I have done this way:

private RelativeLayout relativeLayout;

onCreate:

relativeLayout= (RelativeLayout)findViewById(R.id.relativeLayout);

new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg",
            "androidfigure").execute();

AsyncTask to load image in background:

private class LoadBackground extends AsyncTask<String, Void, Drawable> {

    private String imageUrl , imageName;

    public LoadBackground(String url, String file_name) {
        this.imageUrl = url;
        this.imageName = file_name;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Drawable doInBackground(String... urls) {

        try {
            InputStream is = (InputStream) this.fetch(this.imageUrl);
            Drawable d = Drawable.createFromStream(is, this.imageName);
            return d;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    private Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        relativeLayout.setBackgroundDrawable(result);
    }
}

Hope this will help you.

桃气十足 2024-12-15 14:20:51

更简单的方法:

BitmapDrawable d = new BitmapDrawable("/sdcard/data/image.jpg");
linearLayout.setBackgroundDrawable(d);

An easier way:

BitmapDrawable d = new BitmapDrawable("/sdcard/data/image.jpg");
linearLayout.setBackgroundDrawable(d);
-残月青衣踏尘吟 2024-12-15 14:20:51

API 已弃用,您可以使用以下代码

BitmapDrawable background = new BitmapDrawable(getResources(), bitmapImage);
linearLayout.setBackground(background);

API are deprecated you can use the below code

BitmapDrawable background = new BitmapDrawable(getResources(), bitmapImage);
linearLayout.setBackground(background);
帝王念 2024-12-15 14:20:51

尝试使用这个:

Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.img);
BitmapDrawable bmpBackground = new BitmapDrawable(getResources(), bmpOriginal)

Try to use this:

Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.img);
BitmapDrawable bmpBackground = new BitmapDrawable(getResources(), bmpOriginal)
口干舌燥 2024-12-15 14:20:51

使用@Deimos的答案,但像这样,因为现在不推荐使用某些方法

Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(context.getResources(), bmImg);
linearLayout.setBackground(background);

Use @Deimos's answer, but like this since some methods are deprecated now

Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(context.getResources(), bmImg);
linearLayout.setBackground(background);
欢你一世 2024-12-15 14:20:51

您还可以从可绘制文件夹中设置图像。

yourView.setBackgroundResource(R.drawable.FILENAME);

将 FILENAME 设置为背景图像。

You can also set image from drawable folder.

yourView.setBackgroundResource(R.drawable.FILENAME);

That set FILENAME as background image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文