Glide是用起来相对简单方便,功能比较强大的图片加载框架,在此强烈推荐,下面部分为参考(chaoxi)各位大神而来的一些知识点,在此记录备用。
引用
compile 'com.github.bumptech.glide:glide:3.7.0'
使用
Glide.with(mContext)
.load(url)
.dontAnimate()
.placeholder(R.drawable.loading_spinner)
.into(circleImageview);
方法介绍
引用
compile 'jp.wasabeef:glide-transformations:2.0.0'
使用
Glide.with(view.getContext())
.load(picUrl)
.centerCrop()
.placeholder(place) // 也可以是一个drawable
.override(width, height) // 重新改变图片大小成这些尺寸(像素).不关心长宽比
.error(R.mipmap.main_pic0) // 如果图片不能加载就会显示
.animate(R.anim.item_alpha_in ) //或者R.anim.zoom_in
//.bitmapTransform(new BlurTransformation(view.getContext(), 25), new CropCircleTransformation(view.getContext()))
.bitmapTransform(new RoundedCornersTransformation(view.getContext(), 10, 0, RoundedCornersTransformation.CornerType.ALL))
.thumbnail(0.1f)
.into(view);
加载进度条
Glide
Glide.with(context).using(new ImageUrlLoader(this)).load(new GlideUrl(picUrl))
ImageUrlLoader
public class ImageUrlLoader implements StreamModelLoader<GlideUrl> {
private InputStreamReadCallback inputStreamReadCallback;
public ImageUrlLoader(InputStreamReadCallback inputStreamReadCallback) {
this.inputStreamReadCallback = inputStreamReadCallback;
}
@Override
public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) {
//这里简单粗暴,直接返回用于获得网络InputStream的DataFetcher。内部才是真正读网络的逻辑,所以将读网络的回调传进去
return new HttpUrlFetcher(model, inputStreamReadCallback);
}
}
HttpUrlFetcher
/*
A DataFetcher that retrieves an {@link java.io.InputStream} for a Url.
/
public class HttpUrlFetcher implements DataFetcher<InputStream> {
private static final String TAG = "HttpUrlFetcher";
private static final int MAXIMUM_REDIRECTS = 5;
private final GlideUrl glideUrl;
private HttpURLConnection urlConnection;
private InputStream stream;
private volatile boolean isCancelled;
private InputStreamReadCallback inputStreamReadCallback;
// Visible for testing.
HttpUrlFetcher(GlideUrl glideUrl, InputStreamReadCallback inputStreamReadCallback) {
this.glideUrl = glideUrl;
this.inputStreamReadCallback = inputStreamReadCallback;
}
@Override
public InputStream loadData(Priority priority) throws Exception {
return loadDataWithRedirects(glideUrl.toURL(), 0 /redirects/, null /lastUrl*/, glideUrl.getHeaders());
}
private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl, Map<String, String> headers)
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 = (HttpURLConnection) url.openConnection();
for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue());
}
urlConnection.setConnectTimeout(5500);
urlConnection.setReadTimeout(5500);
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, inputStreamReadCallback);
} 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());
}
}
private InputStream getStreamForSuccessfulRequest(HttpURLConnection urlConnection, InputStreamReadCallback inputStreamReadCallback)
throws IOException {
if (TextUtils.isEmpty(urlConnection.getContentEncoding())) {
int contentLength = urlConnection.getContentLength();
//修改这里,对网络返回的InputStream做一个包装,并提供回调的接口
stream = ContentLengthReadCallbackInputStream.obtain(urlConnection.getInputStream(), contentLength, inputStreamReadCallback);
} else {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got non empty content encoding: " + urlConnection.getContentEncoding());
}
stream = urlConnection.getInputStream();
}
return stream;
}
@Override
public void cleanup() {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Ignore
}
}
if (urlConnection != null) {
urlConnection.disconnect();
}
}
@Override
public String getId() {
return glideUrl.getCacheKey();
}
@Override
public void cancel() {
// TODO: we should consider disconnecting the url connection here, but we can't do so directly because cancel is
// often called on the main thread.
isCancelled = true;
}
}
ContentLengthReadCallbackInputStream
public final class ContentLengthReadCallbackInputStream extends FilterInputStream {
private InputStreamReadCallback readCallback;
private final long contentLength;
private int readSoFar;
public static InputStream obtain(InputStream other, long contentLength, InputStreamReadCallback readCallback) {
return new ContentLengthReadCallbackInputStream(other, contentLength, readCallback);
}
ContentLengthReadCallbackInputStream(InputStream in, long contentLength, InputStreamReadCallback readCallback) {
super(in);
this.readCallback = readCallback;
this.contentLength = contentLength;
}
@Override
public synchronized int available() throws IOException {
return (int) Math.max(contentLength - readSoFar, in.available());
}
@Override
public synchronized int read() throws IOException {
return checkReadSoFarOrThrow(super.read());
}
@Override
public int read(byte[] buffer) throws IOException {
return read(buffer, 0 /byteOffset/, buffer.length /byteCount/);
}
@Override
public synchronized int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
return checkReadSoFarOrThrow(super.read(buffer, byteOffset, byteCount));
}
private int checkReadSoFarOrThrow(int read) throws IOException {
if (read >= 0) {
readSoFar += read;
} else if (contentLength - readSoFar > 0) {
throw new IOException("Failed to read all expected data"
+ ", expected: " + contentLength
+ ", but read: " + readSoFar);
}
if (readCallback != null) {
//实际网络读取数和总长度在这里回调到外部
readCallback.onRead(readSoFar, contentLength);
}
return read;
}
}
参考大神 : 倾城_之泪 http://www.jianshu.com/p/4a3177b57949
参考大神:总李写代码 http://www.cnblogs.com/whoislcj/p/5558168.html
参考大神:http://www.jianshu.com/p/8bf041b2c5be
参考大神:http://www.jianshu.com/p/c9efd313e79e