Android开发-高级网络组件(2)使用Volley加载网络图片-AndroidStudio
来源:互联网 发布:鸟哥linux私房菜基础篇 编辑:程序博客网 时间:2024/06/05 02:32
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/70859690
觉得博文有用,请点赞,请评论,请关注,谢谢!~
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/70859690
觉得博文有用,请点赞,请评论,请关注,谢谢!~
觉得博文有用,请点赞,请评论,请关注,谢谢!~
/** * ImageRequest 加载网络图片 * ImageLoader 加载网络图片,使用ImageRequest实现,但是,ImageLoader不仅可以缓存图片,还是可以过滤 * 重复用连接,避免重复发送请求 * * 下面有一个ImageLoader使用缓存的例子,但是,使用起来没有感觉到区别的存在。 * 我猜测这个版本的Volley已经使用LruCache,有看过这个版本Volley的ImageLoader源码的同学请指点一二 * * Demo有以下几个演示: * 1、使用Volley的ImageRequest 加载网络图片 * 2、使用Volley的ImageLoader 加载网络图片 * 3、使用Volley的ImageLoader 加载网络图片(带缓存) * 4、使用Volley的NetworkImageView 加载网络图片(最大优点是,显示的时候自动化对图片进行压缩,不会多占用内存) */
老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
导包:
MainActivity.java:
package com.iwanghang.volleydemo;import android.graphics.Bitmap;import android.support.v4.util.LruCache;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.ImageLoader;import com.android.volley.toolbox.ImageRequest;import com.android.volley.toolbox.NetworkImageView;import com.android.volley.toolbox.Volley;/** * ImageRequest 加载网络图片 * ImageLoader 加载网络图片,使用ImageRequest实现,但是,ImageLoader不仅可以缓存图片,还是可以过滤 * 重复用连接,避免重复发送请求 * * 下面有一个ImageLoader使用缓存的例子,但是,使用起来没有感觉到区别的存在。 * 我猜测这个版本的Volley已经使用LruCache,有看过这个版本Volley的ImageLoader源码的同学请指点一二 * * Demo有以下几个演示: * 1、使用Volley的ImageRequest 加载网络图片 * 2、使用Volley的ImageLoader 加载网络图片 * 3、使用Volley的ImageLoader 加载网络图片(带缓存) * 4、使用Volley的NetworkImageView 加载网络图片(最大优点是,显示的时候自动化对图片进行压缩,不会多占用内存) */public class MainActivity extends AppCompatActivity { private ImageView imageView_volley; private ImageView imageView_volley2; private ImageView imageView_volley3; private NetworkImageView networkImageView; // 创建请求队列 RequestQueue requestQueue = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView_volley = (ImageView) findViewById(R.id.imageView_volley); imageView_volley2 = (ImageView) findViewById(R.id.imageView_volley2); imageView_volley3 = (ImageView) findViewById(R.id.imageView_volley3); networkImageView = (NetworkImageView) findViewById(R.id.networkImageView); requestQueue = Volley.newRequestQueue(this); } // 使用Volley的ImageRequest 加载网络图片 public void volleySendImageRequestClick(View view){ // 请求地址 //String url = "http://avatar.csdn.net/6/5/8/1_iwanghang.jpg"; String url = "http://csdnimg.cn/www/company/images/company09_pic20.gif"; ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { imageView_volley.setImageBitmap(response); } }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { System.out.println(error); } }); // 添加到请求队列 requestQueue.add(request); } // 使用Volley的ImageLoader 加载网络图片 public void volleySendImageLoaderClick(View view){ //String url = "http://csdnimg.cn/www/company/images/company09_pic20.gif"; String url = "http://img-download.pchome.net/download/1k1/bh/5o/ok6m9e-muu.jpg"; ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() { @Override public Bitmap getBitmap(String url) { return null; } @Override public void putBitmap(String url, Bitmap bitmap) { } }); //imageLoader.get(url, imageLoader.getImageListener(imageView_volley2,R.drawable.loading,R.drawable.error)); // 还可以多加2个参数,还可以设置图片显示大小 imageLoader.get(url, imageLoader.getImageListener(imageView_volley2,R.drawable.loading,R.drawable.error),200,200); } // 用于图片缓存 private class BitmapCache implements ImageLoader.ImageCache{ private LruCache<String,Bitmap> lruCache; private int maxCache = 10*1024*1024; // 10MB缓存 public BitmapCache(){ lruCache = new LruCache<>(maxCache); } @Override public Bitmap getBitmap(String url) { return lruCache.get(url); // 根据图片地址从缓存获取图片 } @Override public void putBitmap(String url, Bitmap bitmap) { lruCache.put(url,bitmap); // 根据图片地址存储图片到缓存 } } // 使用Volley的ImageLoader 加载网络图片(带缓存) public void volleySendImageLoaderClick2(View view){ //String url = "http://csdnimg.cn/www/company/images/company09_pic20.gif"; String url = "http://img-download.pchome.net/download/1k1/bh/5o/ok6m9e-muu.jpg"; ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache()); //imageLoader.get(url, imageLoader.getImageListener(imageView_volley2,R.drawable.loading,R.drawable.error)); // 还可以多加2个参数,还可以设置图片显示大小 imageLoader.get(url, imageLoader.getImageListener(imageView_volley3,R.drawable.loading,R.drawable.error),200,200); } // 用Volley的NetworkImageView 加载网络图片 public void volleySendNetworkImageViewClick(View view){ String url = "http://img-download.pchome.net/download/1k1/bh/5o/ok6m9e-muu.jpg"; networkImageView.setDefaultImageResId(R.drawable.loading); networkImageView.setErrorImageResId(R.drawable.error); ImageLoader imageLoader = new ImageLoader(requestQueue,new BitmapCache()); networkImageView.setImageUrl(url,imageLoader); }}activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.iwanghang.volleydemo.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView_info" /> <Button android:onClick="volleySendImageRequestClick" android:text="使用Volley的ImageRequest 加载网络图片" android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buttonVolleySendImageRequestClick" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="center" android:id="@+id/imageView_volley" /> <Button android:onClick="volleySendImageLoaderClick" android:text="使用Volley的ImageLoader 加载网络图片" android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buttonVolleySendImageLoaderClick" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="center" android:id="@+id/imageView_volley2" /> <Button android:onClick="volleySendImageLoaderClick2" android:text="使用Volley的ImageLoader 加载网络图片(带缓存)" android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buttonVolleySendImageLoaderClick2" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="center" android:id="@+id/imageView_volley3" /> <Button android:onClick="volleySendNetworkImageViewClick" android:text="使用Volley的NetworkImageView 加载网络图片" android:textAllCaps="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/buttonVolleySendImageLoaderClick3" /> <com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_gravity="center_horizontal" android:layout_width="200dp" android:layout_height="200dp" /></LinearLayout>loading.png:
error.png:(网络请求失败会显示这个图片,可以随便写个不存在的图片地址,就可以看到效果,GIF没有演示这个效果,很简单)
网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/70859690
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
觉得博文有用,请点赞,请评论,请关注,谢谢!~
2 0
- Android开发-高级网络组件(2)使用Volley加载网络图片-AndroidStudio
- Android开发-高级网络组件(1)使用Volley登陆-AndroidStudio
- Android开发使用Volley加载网络图片
- Android Volley,使用Volley加载网络图片
- Android Volley 使用Volley加载网络图片
- Android Volley完全解析2:使用Volley加载网络图片
- Android 使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- Android Volley完全解析(二),使用Volley加载网络图片
- kMeans算法(K均值聚类算法)
- servlet
- nec css规范
- spring和hibernate整合遇到的问题
- 无人与我立黄昏,无人问我粥可温。
- Android开发-高级网络组件(2)使用Volley加载网络图片-AndroidStudio
- 2017年4月26日 阿里巴巴笔试编程题 股票
- 利用SSL For Free工具3分钟获取Let's Encrypt免费SSL证书(https免费证书)
- 查看linux操作系统版本号,查看glibc版本号
- jsp笔记
- linux 出现大量TIME_WAIT
- Git命令大全及相关概念
- matlab:常用内置函数
- promise是什么,使用方法