三种基本网络加载图片方式
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 | publicclassNormalLoadPictrue {        privateString uri;    privateImageView imageView;    privatebyte[] picByte;            publicvoidgetPicture(String uri,ImageView imageView){        this.uri = uri;        this.imageView = imageView;        newThread(runnable).start();    }        @SuppressLint("HandlerLeak")    Handler handle = newHandler(){        @Override        publicvoidhandleMessage(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 = newRunnable() {        @Override        publicvoidrun() {            try{                URL url = newURL(uri);                HttpURLConnection conn = (HttpURLConnection)url.openConnection();                conn.setRequestMethod("GET");                conn.setReadTimeout(10000);                                if(conn.getResponseCode() == 200) {                    InputStream fis =  conn.getInputStream();                    ByteArrayOutputStream bos = newByteArrayOutputStream();                    byte[] bytes = newbyte[1024];                    intlength = -1;                    while((length = fis.read(bytes)) != -1) {                        bos.write(bytes, 0, length);                    }                    picByte = bos.toByteArray();                    bos.close();                    fis.close();                                        Message message = newMessage();                    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 | publicclassImageLoaderPicture {        privateDisplayImageOptions options;    publicImageLoaderPicture(Context context) {                ImageLoaderConfiguration config = newImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2)        .denyCacheImageMultipleSizesInMemory()        .discCacheFileNameGenerator(newMd5FileNameGenerator())        .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()         .memoryCache(newWeakMemoryCache())                                         .build();        ImageLoader.getInstance().init(config);                options = newDisplayImageOptions.Builder()        .showStubImage(0)        .showImageForEmptyUri(0)        .showImageOnFail(0)        .cacheInMemory().cacheOnDisc()        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)        .bitmapConfig(android.graphics.Bitmap.Config.RGB_565)        .build();    }    publicDisplayImageOptions getOptions() {        returnoptions;    }    publicvoidsetOptions(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 | publicclassVolleyLoadPicture {        privateImageLoader mImageLoader = null;    privateBitmapCache mBitmapCache;        privateImageListener one_listener;        publicVolleyLoadPicture(Context context,ImageView imageView){        one_listener = ImageLoader.getImageListener(imageView, 0, 0);                RequestQueue mRequestQueue = Volley.newRequestQueue(context);        mBitmapCache = newBitmapCache();        mImageLoader = newImageLoader(mRequestQueue, mBitmapCache);    }    publicImageLoader getmImageLoader() {        returnmImageLoader;    }    publicvoidsetmImageLoader(ImageLoader mImageLoader) {        this.mImageLoader = mImageLoader;    }    publicImageListener getOne_listener() {        returnone_listener;    }    publicvoidsetOne_listener(ImageListener one_listener) {        this.one_listener = one_listener;    }        classBitmapCache implementsImageCache {        privateLruCache<String, Bitmap> mCache;        privateintsizeValue;                publicBitmapCache() {            intmaxSize = 10* 1024* 1024;            mCache = newLruCache<String, Bitmap>(maxSize) {                @Override                protectedintsizeOf(String key, Bitmap value) {                    sizeValue = value.getRowBytes() * value.getHeight();                    returnsizeValue;                }                            };        }        @Override        publicBitmap getBitmap(String url) {            returnmCache.get(url);        }        @Override        publicvoidputBitmap(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 | publicclassMainActivity extendsActivity {        privateImageView imageView001,imageView002,imageView003;        publicstaticfinalString 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    protectedvoidonCreate(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);                //用普通方法加载图片        newNormalLoadPictrue().getPicture(picUrl,imageView001);                //用ImageLoader加载图片        ImageLoader.getInstance().displayImage(picUrl, imageView002,newImageLoaderPicture(this).getOptions(),newSimpleImageLoadingListener());                //用Volley加载图片        VolleyLoadPicture vlp = newVolleyLoadPicture(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> | 
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号