Пример #1
0
 //响应请求
 private void BatchResponse(String cacheKey, BatchedImageRequest request)
 {
     mBatchedResponses.Add(cacheKey, request);
     if (mRunnable == null)
     {
         mRunnable = () =>
         {
             foreach (BatchedImageRequest bir in mBatchedResponses.Values)
             {
                 foreach (ImageContainer container in bir.Containers)
                 {
                     if (container.OnSuccessResponse == null)
                     {
                         continue;
                     }
                     if (bir.Error == null)
                     {
                         container.Bitmap = bir.ResponseBitmap;
                         container.OnSuccessResponse(container, false);
                     }
                     else
                     {
                         if (container.OnErrorResponse != null)
                         {
                             container.OnErrorResponse(bir.Error);
                         }
                     }
                 }
             }
             mBatchedResponses.Clear();
             mRunnable = null;
         };
     }
     mHandler.PostDelayed(mRunnable, mBatchResponseDelayMs);
 }
        public void CancelRequest()
        {
            if (OnSuccessResponse == null)
            {
                return;
            }

            BatchedImageRequest request = null;

            mInFlightRequests.TryGetValue(mCacheKey, out request);
            if (request != null)
            {
                bool canceled = request.RemoveContainerAndCancelIfNecessary(this);
                if (canceled)
                {
                    mInFlightRequests.Remove(mCacheKey);
                }
            }
            else
            {
                mBatchedResponses.TryGetValue(mCacheKey, out request);
                if (request != null)
                {
                    request.RemoveContainerAndCancelIfNecessary(this);
                    if (request.Containers.Count == 0)
                    {
                        mBatchedResponses.Remove(mCacheKey);
                    }
                }
            }
        }
Пример #3
0
        protected void OnGetImageError(String cacheKey, VolleyError error)
        {
            BatchedImageRequest request = null;

            mInFlightRequests.TryGetValue(cacheKey, out request);
            mInFlightRequests.Remove(cacheKey);

            if (request != null)
            {
                request.Error = error;
                BatchResponse(cacheKey, request);
            }
        }
Пример #4
0
        protected void OnGetImageSuccess(String cacheKey, Bitmap response)
        {
            //将成功的请求放入缓存
            mCache.PutBitmap(cacheKey, response);

            BatchedImageRequest request = null;

            mInFlightRequests.TryGetValue(cacheKey, out request);
            mInFlightRequests.Remove(cacheKey);

            if (request != null)
            {
                request.ResponseBitmap = response;
                BatchResponse(cacheKey, request);
            }
        }
Пример #5
0
        public ImageContainer Get(String requestUrl, int maxWidth, int maxHeight, Android.Widget.ImageView.ScaleType scaleType,
                                  Action <ImageContainer, bool> successResponse, Action <VolleyError> errorResponse)
        {
            ThrowIfNotOnMainThread();
            if (successResponse == null)
            {
                throw new ArgumentNullException("successResponse不能为null,必须提供请求成功的委托。");
            }

            String cacheKey = GetCacheKey(requestUrl, maxWidth, maxHeight, scaleType);

            //缓存命中直接响应请求
            Bitmap cachedBitmap = mCache.GetBitmap(cacheKey);

            if (cachedBitmap != null)
            {
                ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, mInFlightRequests,
                                                              mBatchedResponses);
                successResponse(container, true);
                return(container);
            }

            //缓存未命中添加到请求中
            ImageContainer imageContainer = new ImageContainer(null, requestUrl, cacheKey, mInFlightRequests,
                                                               mBatchedResponses);

            imageContainer.ErrorResponse   += errorResponse;
            imageContainer.SuccessResponse += successResponse;

            successResponse(imageContainer, true);

            //是否存在相同的请求,存在则添加该请求,否则创建
            BatchedImageRequest request = null;

            mInFlightRequests.TryGetValue(cacheKey, out request);
            if (request != null)
            {
                request.AddContainer(imageContainer);
                return(imageContainer);
            }

            Request newRequest = MakeImageRequest(requestUrl, maxWidth, maxHeight, scaleType, cacheKey);

            mRequestQueue.Add(newRequest);
            mInFlightRequests.Add(cacheKey, new BatchedImageRequest(newRequest, imageContainer));
            return(imageContainer);
        }
 //��Ӧ����
 private void BatchResponse(String cacheKey, BatchedImageRequest request)
 {
     mBatchedResponses.Add(cacheKey, request);
     if (mRunnable == null)
     {
         mRunnable = () =>
             {
                 foreach (BatchedImageRequest bir in mBatchedResponses.Values)
                 {
                     foreach (ImageContainer container in bir.Containers)
                     {
                         if (container.OnSuccessResponse == null)
                         {
                             continue;
                         }
                         if (bir.Error == null)
                         {
                             container.Bitmap = bir.ResponseBitmap;
                             container.OnSuccessResponse(container, false);
                         }
                         else
                         {
                             if (container.OnErrorResponse != null)
                             {
                                 container.OnErrorResponse(bir.Error);
                             }
                         }
                     }
                 }
                 mBatchedResponses.Clear();
                 mRunnable = null;
             };
     }
     mHandler.PostDelayed(mRunnable, mBatchResponseDelayMs);
 }