Android三种基本的加载网络图片方式(转)
Android三种基本的加载网络图片方式,包括普通加载网络方式、用ImageLoader加载图片、用Volley加载图片。
1. [代码]普通加载网络方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
public class NormalLoadPictrue { private String uri; private ImageView imageView; private byte [] picByte; public void getPicture(String uri,ImageView imageView){ this .uri = uri; this .imageView = imageView; new Thread(runnable).start(); } @SuppressLint ( "HandlerLeak" ) Handler handle = new Handler(){ @Override public void handleMessage(Message msg) { super .handleMessage(msg); if (msg.what == 1 ) { if (picByte != null ) { Bitmap bitmap = BitmapFactory.decodeByteArray(picByte, 0 , picByte.length); imageView.setImageBitmap(bitmap); } } } }; Runnable runnable = new Runnable() { @Override public void run() { try { URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod( "GET" ); conn.setReadTimeout( 10000 ); if (conn.getResponseCode() == 200 ) { InputStream fis = conn.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte [] bytes = new byte [ 1024 ]; int length = - 1 ; while ((length = fis.read(bytes)) != - 1 ) { bos.write(bytes, 0 , length); } picByte = bos.toByteArray(); bos.close(); fis.close(); Message message = new Message(); message.what = 1 ; handle.sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } } }; } |
2. [代码]用ImageLoader加载图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public class ImageLoaderPicture { private DisplayImageOptions options; public ImageLoaderPicture(Context context) { ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2 ) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator( new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() .memoryCache( new WeakMemoryCache()) .build(); ImageLoader.getInstance().init(config); options = new DisplayImageOptions.Builder() .showStubImage( 0 ) .showImageForEmptyUri( 0 ) .showImageOnFail( 0 ) .cacheInMemory().cacheOnDisc() .imageScaleType(ImageScaleType.IN_SAMPLE_INT) .bitmapConfig(android.graphics.Bitmap.Config.RGB_565) .build(); } public DisplayImageOptions getOptions() { return options; } public void setOptions(DisplayImageOptions options) { this .options = options; } } |
3. [代码]用Volley加载图片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
public class VolleyLoadPicture { private ImageLoader mImageLoader = null ; private BitmapCache mBitmapCache; private ImageListener one_listener; public VolleyLoadPicture(Context context,ImageView imageView){ one_listener = ImageLoader.getImageListener(imageView, 0 , 0 ); RequestQueue mRequestQueue = Volley.newRequestQueue(context); mBitmapCache = new BitmapCache(); mImageLoader = new ImageLoader(mRequestQueue, mBitmapCache); } public ImageLoader getmImageLoader() { return mImageLoader; } public void setmImageLoader(ImageLoader mImageLoader) { this .mImageLoader = mImageLoader; } public ImageListener getOne_listener() { return one_listener; } public void setOne_listener(ImageListener one_listener) { this .one_listener = one_listener; } class BitmapCache implements ImageCache { private LruCache<String, Bitmap> mCache; private int sizeValue; public BitmapCache() { int maxSize = 10 * 1024 * 1024 ; mCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap value) { sizeValue = value.getRowBytes() * value.getHeight(); return sizeValue; } }; } @Override public Bitmap getBitmap(String url) { return mCache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { mCache.put(url, bitmap); } } } |
4. [代码]Activity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public class MainActivity extends Activity { private ImageView imageView001,imageView002,imageView003; public static final String picUrl = "http://img.quwenjiemi.com/2014/0701/thumb_420_234_20140701112917406.jpg" ; //public static final String picUrl = "http://192.168.1.181:8081/AndroidSerivces/house.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView001 = (ImageView)findViewById(R.id.imageView001); imageView002 = (ImageView)findViewById(R.id.imageView002); imageView003 = (ImageView)findViewById(R.id.imageView003); //用普通方法加载图片 new NormalLoadPictrue().getPicture(picUrl,imageView001); //用ImageLoader加载图片 ImageLoader.getInstance().displayImage(picUrl, imageView002, new ImageLoaderPicture( this ).getOptions(), new SimpleImageLoadingListener()); //用Volley加载图片 VolleyLoadPicture vlp = new VolleyLoadPicture( this , imageView003); vlp.getmImageLoader().get(picUrl, vlp.getOne_listener()); } } |
5. [代码]布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<ScrollView xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" > <RelativeLayout android:layout_width= "match_parent" android:layout_height= "match_parent" android:padding= "10dp" > <TextView android:id= "@+id/textView001" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "1.用普通方法的加载图片" /> <ImageView android:id= "@+id/imageView001" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/textView001" /> <TextView android:id= "@+id/textView002" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/imageView001" android:text= "2.用Android-Universal-Image-Loader加载图片" /> <ImageView android:id= "@+id/imageView002" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/textView002" /> <TextView android:id= "@+id/textView003" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/imageView002" android:text= "3.用Volley加载图片" /> <ImageView android:id= "@+id/imageView003" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/textView003" /> </RelativeLayout> </ScrollView> |
6. [文件] 三种基本网络加载图片方式.rar ~ 2MB 下载
转自:http://www.oschina.net/code/snippet_1470644_36944
Android三种基本的加载网络图片方式(转)的更多相关文章
- 6_1 持久化模型与再次加载_探讨(1)_三种持久化模型加载方式以及import_meta_graph方式加载持久化模型会存在的变量管理命名混淆的问题
笔者提交到gitHub上的问题描述地址是:https://github.com/tensorflow/tensorflow/issues/20140 三种持久化模型加载方式的一个小结论 加载持久化模型 ...
- Android笔记之使用Glide加载网络图片、下载图片
Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...
- Android开发三种第三方图片加载的框架
最近在项目中用到了大量图片加载,第三方优秀框架还不错,下面介绍三款榜首的框架用法和问题,做一个记录. 现在项目使用的是Android Studio开发的,现在也没有多少人使用Eclipse了吧. 一. ...
- Android中用双缓存技术,加载网络图片
最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...
- Android四种Activity的加载模式(转)
建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型: http://www.cnblogs.com/ghj1976/archive/2011/0 ...
- Android四种Activity的加载模式
建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型 http://www.cnblogs.com/ghj1976/archive/2011/04 ...
- [android]完美的解决方案ListView加载网络图片反弹问题
为什么 先说为什么有照片反弹. 使用convertView对ListView的每一个item优化,item的复用能够有效减少内存的占用.使ListView滑动更为流畅. 但会带来一个问题,当最顶部的i ...
- Android笔记之使用ImageView加载网络图片以及保存图片到本地并更新图库
ImageView显示网络图片 findViewById(R.id.btnLoad).setOnClickListener(new View.OnClickListener() { @Override ...
- Android Volley入门到精通:使用Volley加载网络图片
在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...
随机推荐
- C++函数传递指针面试题
[本文链接] http://www.cnblogs.com/hellogiser/p/function-passing-pointer-interview-questions.html [代码1] ...
- 79 两个整数集合A和B,求其交集
[本文链接] http://www.cnblogs.com/hellogiser/p/ab-intersect.html [题目] 两个整数集合A和B,求其交集. [分析] 1. 读取整数集合A中 ...
- Jenkins安装部署
官方文档:https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Red+Hat+distributions#Install ...
- (转载)使用 udev 高效、动态地管理 Linux 设备文件
概述: Linux 用户常常会很难鉴别同一类型的设备名,比如 eth0, eth1, sda, sdb 等等.通过观察这些设备的内核设备名称,用户通常能知道这些是什么类型的设备,但是不知道哪一个设备是 ...
- [转] 理解 Thread.Sleep 函数
原文链接:http://www.cnblogs.com/ILove/archive/2008/04/07/1140419.html 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时 ...
- KMP单模快速字符串匹配算法
KMP算法是由Knuth,Morris,Pratt共同提出的算法,专门用来解决模式串的匹配,无论目标序列和模式串是什么样子的,都可以在线性时间内完成,而且也不会发生退化,是一个非常优秀的算法,时间复杂 ...
- CSS3实现元素旋转
-webkit-transform:rotate(30deg); 参数代表顺时针自旋转角度 这个元素还提供了文本倾斜的方法 - HTML5与CSS3 P299
- 【QT】C++ GUI Qt4 学习笔记4
感觉这本书的顺序设计的太不合理了,出现的最多的一句话就是后面会讲.按照使用的顺序讲不行吗?搞得代码都运行不了. 我决定先直接跳到73页,子类化QTableWidgetItem这一节.因为前面功能的实现 ...
- $.each 和$(selector).each()的区别
$.each() 对数组或对对象内容进行循环处理 jQuery.each( collection, callback(indexInArray, valueOfElement) ) collectio ...
- SQL基本CRUD
--已知Oracle的Scott用户中提供了三个测试数据库表 --名称分别为dept,emp,salgrade.使用SQL语言完成一下操作 --1,查询20号部门的所有员工信息: SELECT * F ...