利用Glide怎么實(shí)現(xiàn)一個(gè)加載進(jìn)度條功能

利用Glide怎么實(shí)現(xiàn)一個(gè)加載進(jìn)度條功能?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

創(chuàng)新互聯(lián)公司秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營(yíng)銷(xiāo)的理念,以專(zhuān)業(yè)定制企業(yè)官網(wǎng),網(wǎng)站制作、網(wǎng)站設(shè)計(jì),微信小程序,網(wǎng)頁(yè)設(shè)計(jì)制作,手機(jī)網(wǎng)站制作,全網(wǎng)整合營(yíng)銷(xiāo)推廣幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級(jí)專(zhuān)業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對(duì)客戶(hù)都以感恩的心態(tài)奉獻(xiàn)自己的專(zhuān)業(yè)和所長(zhǎng)。

使用

Glide.with(MainActivity.this).using(new ProgressModelLoader(
new ProgressHandler(MainActivity.this, progressImageView))). 
load("http://image2.sina.com.cn/dy/o/2004-11-10/1100077821_2laygS.jpg")  
.diskCacheStrategy(DiskCacheStrategy.NONE).into(progressImageView.getImageView());

思路

Glide的圖片下載底層用的是OkHttp,它已經(jīng)實(shí)現(xiàn)好了,所有要實(shí)現(xiàn)進(jìn)度條加載,就必須要知道圖片下載的進(jìn)度,就要自己來(lái)寫(xiě)圖片的下來(lái)實(shí)現(xiàn),但是Glide支持不支持呢?網(wǎng)上查了一下發(fā)現(xiàn)它有個(gè)方法

public <T> ImageModelRequest<T> using(final StreamModelLoader<T> modelLoader) { 
  return new ImageModelRequest<T>(modelLoader);
}

這個(gè)方法可以指定圖片請(qǐng)求loader,我們創(chuàng)建一個(gè)ProgressModelLoader類(lèi),實(shí)現(xiàn)StreamModelLoader接口

public class ProgressModelLoader implements StreamModelLoader<String> { 

 private Handler handler; 

 public ProgressModelLoader(Handler handler) {  
  this.handler = handler; 
 } 

 @Override 
 public DataFetcher<InputStream> getResourceFetcher(String model, int width, int height) {  
  return new ProgressDataFetcher(model, handler); 
 }
}

重寫(xiě)getResourceFetcher方法,這個(gè)方法返回一個(gè)DataFetcher類(lèi),這個(gè)類(lèi)是個(gè)數(shù)據(jù)提取類(lèi),是個(gè)接口,重寫(xiě)它的loadData方法來(lái)下載圖片,我們來(lái)看下我創(chuàng)建的ProgressDataFetcher對(duì)loadData方法的重寫(xiě)

@Override
public InputStream loadData(Priority priority) throws Exception { 
 Request request = new Request.Builder().url(url).build(); 
 OkHttpClient client = new OkHttpClient(); 
 client.interceptors().add(new ProgressInterceptor(getProgressListener())); 

 try {  
  progressCall = client.newCall(request);  
  Response response = progressCall.execute();  
  if (isCancelled) {   
  return null;  
  }  

  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);  
  stream = response.body().byteStream(); 
  } catch (IOException e) {  
  e.printStackTrace();  
  return null; 
  } 
  return stream;
 }

使用okhttp下載圖片,添加一個(gè)攔截器

public class ProgressInterceptor implements Interceptor { 

 private ProgressListener progressListener; 

 public ProgressInterceptor(ProgressListener progressListener) { 
  this.progressListener = progressListener; 
 } 

 @Override 
 public Response intercept(Chain chain) throws IOException { 
  Response originalResponse = chain.proceed(chain.request()); 
 return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), progressListener)).build(); 
 }
}

重寫(xiě)intercept方法,創(chuàng)建一個(gè)ProgressResponseBody得到圖片下載的進(jìn)度,來(lái)看一下讀流的方法

private Source source(Source source) { 
 return new ForwardingSource(source) {  
  long totalBytesRead = 0;  
 @Override  
 public long read(Buffer sink, long byteCount) throws IOException {   
  long bytesRead = super.read(sink, byteCount);  
  totalBytesRead += bytesRead != -1 &#63; bytesRead : 0;  
  if(progressListener != null) 
  progressListener.progress(totalBytesRead, responseBody.contentLength(), bytesRead == -1);   
  return bytesRead;  
 } 
 };
}

把讀到的bytesRead和responseBody.contentLength()傳給回調(diào)方法progressListener.progress來(lái)計(jì)算進(jìn)度。

看完上述內(nèi)容,你們掌握利用Glide怎么實(shí)現(xiàn)一個(gè)加載進(jìn)度條功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

標(biāo)題名稱(chēng):利用Glide怎么實(shí)現(xiàn)一個(gè)加載進(jìn)度條功能
瀏覽路徑:http://bm7419.com/article0/igieio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站改版、面包屑導(dǎo)航網(wǎng)站策劃、品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作