现有一个RelativeLayout,里面放了一些控件。要求给RelativeLayout设置背景图片,图片只有一个网络地址,要联网获取。已经有picasso框架,但是没找到类似方法。求教应该怎么做
1、先获取RelativeLayout控件
2、使用picasso获取图片在把图片设置RelativeLayout的background
private Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//加载成功后会得到一个bitmap,可以自定义操作
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
// 加载失败进行相应处理
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.into(target);
可以加一个imageview和relativelayout一样大,然后通过picasso加载网络图片显示到imageview上就会了吧!直接往relativelayout设置背景的我也不弄过,不过应该可以弄吧!
给RelativeLayout设置一个id ,通过findViewById() 方法找到RelativeLayout 的 relativeLayout
Picasso
.with(context)
.load(网址)
.into(relativeLayout);
要想给Relativelayout加载网络图片
1、通过服务端返回的图片地址,转换成bitmap对象
2、创建一个Drawable对象,把bitmap放进Drawable里去
2、Relativelayout有一个setBackgroundDrawable方法,然后把上一步的Drawable对象放进setBackgroundDrawable方法里去。
要想给Relativelayout加载网络图片
1、通过服务端返回的图片地址,转换成bitmap对象
2、创建一个Drawable对象,把bitmap放进Drawable里去
2、Relativelayout有一个setBackgroundDrawable方法,然后把上一步的Drawable对象放进setBackgroundDrawable方法里去。
建议使用ImageLoader
给RelativeLayout设置一个id ,通过findViewById() 方法找到RelativeLayout 的 relativeLayout
Picasso
.with(context)
.load(网址)
.into(relativeLayout);
1.给RelativeLayout设置一个id
2.通过id查找该relativeLayout
3.背景图片获取后,设置为relativeLayout的背景