如何在ImageView中使用URL引用的图像?
当前回答
在我看来,最适合完成这项任务的现代图书馆是毕加索广场图书馆。它允许通过URL加载图像到ImageView,只需一行代码:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
其他回答
你也可以使用LoadingImageView视图从url加载图片:
http://blog.blundellapps.com/imageview-with-loading-spinner/
一旦你从链接中添加了类文件,你就可以实例化一个url图像视图:
在xml:
<com.blundell.tut.LoaderImageView
android:id="@+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
image="http://developer.android.com/images/dialog_buttons.png"
/>
在代码:
final LoaderImageView image = new LoaderImageView(this, "http://developer.android.com/images/dialog_buttons.png");
并更新它使用:
image.setImageDrawable("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
我写了一个类来处理这个问题,因为它似乎是我各种项目中反复出现的需求:
https://github.com/koush/UrlImageViewHelper
UrlImageViewHelper will fill an ImageView with an image that is found at a URL. The sample will do a Google Image Search and load/show the results asynchronously. UrlImageViewHelper will automatically download, save, and cache all the image urls the BitmapDrawables. Duplicate urls will not be loaded into memory twice. Bitmap memory is managed by using a weak reference hash table, so as soon as the image is no longer used by you, it will be garbage collected automatically.
适用于任何容器中的imageView,比如listview grid view, normal layout
private class LoadImagefromUrl extends AsyncTask< Object, Void, Bitmap > {
ImageView ivPreview = null;
@Override
protected Bitmap doInBackground( Object... params ) {
this.ivPreview = (ImageView) params[0];
String url = (String) params[1];
System.out.println(url);
return loadBitmap( url );
}
@Override
protected void onPostExecute( Bitmap result ) {
super.onPostExecute( result );
ivPreview.setImageBitmap( result );
}
}
public Bitmap loadBitmap( String url ) {
URL newurl = null;
Bitmap bitmap = null;
try {
newurl = new URL( url );
bitmap = BitmapFactory.decodeStream( newurl.openConnection( ).getInputStream( ) );
} catch ( MalformedURLException e ) {
e.printStackTrace( );
} catch ( IOException e ) {
e.printStackTrace( );
}
return bitmap;
}
/** Usage **/
new LoadImagefromUrl( ).execute( imageView, url );
imageView.setImageBitmap(BitmapFactory.decodeStream(imageUrl.openStream()));//try/catch IOException and MalformedURLException outside
嗨,我有最简单的代码试试这个
public class ImageFromUrlExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
imgView.setImageDrawable(drawable);
}
private Drawable LoadImageFromWebOperations(String url)
{
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
main。xml
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/ImageView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
试试这个
推荐文章
- 如何防止回到以前的活动?
- Android模拟器没有启动,显示“无效的命令行参数”
- 将double类型转换为字符串
- Android防止双击按钮
- 在Android上理解颜色(6个字符)
- Restful API服务
- 我如何处理ImeOptions的完成按钮点击?
- 模块是用不兼容的Kotlin版本编译的。其元数据的二进制版本为1.5.1,预期版本为1.1.15
- 如何在Android工作室添加“libs”文件夹?
- 使用什么api来绘制其他应用程序(如Facebook的Chat Heads)?
- getDefaultSharedPreferences和getSharedPreferences的区别
- 如何模拟按钮点击使用代码?
- Android Webview给出net::ERR_CACHE_MISS消息
- Parcelable遇到IOException写入序列化对象getactivity()
- 如何在Android中动态更改菜单项文本