Android 无法使用毕加索从URL设置LinearLayout BackgroundResource
我有一个线性布局,我想通过编程更改背景:Android 无法使用毕加索从URL设置LinearLayout BackgroundResource,android,android-layout,background,android-linearlayout,Android,Android Layout,Background,Android Linearlayout,我有一个线性布局,我想通过编程更改背景: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/downloadLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="1" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/downloadLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
...
线性布局2.挫折资源(剩余)
然而,背景图像从未加载,没有NPE,图像只是从未加载。如有任何建议,我们将不胜感激
我已经进行了一些调试,目前有以下值:
linearLayout2 = android.widget.LinearLayout{529b3f58 V.E..... ......I. 0,0-0,0 #7f0a008e app:id/downloadLayout}
background = http://xxx.xxx.x.xxx/bgs/big_lebowski_bg.jpg
resID = 0
附言
我也尝试过用毕加索来完成同样的工作——我不知道如何绕过所述错误并成功加载它:
资料来源:
final LinearLayout downloadLayout = (LinearLayout) findViewById(R.id.downloadLayout);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(downloadLayout);
错误:
The method into(Target) in the type RequestCreator is not applicable for the arguments (LinearLayout)
您必须在后台线程中执行此操作,而不是在主线程中。考虑使用图像加载库,如 例如 已更新 确保在LinearLayout中有一个图像视图,并让它填充父视图(即LinearLayout),毕加索不直接适用于图像视图,因此会出现错误
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
或者
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.yourLinearLayoutid);
BitmapDrawable drawableBitmap=new BitmapDrawable(getBitmap(urlString));
linearLayout.setBackgroundDrawable(drawableBitmap);
这个方法getBitMap,就像Piccasso库中的方法一样,应该足够了,而不需要库
private Bitmap getBitmap(String url)
{
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}
毕加索只处理图像视图,不处理布局 您可以在布局中放置ImageView,将其设置为与父级的宽度和高度匹配,然后将图像注入ImageView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/downloadLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我尝试了您建议的方法-请查看我上面更新的源代码。setBackgroundDrawable()已被弃用。@peresisUser Easy fix mate,只需将其更改为setBackground(),它将调用setBackgroundDrawable()
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/downloadLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);