【问题标题】:set image source to Bitmap in Drawable在 Drawable 中将图像源设置为位图
【发布时间】:2013-02-13 01:12:16
【问题描述】:

我在 drawable 文件夹中创建了 XML 文件,代码如下:

XML 名称为:background_bitmap_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="@drawable/background"
android:tileMode="disabled" />

现在,我想[通过代码]动态地将图像设置为这个 xml 文件。

我正在制作一个应用程序,我必须在每个活动中设置不同的背景,并且这些图像来自在线。

我没有找到任何解决方案将图像 src 设置为这个 xml 文件作为位图。

但是我还有另一种方法可以将背景设置为根布局,如下所示:

bitmap = BitmapFactory.decodeFile(FILE_PATH + "/" + SettingsScreen.BACKGROUND_IMAGE);
d = new BitmapDrawable(cntx.getResources(), bitmap);

但我需要将图像资源设置为上述 XML 文件,这样如果图像尺寸小于设备尺寸,图像就不会被拉伸。

任何帮助将图像资源设置为 xml 文件?

【问题讨论】:

  • 你在创建新的安卓操作系统还是什么???
  • 不,我正在从该可绘制 d 设置背景,如下所示:mView.setBackgroundDrawable(d);
  • android中有&lt;bitmap/&gt;标签吗?我第一次看到这个。
  • 当然,在 xml 文件中有

标签: android layout bitmap drawable xml-drawable


【解决方案1】:

你也可以试试这个,

URL url_value = new URL("your url");

ImageView img = (ImageView)v.findViewById(R.id.backgorundImage);
if (profile != null) {
    Bitmap mIcon1 =
        BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
    img.setImageBitmap(mIcon1);
}

您可以将 ImageView 设置为完全活动。在此示例中,图像从 Internet 下载并转换为位图并设置为 ImageView。

【讨论】:

  • 是的,我想到了;但是我的布局已经设计好了(啊这么多文件:P !!!!)。所以,我遇到了上述情况。我正在从单个全局类设置背景。 - 无论如何谢谢
猜你喜欢
  • 1970-01-01
  • 2019-12-17
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
相关资源
最近更新 更多