<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Android圖片異步加載

      開發Android程序,一般情況下都會有兩個操作,圖片的異步加載與緩存,而圖片的異步加載大都是從網絡讀取圖片(還有生成本地圖片縮略圖等操作),為了減少網絡操作,加快圖片加載速度就需要對圖片進行緩存,所以網上的好多圖片異步加載方法都是與圖片的緩存緊密關聯的。但也有可能用戶已經有了緩存的相關類庫,這樣使用起來就會有點麻煩。

      最近一段處理跟圖片相關的問題,本來是自己寫的圖片加載,不過有些狀態的控制還是比較煩人的,比如ListView滾動時ImageView的重用,所以本著偷懶與充分利用現有資源的態度去網上搜羅圖片異步加載的代碼,最終在GreenDroid UI庫中找到一個,其中有個AsyncImageView的自定義View用于異步加載圖片,不過也像網上的大多數圖片異步加載方法一樣,是跟圖片的緩存關聯在一起的,不過只是很簡單的內存緩存,無文件緩存。圖片的加載方法也如其他的一樣是寫死了的,這就限制了其使用范圍,只可通過InputStream來decode圖片,而像生成縮略圖或其他一些圖片處理的異步處理就無法用途。修改現有類庫總比自己從頭寫來的簡單,于是稍微修改了下AsyncImageView,使其可以自定義緩存與圖片加載方法,對于AsyncImageView只有一點點的修改,大都是別人源碼。

      1. 核心類

      • ImageLoader:圖片加載核心類,內部使用線程池加載圖片
      • ImageRequest:表示一個圖片加載的請求
      • AsyncImageView:自定義的圖片異步加載View
      • LoadMethod:自定義圖片加載方法的接口,可以通過實現此接口來自定義圖片的加載方法
      • CacheCallback:緩存接口,可以通過實現此接口實現對緩存的讀寫
      • AsyncImageView.OnImageViewLoadListener:圖片加載狀態監聽(開始,失敗,結束)

      2. 圖片加載方法

      public void run() {
      
          Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
      
          final Handler h = mHandler;
          Bitmap bitmap = null;
          Throwable throwable = null;
      
          h.sendMessage(Message.obtain(h, ON_START));
      
          try {
              if (TextUtils.isEmpty(mUrl)) {
                  throw new Exception("The given URL cannot be null or empty");
              }
      
              // 如果自定義了加載方法,則用自定義的方法
              if (mLoadMethod != null) {
                  bitmap = mLoadMethod.load(mUrl);
              } else {
      
                  InputStream inputStream = null;
      
                  // Asset
                  if (mUrl.startsWith("file:///android_asset/")) {
                      inputStream = sAssetManager.open(mUrl.replaceFirst(
                              "file:///android_asset/", ""));
                  }
                  // File
                  else if (mUrl.startsWith("file:///") || mUrl.startsWith("/")) {
                      if (mUrl.startsWith("file:///"))
                          mUrl = mUrl.replaceFirst("file:///", "/");
                      inputStream = new FileInputStream(mUrl);
                  }
                  // NetWork
                  else {
                      // 在用URL類加載圖片時,發現有的機型上面通過URL類獲得的InputStream解析獲得的圖片總是null,故使用HttpClient
                      HttpGet httpRequest = new HttpGet(mUrl);
                      HttpClient httpclient = new DefaultHttpClient();
                      HttpParams httpParams = new BasicHttpParams();
                      HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
                      HttpConnectionParams.setSoTimeout(httpParams, 5000);
                      httpRequest.setParams(httpParams);
                      HttpResponse response = (HttpResponse)httpclient.execute(httpRequest);
                      HttpEntity entity = response.getEntity();
                      BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
                      InputStream instream = bufHttpEntity.getContent();
                      BufferedInputStream bi = new BufferedInputStream(instream);
                      inputStream = bi;
                  }
      
                  // 雖然AsyncImageView中有設置BitmapFactory.Options的方法,但一般情況下都未知圖片的大小,也就無法計算相應的inSampleSize,
                  // 也就無法設置相應的BitmapFactory.Options,所以一般情況下還是根據自己的需要自定義LoadMethod為好
                  bitmap = BitmapFactory.decodeStream(inputStream, null,
                          (mOptions == null) ? sDefaultOptions : mOptions);
                  inputStream.close();
              }
              if (mBitmapProcessor != null && bitmap != null) {
                  final Bitmap processedBitmap = mBitmapProcessor.processImage(bitmap);
                  if (processedBitmap != null) {
                      bitmap.recycle();
                      bitmap = processedBitmap;
                  }
              }
      
          } catch (Exception e) {
              Log.e(LOG_TAG, "Error while fetching image", e);
              throwable = e;
          }
      
          if (bitmap == null) {
              if (throwable == null) {
                  throwable = new Exception("Skia image decoding failed");
              }
              h.sendMessage(Message.obtain(h, ON_FAIL, throwable));
          } else {
              h.sendMessage(Message.obtain(h, ON_END, bitmap));
              if (mCache != null) {
                  mCache.writeCache(TextUtils.isEmpty(mCacheKey) ? mUrl : mCacheKey, bitmap);
              }
          }
      }

      如果自定義了LoadMethod,會調用相應的方法加載圖片,如果沒有自定義,會使用默認的加載方法,可以加載本地圖片,Asset圖片與網絡圖片,GreenDroid的源碼中加載網絡圖片是用的URL的,但我們以前在加載網絡圖片時遇到一個問題,有的機型通過URL類獲得的ImputStream解析圖片總是返回null,所以就改為了HttpClient。

      3.使用方法

      通過AsyncImageView的setPath方法來加載圖片,setPath有3個重載方法:

      • public void setPath(String path)
      • public void setPath(String path, LoadMethod loadMethod)
      • public void setPath(String path, LoadMethod loadMethod, String cacheKey)

      第一個參數指定要加載的圖片的路徑,第二個參數為自定義的圖片加載方法,若不指定則用默認的。

      至于加第三個參數,是做緩存用的,一般要加載的圖片的路徑都是唯一的,所以一般用第一個參數來做為緩存的Key就行了,但也有特殊情況,比如讀取局域網中的圖片,一般都是自動獲取IP,所以根據圖片路徑做為緩存的Key可能是不合適的,所以就需要根據需要手動指定用來作為緩存的Key。

      /**
           * 設置要加載的圖片的路徑, 可為網絡路徑, Asset文件路徑(file:///android_asset), 本地圖片路徑(file:///或/)
           * 
           * @param path 要加載的圖片的路徑, 若為null則加載默認圖片
           * @param loadMethod 自定義的圖片加載的方法, 可以null, 使用默認的加載方法
           * @param cacheKey 緩存key
           */
          public void setPath(String path, LoadMethod loadMethod, String cacheKey) {
      
              // Check the url has changed
              if (mBitmap != null && path != null && path.equals(mUrl)) { // TODO mBitmap != null necessary?
                  return;
              }
      
              stopLoading();
              mUrl = path;
              mCacheKey = cacheKey;
              mLoadMethod = loadMethod;
      
              // Setting the url to an empty string force the displayed image to the
              // default image
              if (TextUtils.isEmpty(mUrl)) {
                  mBitmap = null;
                  setDefaultImage();
              } else {
                  if (!mPaused) {
                      reload();
                  } else {
                      // We're paused: let's look in a synchronous and efficient cache
                      // prior using the default image.
                      mBitmap = readCache(); // TODO 可能會耗時間
                      if (mBitmap != null) {
                          setImageBitmap(mBitmap);
                      } else {
                          setDefaultImage();
                      }
                  }
              }
          }
      public void reload(boolean force) {
              if (mRequest == null && mUrl != null) {
      
                  // Prior downloading the image ... let's look in a cache !
                  mBitmap = null;
                  if (!force) {
                      // This may take a long time.
                      mBitmap = readCache();
                  }
      
                  if (mBitmap != null) {
                      setImageBitmap(mBitmap);
                      return;
                  }
      
                  setDefaultImage();
                  mRequest = new ImageRequest(mUrl, this, mImageProcessor, mOptions, mCacheKey);
                  mRequest.load(getContext(), mLoadMethod);
                  if (ImageLoader.getInstance() != null && ImageLoader.getInstance().getCache() == null) {
                      ImageLoader.getInstance().setCache(mCache);
                  }
              }

      readCache()用于讀取緩存,代碼如下:

          private Bitmap readCache() {
              if (mCache != null)
                  return mCache.readCache(TextUtils.isEmpty(mCacheKey) ? mUrl : mCacheKey);
              return null;
          }

      其中的mCache由用戶能過setCacheCallback(CacheCallback callback)設置用戶自定義的緩存方法,由此將圖片的加載與緩存分離開,使用戶可以使用現有的緩存實現。如要用戶指定了緩存Key就使用用戶指定的Key,否則就用圖片的路徑作Key。

      4.AsyncImageView中的其他重要方法

      • reload([boolean force]):重新加載
      • stopLoading():停止加載,如果當前正在加載則沒有效果,如果加載任務在加載線程池隊列中則取消。
      • setDefaultImage...()類方法:設置默認圖片。
      • setPause(boolean pause):是否加載圖片

       

      源碼下載:AsyncImage

      posted @ 2012-09-16 02:19  AngelDevil  閱讀(34567)  評論(6)    收藏  舉報
      主站蜘蛛池模板: 一本一道久久综合狠狠老| 2020年最新国产精品正在播放| 一级国产在线观看高清| 99久久国产综合精品成人影院| 成人中文在线| 国产99青青成人A在线| 日本乱子人伦在线视频| 久久亚洲女同第一区综合| 久久精品国产亚洲av熟女| 丝袜人妻一区二区三区网站| 久久综合亚洲色一区二区三区| 久久精品国产99精品亚洲| 国产精品多p对白交换绿帽| 亚洲av永久无码天堂影院| 亚洲乱码精品久久久久..| 亚洲一区二区三区在线激情| 厨房与子乱在线观看| 日本亲近相奷中文字幕| 亚洲午夜精品久久久久久抢| 国产精品福利在线观看无码卡一 | 成人网站免费看黄a站视频| 亚洲国产成人无码av在线播放| 在线视频一区二区三区色| 激情在线网| 色欧美片视频在线观看| 四虎国产精品久久免费精品| 国语精品国内自产视频| 日本极品少妇videossexhd| 春菜花亚洲一区二区三区| 精品国产一区av天美传媒| 亚洲无人区码一二三四区| 亚洲一区av在线观看| 小嫩模无套内谢第一次| 欧美成人午夜精品免费福利| 国产久免费热视频在线观看 | 亚洲人午夜精品射精日韩| 国产网友愉拍精品视频手机| 国产日韩精品免费二三氏| 国产精品久久精品| 成人拍拍拍无遮挡免费视频| 国产精品一区二区三区污|