网络通讯框架-Volley源码分析(3)
2014-03-25 16:26
267 查看
Request//请求对象
ImageRequest:用于执行网络请求
void finish(final String tag) { if (mRequestQueue != null) { mRequestQueue.finish(this);//移除该请求对象 } if (MarkerLog.ENABLED) { final long threadId = Thread.currentThread().getId(); if (Looper.myLooper() != Looper.getMainLooper()) { // If we finish marking off of the main thread, we need to // actually do it on the main thread to ensure correct ordering. Handler mainThread = new Handler(Looper.getMainLooper()); mainThread.post(new Runnable() { public void run() { mEventLog.add(tag, threadId); mEventLog.finish(this.toString()); } }); return; } mEventLog.add(tag, threadId); mEventLog.finish(this.toString()); } else { long requestTime = SystemClock.elapsedRealtime() - mRequestBirthTime; if (requestTime >= SLOW_REQUEST_THRESHOLD_MS) { VolleyLog.d("%d ms: %s", requestTime, this.toString()); } } }
ImageRequest:用于执行网络请求
protected Response<Bitmap> parseNetworkResponse(NetworkResponse response) { // Serialize all decode on a global lock to reduce concurrent heap usage. synchronized (sDecodeLock) {//解码锁,不在同一时刻对多个图像进行解码,避免OOM try { return doParse(response); } catch (OutOfMemoryError e) { VolleyLog.e("Caught OOM for %d byte image, url=%s", response.data.length, getUrl()); return Response.error(new ParseError(e)); } } }
private Response<Bitmap> doParse(NetworkResponse response) { byte[] data = response.data; BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); Bitmap bitmap = null; if (mMaxWidth == 0 && mMaxHeight == 0) { decodeOptions.inPreferredConfig = mDecodeConfig;//编码图片格式 bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions); } else { // If we have to resize this image, first get the natural bounds. decodeOptions.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions); int actualWidth = decodeOptions.outWidth; int actualHeight = decodeOptions.outHeight; //计算理想的尺寸进行编码图片 int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight); int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth); // Decode to the nearest power of two scaling factor. decodeOptions.inJustDecodeBounds = false; // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it? // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED; decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight); Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions); // If necessary, scale down to the maximal acceptable size. if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {//从现有的位图进行缩放,创建新的位图 bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true); tempBitmap.recycle(); } else { bitmap = tempBitmap; } } if (bitmap == null) { return Response.error(new ParseError());//如果位图为null,则返回解析错误 } else { return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));//如果位图不为null,则返回图片 } }
相关文章推荐
- 网络通讯框架-Volley源码分析(2)
- 使用AsyncTask弹出进度条ProgressDialog,利用网上的某音乐地址下载歌曲
- go TCP 测试
- 【iOS】从网络下载图片,保存,并用 UIImageView 从保存中显示
- VM9.0中Ubuntu12.04设置好网络后无法ping通主机
- Java 7网络编程
- HTTP协议中的ContenType类型大全(转)
- TCP,UDP,IP总结
- ubuntu 网络配置中
- openrowset 的网络访问
- SQL server 自动网络备份
- TCP网络传输客户端client
- tcp网络传输服务器 listener
- windows下映射网络驱动器
- HttpClient 4.3 DEMO
- java http请求 获取xml格式
- TCP的拥塞控制 (四)
- TCP的拥塞控制 (三)
- GMHttp
- TCP的拥塞控制 (二)