Kan*_*ika 18 android android-widget android-layout
我想以下列方式动态设置线性布局背景:
通过XML解析从Web URL获取图像,然后将该图像存储到SD卡中.
现在图像保存到SD卡中.
将该图像设置为应用程序中的线性布局背景.
现在我陷入了第三步.有人可以帮忙吗?
Dei*_*mos 49
用这个:
Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(bmImg);
linearLayout.setBackgroundDrawable(background);
Run Code Online (Sandbox Code Playgroud)
还要检查一下:如何在Android中将Bitmap转换为Drawable?
我已经这样做了:
private RelativeLayout relativeLayout;
Run Code Online (Sandbox Code Playgroud)
在创建:
relativeLayout= (RelativeLayout)findViewById(R.id.relativeLayout);
new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg",
"androidfigure").execute();
Run Code Online (Sandbox Code Playgroud)
的AsyncTask到负载在图像背景:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你。
| 归档时间: |
|
| 查看次数: |
49900 次 |
| 最近记录: |