带进度值的GlideProgress
GlideProgress 基于
Glide的底层是基于okhttp实现的
implementation 'com.github.bumptech.glide:glide:3.7.0' implementation 'com.squareup.okhttp:okhttp:2.4.0' implementation 'com.squareup.okio:okio:1.5.0'
ProgressResponseBody
private ResponseBody responseBody; private ProgressListener progressListener; private BufferedSource bufferedSource; public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) { this.responseBody = responseBody; this.progressListener = progressListener; } @Override public MediaType contentType() { return responseBody.contentType(); } @Override public long contentLength() { try { return responseBody.contentLength(); } catch (IOException e) { e.printStackTrace(); } return 0; } @Override public BufferedSource source() { if (bufferedSource == null) { try { bufferedSource = Okio.buffer(source(responseBody.source())); } catch (IOException e) { e.printStackTrace(); } } return bufferedSource; } 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 ? bytesRead : 0; if (progressListener != null) progressListener.progress(totalBytesRead, responseBody.contentLength(), bytesRead == -1); return bytesRead; } }; }
代码下载地址,复制粘贴进去就可用