Skip to content

Commit

Permalink
修复加载错位bug ing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zane96 committed Oct 17, 2016
1 parent f13f509 commit 39c963a
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Bitmap call() throws Exception {
finalStream = BitmapDecode.byteTOInputStream(data);
inputStream = BitmapDecode.byteTOInputStream(data);

Log.i("BitmapCallback", conn.getInputStream() + " is");
Log.i("BitmapCallback", conn.getInputStream() + " is " + request.ID);
//高效加载
bitmap = BitmapDecode.decodeRequestBitmap(inputStream, finalStream, request.getImageViewWidth(), request.getImageViewHeight());
//bitmap = BitmapFactory.decodeStream(is);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.zane.easyimageprovider.download.execute;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
* 通过虚引用保存一个Map,里面是Callable,通过Callable来获得ImageView对应的Task的
* url,和BitmapRequest的url进行比对,如果不相同那么将value Future实例cancle掉
* Created by Zane on 16/10/16.
* Email: [email protected]
* Blog: zane96.github.io
*/

public class ContainerDrawable extends BitmapDrawable{

private Reference<Map<Callable, Future>> container;

public ContainerDrawable(Map<Callable, Future> containerMap){
container = new WeakReference<Map<Callable, Future>>(containerMap);
}

public Map<Callable, Future> getContainerMap(){
return container.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.zane.easyimageprovider.download.EasyImageLoadConfiguration;
import com.example.zane.easyimageprovider.download.request.BitmapRequest;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

Expand All @@ -22,28 +23,35 @@ public class LoadTask<Bitmap> extends FutureTask<Bitmap> implements Comparable<L

//实现和BitmapRequest一样的ID,这是为了在线程的等待对立里面仍然是一种优先级的分发任务
private int policy;
//进行错位判断
private String url;

public LoadTask(Callable<Bitmap> callable) {
super(callable);

if (!(callable instanceof ThreadPoolQueuePolicy)){
throw new IllegalArgumentException("callable should be implements ThreadPoolQueuePolicy!");
}
policy = ((ThreadPoolQueuePolicy) callable).getPolicy();
Log.i("LoadTask", policy + " ID 1");
}

public LoadTask(Runnable runnable, Bitmap result){
super(runnable, result);
if (!(runnable instanceof Comparable)){
throw new IllegalArgumentException("runnable should be implements Comparable!");
}

public String getUrl(){
return url;
}

//
// public LoadTask(Runnable runnable, Bitmap result){
// super(runnable, result);
// if (!(runnable instanceof Comparable)){
// throw new IllegalArgumentException("runnable should be implements Comparable!");
// }
//
// }

@Override
public int compareTo(LoadTask<Bitmap> another) {
Log.i("LoadTask", policy + " ID 2");
Log.i("LoadTask", policy + " " + another.policy + " ID 2");
return EasyImageLoadConfiguration.getInstance().getLoadPolicy().compare(policy, another.policy);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,9 @@ public LoadThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAl
*/
@Override
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
Log.i("LoadThreadPoolExecutor", "newTaskfor callable");
return new LoadTask<>(callable);
}

@Override
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
Log.i("LoadThreadPoolExecutor", "newTaskfor runnable");
return new LoadTask<>(runnable, value);
}

//自定义线程工厂,设置优先级和自定义name
private static class MyCoustomeThreadFactory implements ThreadFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import android.graphics.Bitmap;
import android.util.Log;
import android.widget.Toast;

import com.example.zane.easyimageprovider.download.execute.BitmapCallback;
import com.example.zane.easyimageprovider.download.execute.LoadTask;
import com.example.zane.easyimageprovider.download.execute.LoadThreadPoolExecutor;
import com.example.zane.easyimageprovider.download.request.BitmapRequest;
import com.example.zane.easyimageprovider.utils.BitmapDecode;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
Expand All @@ -27,47 +29,73 @@ public class NetLoader implements ImageLoader{
private Bitmap bitmap;
private UIImageViewLoader loader;

private Thread startLoader;

public NetLoader(ThreadPoolExecutor executorService){
executor = executorService;
}

@Override
public void loadImage(final BitmapRequest request) {
loader = new UIImageViewLoader(request);
if (loader.beforeLoad()){
startTask(request);
}

private void startTask(final BitmapRequest request) {
if (loader.beforeLoad()) {
callback = new BitmapCallback(request);
future = executor.submit(callback);
loader.showLoading(request.placeHolderId);

new Thread(new Runnable() {
startLoader = new Thread(new Runnable() {
@Override
public void run() {
try {
bitmap = future.get();
Log.i("NetLoader", bitmap + " getBitmap");
if (request.getImageView() != null){
if (bitmap != null){
//注意,这里的bitmap已经是压缩了的
loader.loadImageView(bitmap);
} else if (!request.getImageView().getTag().equals(request.uri)) {
//防止Recycleview的回收机制导致显示了错误图片
loader.showLoading(request.placeHolderId);
if (!Thread.currentThread().isInterrupted()){
bitmap = future.get();
}
} catch (InterruptedException e){
Log.i("NetLoader", "thread intrrupt");
Thread.currentThread().interrupt();
} catch (CancellationException e){
Log.i("NetLoader", "cancle computation");
Thread.currentThread().interrupt();
} catch (ExecutionException e){
Log.i("NetLoader", "computation error");
}

Log.i("NetLoader", bitmap + " getBitmap " + request.ID);
if (request.getImageView() != null){
if (bitmap != null && !request.getImageView().getTag().equals(request.uri)){
//注意,这里的bitmap已经是压缩了的
loader.loadImageView(bitmap);
} else {
Log.i("NetLoader", "error " + request.getImageView().getTag().equals(request.uri));
if (Thread.currentThread().isInterrupted()){
Log.i("NetLoader", "error by thread intrrupted");
} else {
Log.i("NetLoader", "error " + request.getImageView().getTag().equals(request.uri));
loader.showError(request.errorId);
}
} else {
Log.i("NetLoader", "imageview reference is null!");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} else {
Log.i("NetLoader", "imageview reference is null!");
}
}
}).start();
});

startLoader.start();

} else {
loader.loadImageViewInCache();
}
}

/**
* 中断加载线程
*/
public void cancelLoader(){
if (startLoader.isAlive() && !startLoader.isInterrupted()){
startLoader.interrupt();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.zane.easyimageprovider.download.policy;

import android.util.Log;

import com.example.zane.easyimageprovider.download.execute.LoadTask;
import com.example.zane.easyimageprovider.download.request.BitmapRequest;

Expand All @@ -16,6 +18,7 @@ public int compare(BitmapRequest request1, BitmapRequest request2) {
if (request1.ID == request2.ID){
throw new IllegalStateException("the ID in two imageview cann't be same!");
}
Log.i("FIFOPolicy", request1.ID + " " + request2.ID);
return request1.ID - request2.ID;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ private void init(){
policy = EasyImageLoadConfiguration.getInstance().getLoadPolicy();
uri = r.uri;
uriHead = r.uriHead;
//防止list加载乱序
// TODO: 16/9/24 防止乱序测试
imageViewReference = new WeakReference<ImageView>(r.imageView);
r.imageView.setTag(uri);
imageViewReference = new WeakReference<ImageView>(r.imageView);
cache = r.imageCache;
errorId = r.errorId;
placeHolderId = r.holderPlaceId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.zane.easyimageprovider.provider.listener;

import android.graphics.Bitmap;
import android.net.Uri;

import java.util.List;

/**
* Created by Zane on 16/5/13.
* 回调抽象
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/java/com/example/zane/sample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class App extends Application{
public void onCreate() {
super.onCreate();
EasyImageLoadConfiguration.getInstance()
.setLoadPolicy(new FIFOPolicy())
.setLoadPolicy(new FILOPolicy())
.setThreadCount(threadCount)
.init(this);
}
Expand Down
91 changes: 77 additions & 14 deletions sample/src/main/java/com/example/zane/sample/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,83 @@
final class Data {
static final String BASE = "http://i.imgur.com/";
static final String EXT = ".jpg";
static final String[] URLS = {
BASE + "CqmBjo5" + EXT, BASE + "zkaAooq" + EXT, BASE + "0gqnEaY" + EXT,
BASE + "9gbQ7YR" + EXT, BASE + "aFhEEby" + EXT, BASE + "0E2tgV7" + EXT,
BASE + "P5JLfjk" + EXT, BASE + "nz67a4F" + EXT, BASE + "dFH34N5" + EXT,
BASE + "FI49ftb" + EXT, BASE + "DvpvklR" + EXT, BASE + "DNKnbG8" + EXT,
BASE + "yAdbrLp" + EXT, BASE + "55w5Km7" + EXT, BASE + "NIwNTMR" + EXT,
BASE + "DAl0KB8" + EXT, BASE + "xZLIYFV" + EXT, BASE + "HvTyeh3" + EXT,
BASE + "Ig9oHCM" + EXT, BASE + "7GUv9qa" + EXT, BASE + "i5vXmXp" + EXT,
BASE + "glyvuXg" + EXT, BASE + "u6JF6JZ" + EXT, BASE + "ExwR7ap" + EXT,
BASE + "Q54zMKT" + EXT, BASE + "9t6hLbm" + EXT, BASE + "F8n3Ic6" + EXT,
BASE + "P5ZRSvT" + EXT, BASE + "jbemFzr" + EXT, BASE + "8B7haIK" + EXT,
BASE + "aSeTYQr" + EXT, BASE + "OKvWoTh" + EXT, BASE + "zD3gT4Z" + EXT,
BASE + "z77CaIt" + EXT,
};

//16M一张图片
// static final String[] URLS = {
// BASE + "CqmBjo5" + EXT, BASE + "zkaAooq" + EXT, BASE + "0gqnEaY" + EXT,
// BASE + "9gbQ7YR" + EXT, BASE + "aFhEEby" + EXT, BASE + "0E2tgV7" + EXT,
// BASE + "P5JLfjk" + EXT, BASE + "nz67a4F" + EXT, BASE + "dFH34N5" + EXT,
// BASE + "FI49ftb" + EXT, BASE + "DvpvklR" + EXT, BASE + "DNKnbG8" + EXT,
// BASE + "yAdbrLp" + EXT, BASE + "55w5Km7" + EXT, BASE + "NIwNTMR" + EXT,
// BASE + "DAl0KB8" + EXT, BASE + "xZLIYFV" + EXT, BASE + "HvTyeh3" + EXT,
// BASE + "Ig9oHCM" + EXT, BASE + "7GUv9qa" + EXT, BASE + "i5vXmXp" + EXT,
// BASE + "glyvuXg" + EXT, BASE + "u6JF6JZ" + EXT, BASE + "ExwR7ap" + EXT,
// BASE + "Q54zMKT" + EXT, BASE + "9t6hLbm" + EXT, BASE + "F8n3Ic6" + EXT,
// BASE + "P5ZRSvT" + EXT, BASE + "jbemFzr" + EXT, BASE + "8B7haIK" + EXT,
// BASE + "aSeTYQr" + EXT, BASE + "OKvWoTh" + EXT, BASE + "zD3gT4Z" + EXT,
// BASE + "z77CaIt" + EXT,
// };

static final String[] URLS = {"http://img.my.csdn.net/uploads/201508/05/1438760758_3497.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760758_6667.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760757_3588.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760756_3304.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760755_6715.jpeg",
"http://img.my.csdn.net/uploads/201508/05/1438760726_5120.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760726_8364.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760725_4031.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760724_9463.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760724_2371.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760707_4653.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760706_6864.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760706_9279.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760704_2341.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760704_5707.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760685_5091.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760685_4444.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760684_8827.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760683_3691.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760683_7315.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760663_7318.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760662_3454.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760662_5113.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760661_3305.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760661_7416.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760589_2946.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760589_1100.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760588_8297.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760587_2575.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760587_8906.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760550_2875.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760550_9517.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760549_7093.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760549_1352.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760548_2780.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760531_1776.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760531_1380.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760530_4944.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760530_5750.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760529_3289.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760500_7871.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760500_6063.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760499_6304.jpeg",
"http://img.my.csdn.net/uploads/201508/05/1438760499_5081.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760498_7007.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760478_3128.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760478_6766.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760477_1358.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760477_3540.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760476_1240.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760446_7993.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760446_3641.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760445_3283.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760444_8623.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760444_6822.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760422_2224.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760421_2824.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760420_2660.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760420_7188.jpg",
"http://img.my.csdn.net/uploads/201508/05/1438760419_4123.jpg"};

private Data() {
// No instances.
Expand Down

0 comments on commit 39c963a

Please sign in to comment.