-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleImageLoad.java
More file actions
248 lines (170 loc) · 5.92 KB
/
Copy pathSampleImageLoad.java
File metadata and controls
248 lines (170 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.example.kongalong.ximalaya_mvp.imageLoad;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.ImageView;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by kongalong on 2016/11/7.
*/
public class SampleImageLoad {
//创建线程池
public static final ThreadFactory mThreadFactory = new ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"ImageLoader#" + mCount.getAndIncrement());
}
};
public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
Constants.CORE_POOL_SIZE,
Constants.MAX_POOL_SIZE,
Constants.THREAD_KEEP_ALIVE,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
mThreadFactory
);
//使用handler更新ui
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case Constants.MESSAGE_ONE:
Obj myObj = (Obj) msg.obj;
//判断标记,防止错位
if(myObj.mTag.equals((String)myObj.mImageView.getTag())){
myObj.mImageView.setImageBitmap(myObj.mBitmap);
}
break;
}
}
};
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public Bitmap beginLoadBitmap(String url){
Bitmap bitmap = null;
//先从lruCache获取
bitmap = mCache.loadDataFromMem(url);
if(bitmap!=null){
Log.d("flag", "beginLoadBitmap: 从内存获取");
return bitmap;
}
//再从disk缓存获取
bitmap = mCache.loadDataFromDisk(url,mParams.mWidth,mParams.mHeight);
if(bitmap != null){
Log.d("flag", "beginLoadBitmap: 从磁盘获取");
return bitmap;
}
//最后从网络加载
return mLoadImage.laodDataByHttp(url,mCache,mParams.mWidth,mParams.mHeight);
}
private Params mParams;
private LoadImage mLoadImage;
private Caches mCache;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public SampleImageLoad(Context context){
//创建图片缓存对象
mCache = new Caches(context);
//创建网络加载对象
mLoadImage = new LoadImage(context);
}
public void attachToView(){
final Params tempParams = mParams;
//设置标记,防止错位
tempParams.mImageView.setTag(tempParams.mUri);
Runnable loadTask = new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void run() {
// Log.d("flag", "initParams: " +params);
Bitmap bitmap = beginLoadBitmap(tempParams.mUri);
Obj myObj = new Obj(bitmap,tempParams.mImageView,tempParams.mUri);
Message msg = mHandler.obtainMessage();
msg.obj = myObj;
msg.what = Constants.MESSAGE_ONE;
mHandler.sendMessage(msg);
}
};
//线程池执行
THREAD_POOL_EXECUTOR.execute(loadTask);
}
public SampleImageLoad initParams(){
mParams = new Params();
return this;
}
public SampleImageLoad setUrl(String url){
mParams.mUri = url;
return this;
}
public SampleImageLoad setImageSize(int width,int height){
mParams.mWidth = width;
mParams.mHeight = height;
return this;
}
public SampleImageLoad setImageView(ImageView imageView){
mParams.mImageView = imageView;
return this;
}
//属性参数
public class Params{
private String mUri;
private ImageView mImageView;
private int mWidth;
private int mHeight;
//使用线程池异步加载图片
// private boolean mAsyncAble = false;
}
//通过handler发送 msg.obj
public class Obj{
private Bitmap mBitmap;
private ImageView mImageView;
private String mTag;
public Obj(Bitmap bitmap,ImageView imageView,String tag){
mBitmap = bitmap;
mImageView = imageView;
mTag = tag;
}
}
/* //使用Builder模式 ----------失败
public static class Builder{
private Context mContext;
public Builder(Context context){
mContext = context;
mParams = new Params();
}
public void setUri(String uri){
mParams.mUri = uri;
}
public void setImageView(ImageView imageView){
mParams.mImageView = imageView;
}
public void setWidth(int width){
mParams.mWidth = width;
}
public void setHeight(int heigth){
mParams.mHeight = heigth;
}
*//* public void setAsyncAble(boolean asyncAble){
mParams.mAsyncAble = asyncAble;
}*//*
//创建SampleImageLoad对象
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public SampleImageLoad build(){
SampleImageLoad sampleImageLoad = new SampleImageLoad(mParams);
//创建图片缓存对象
mParams.mCache = new Caches(mContext);
//创建网络加载对象
mParams.mLoadImage = new LoadImage(mContext);
return sampleImageLoad;
}
}*/
}