摘要:從網(wǎng)絡(luò)加載圖片加載從加載從網(wǎng)絡(luò)加載從加載具體的方法實(shí)現(xiàn)接口的類以后再做分析,而從網(wǎng)絡(luò)加載兩步從網(wǎng)絡(luò)獲取數(shù)據(jù)處理數(shù)據(jù)。
4.從網(wǎng)絡(luò)加載
EngineJob current = jobs.get(key); if (current != null) { current.addCallback(cb); if (Log.isLoggable(TAG, Log.VERBOSE)) { logWithTimeAndKey("Added to existing load", startTime, key); } return new LoadStatus(cb, current); } EngineJob engineJob = engineJobFactory.build(key, isMemoryCacheable); DecodeJobdecodeJob = new DecodeJob (key, width, height, fetcher, loadProvider, transformation, transcoder, diskCacheProvider, diskCacheStrategy, priority); EngineRunnable runnable = new EngineRunnable(engineJob, decodeJob, priority); jobs.put(key, engineJob); engineJob.addCallback(cb); engineJob.start(runnable);
EngineRunnable.java:
@Override public void run() { if (isCancelled) { return; } Exception exception = null; Resource> resource = null; try { resource = decode(); } catch (Exception e) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Exception decoding", e); } exception = e; } if (isCancelled) { if (resource != null) { resource.recycle(); } return; } if (resource == null) { onLoadFailed(exception); } else { onLoadComplete(resource); } } private Resource> decode() throws Exception { if (isDecodingFromCache()) { return decodeFromCache(); } else { return decodeFromSource(); } }
圖片加載:
1.從disk加載
2.從網(wǎng)絡(luò)加載
從disk加載
DecodeJob.java:
private ResourceloadFromCache(Key key) throws IOException { File cacheFile = diskCacheProvider.getDiskCache().get(key); if (cacheFile == null) { return null; } Resource result = null; try { result = loadProvider.getCacheDecoder().decode(cacheFile, width, height); } finally { if (result == null) { diskCacheProvider.getDiskCache().delete(key); } } return result; }
具體decode的方法 實(shí)現(xiàn)ResourceDecoder接口的類
以后再做分析,而從網(wǎng)絡(luò)加載
Decoder.java:
private ResourcedecodeSource() throws Exception { Resource decoded = null; try { long startTime = LogTime.getLogTime(); final A data = fetcher.loadData(priority); if (Log.isLoggable(TAG, Log.VERBOSE)) { logWithTimeAndKey("Fetched data", startTime); } if (isCancelled) { return null; } decoded = decodeFromSourceData(data); } finally { fetcher.cleanup(); } return decoded; }
兩步:
1.從網(wǎng)絡(luò)獲取數(shù)據(jù);
2.處理數(shù)據(jù)。
HttpUrlFetcher.java:
private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl, Mapheaders) throws IOException { if (redirects >= MAXIMUM_REDIRECTS) { throw new IOException("Too many (> " + MAXIMUM_REDIRECTS + ") redirects!"); } else { // Comparing the URLs using .equals performs additional network I/O and is generally broken. // See http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html. try { if (lastUrl != null && url.toURI().equals(lastUrl.toURI())) { throw new IOException("In re-direct loop"); } } catch (URISyntaxException e) { // Do nothing, this is best effort. } } urlConnection = connectionFactory.build(url); for (Map.Entry headerEntry : headers.entrySet()) { urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue()); } urlConnection.setConnectTimeout(2500); urlConnection.setReadTimeout(2500); urlConnection.setUseCaches(false); urlConnection.setDoInput(true); // Connect explicitly to avoid errors in decoders if connection fails. urlConnection.connect(); if (isCancelled) { return null; } final int statusCode = urlConnection.getResponseCode(); if (statusCode / 100 == 2) { return getStreamForSuccessfulRequest(urlConnection); } else if (statusCode / 100 == 3) { String redirectUrlString = urlConnection.getHeaderField("Location"); if (TextUtils.isEmpty(redirectUrlString)) { throw new IOException("Received empty or null redirect url"); } URL redirectUrl = new URL(url, redirectUrlString); return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers); } else { if (statusCode == -1) { throw new IOException("Unable to retrieve response code from HttpUrlConnection."); } throw new IOException("Request failed " + statusCode + ": " + urlConnection.getResponseMessage()); } }
3XX重定向
URL redirectUrl = new URL(url, redirectUrlString); return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers);
處理數(shù)據(jù):
private ResourcedecodeFromSourceData(A data) throws IOException { final Resource decoded; if (diskCacheStrategy.cacheSource()) { decoded = cacheAndDecodeSourceData(data); } else { long startTime = LogTime.getLogTime(); decoded = loadProvider.getSourceDecoder().decode(data, width, height); if (Log.isLoggable(TAG, Log.VERBOSE)) { logWithTimeAndKey("Decoded from source", startTime); } } return decoded; }
具體decode的方法 實(shí)現(xiàn)ResourceDecoder接口的類
EngineJob.java:
private final ExecutorService diskCacheService; public void start(EngineRunnable engineRunnable) { this.engineRunnable = engineRunnable; future = diskCacheService.submit(engineRunnable); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/70705.html
摘要:請(qǐng)求處理將要加載的資源放入到其中這段代碼涉及屬性暫且放到一邊,而這段代碼表明當(dāng)同一個(gè)上要加載兩個(gè)資源,不論資源是否一致,將前一個(gè)資源請(qǐng)求取消創(chuàng)建新的資源請(qǐng)求綁定有兩個(gè)列表,用來存儲(chǔ)所有的和暫停掉的加載圖片的過程開啟資源請(qǐng)求加載圖片分幾步 2.Request請(qǐng)求處理 DrawableRequestBuilder.into(ImageView)RequestManager.load將要加載...
閱讀 2672·2021-11-25 09:43
閱讀 2479·2021-09-22 15:29
閱讀 994·2021-09-22 15:17
閱讀 3637·2021-09-03 10:36
閱讀 2233·2019-08-30 13:54
閱讀 1752·2019-08-30 11:23
閱讀 1170·2019-08-29 16:58
閱讀 1299·2019-08-29 16:14