老婆保佑,代碼無(wú)BUG
效果圖
前言
帶有加載進(jìn)度的Glide,看著是不是很好钓瞭,這里也是給自己做個(gè)筆記袍嬉,
一:原理
Glide 默認(rèn)肯定是沒(méi)有這個(gè)功能的,所以小伙辦們就放棄吧厅贪,首先Glide 默認(rèn)加載器蠢护,是HttpUrlConnection,我們要做的是改成OKHttp,通過(guò)okhttp的攔截器,獲取到圖片的大小养涮,就可以實(shí)現(xiàn)監(jiān)聽(tīng)下載進(jìn)度
二: 實(shí)現(xiàn)
菜雞一枚葵硕,就不獻(xiàn)丑了,郭大神也得很好单寂,可以看我上一篇Android - 圖片處理之Glide4.0版本
里面放上了 大神對(duì)Glide 的全面解析贬芥,說(shuō)真的,小伙伴們還是多敲敲代碼宣决,對(duì)自己好蘸劈,
屏幕快照 2018-01-08 下午4.31.44.png
上面的代碼就是我對(duì)著郭大神的敲得,最后會(huì)直接放上尊沸,方便COPY威沫。。洼专。
這就說(shuō)一下如何更換成OKHttp模塊棒掠,畢竟最新的是 4.4版本
@GlideModule
public class OkHttpLibraryGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
//添加攔截器到Glide
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new ProgressInterceptor());
OkHttpClient okHttpClient = builder.build();
//原來(lái)的是 new OkHttpUrlLoader.Factory();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
}
//完全禁用清單解析
@Override
public boolean isManifestParsingEnabled() {
return false;
}
}
package com.allens.lib_glide.ResponseBody;
import android.support.annotation.Nullable;
import android.util.Log;
import com.allens.lib_glide.Impl.ProgressListener;
import com.allens.lib_glide.Interceptor.ProgressInterceptor;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;
/**
* 描述:
* <p>
* Created by allens on 2018/1/8.
*/
public class ProgressResponseBody extends ResponseBody {
private static final String TAG = "XGlide";
private BufferedSource bufferedSource;
private ResponseBody responseBody;
private ProgressListener listener;
public ProgressResponseBody(String url, ResponseBody responseBody) {
this.responseBody = responseBody;
listener = ProgressInterceptor.LISTENER_MAP.get(url);
}
@Nullable
@Override
public MediaType contentType() {
return responseBody.contentType();
}
@Override
public long contentLength() {
return responseBody.contentLength();
}
@Override
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(new ProgressSource(responseBody.source()));
}
return bufferedSource;
}
private class ProgressSource extends ForwardingSource {
long totalBytesRead = 0;
int currentProgress;
ProgressSource(Source source) {
super(source);
}
@Override
public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead = super.read(sink, byteCount);
long fullLength = responseBody.contentLength();
if (bytesRead == -1) {
totalBytesRead = fullLength;
} else {
totalBytesRead += bytesRead;
}
int progress = (int) (100f * totalBytesRead / fullLength);
Log.d(TAG, "download progress is " + progress);
if (listener != null && progress != currentProgress) {
listener.onProgress(progress);
}
if (listener != null && totalBytesRead == fullLength) {
listener = null;
}
currentProgress = progress;
return bytesRead;
}
}
}
package com.allens.lib_glide.Interceptor;
import com.allens.lib_glide.Impl.ProgressListener;
import com.allens.lib_glide.ResponseBody.ProgressResponseBody;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* 描述:
* <p>
* 攔截器
* Created by allens on 2018/1/8.
*/
public class ProgressInterceptor implements Interceptor {
public static final Map<String, ProgressListener> LISTENER_MAP = new HashMap<>();
//入注冊(cè)下載監(jiān)聽(tīng)
public static void addListener(String url, ProgressListener listener) {
LISTENER_MAP.put(url, listener);
}
//取消注冊(cè)下載監(jiān)聽(tīng)
public static void removeListener(String url) {
LISTENER_MAP.remove(url);
}
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
String url = request.url().toString();
ResponseBody body = response.body();
Response newResponse = response.newBuilder().body(new ProgressResponseBody(url, body)).build();
return newResponse;
}
}
package com.allens.lib_glide.Impl;
/**
* 描述:
* <p>
* 進(jìn)度的的監(jiān)聽(tīng)
* Created by allens on 2018/1/8.
*/
public interface ProgressListener {
void onProgress(int progress);
}
最后是使用
final ImageView img = findViewById(R.id.img);
final RoundProgressBar bar = findViewById(R.id.roundProgressBar01_id);//自定義View
bar.setVisibility(View.VISIBLE);
ProgressInterceptor.addListener(url, new ProgressListener() {
@Override
public void onProgress(int progress) {
bar.setProgress(progress);
}
});
RequestOptions options = new RequestOptions()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE);
// .transforms(new CircleTransform(mContext,2, Color.DKGRAY))
// .transforms(new BlackWhiteTransformation());
// .transforms(new BlurTransformation(mContext, 25),new CircleTransform(mContext,2, Color.DKGRAY)) // (0 < r <= 25)
// .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
Glide.with(this)
.load(url)
.apply(options)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
ProgressInterceptor.removeListener(url);
bar.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
ProgressInterceptor.removeListener(url);
bar.setVisibility(View.GONE);
return false;
}
})
.into(img);