Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从web中线性布局背景和图像_Android - Fatal编程技术网

Android 从web中线性布局背景和图像

Android 从web中线性布局背景和图像,android,Android,我必须从互联网上提取图像,并将其作为线性布局的背景-这可能吗?我还没有看到任何合适的方法。只需使用位图工厂加载图像(如果需要,请调整其大小),使用位图可绘制,并使用LinearLayout.setBackgroundDrawable()将图像视图设置为LinearLayout的背景即可: <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <

我必须从互联网上提取图像,并将其作为线性布局的背景-这可能吗?我还没有看到任何合适的方法。

只需使用
位图工厂加载图像(如果需要,请调整其大小),使用
位图可绘制
,并使用
LinearLayout.setBackgroundDrawable()
将图像视图设置为LinearLayout的背景即可:

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView android:id="@+id/myimageview"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"/>
</LinearLayout>
希望这有帮助。另外,请看这个(我从中获得了图像下载代码)。

当然

只需获取图像的输入流:

InputStream is = (InputStream) new URL(url).getContent();
从流中获取可绘制图形:

Drawable d = Drawable.createFromStream(is, "src name");
然后将LinearLayout背景设置为可绘制:

linearLayout.setBackgroundDrawable(d);
这实际上是直接从流中设置图像。您可能希望使用ASyncTask在后台拉下绘图表,然后进行设置:

您可能还想研究惰性加载程序:

linearLayout.setBackgroundDrawable(d);