├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ ├── Test.xml │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── ImageLoader.iml ├── README.md ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── softtanck │ │ └── imageloader │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── softtanck │ │ └── imageloader │ │ ├── MainActivity.java │ │ └── imageloader │ │ ├── ImageLoader.java │ │ ├── ImageLoaderCore.java │ │ ├── anim │ │ └── LoadAnimCore.java │ │ ├── bean │ │ ├── ImageSize.java │ │ └── ViewBeanHolder.java │ │ ├── disklrucahe │ │ ├── DiskLruCache.java │ │ ├── DiskLruCacheHelper.java │ │ ├── StrictLineReader.java │ │ └── Util.java │ │ ├── listener │ │ ├── AnimListener.java │ │ └── LoadListener.java │ │ └── utils │ │ ├── BaseCache.java │ │ ├── ImageSizeUtils.java │ │ ├── LruCacheUtils.java │ │ ├── MD5Code.java │ │ └── Utils.java │ └── res │ ├── drawable │ └── pictures_no.png │ ├── layout │ ├── item.xml │ └── main.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── test.gif /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | ImageLoader -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/Test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | 25 | 26 | Test 27 | 28 | 33 | 34 | 35 | 36 | 37 | 38 | 1.8 39 | 40 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ImageLoader.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ImageLoader 2 |
3 | 图片快速加载框架. 4 | 三级缓存策略. 5 | LruCache内存缓存算法. 6 | DiskLruCache磁盘缓存算法. 7 | 支持:1.本地图片.2.网络图片.3.加载淡入淡出动画.可以关闭开启. 8 | 自带框架动画.解决错位问题.
9 | ![image](https://github.com/q422013/ImageLoader/blob/master/test.gif) 10 | 11 | #How to use: 12 |
13 | type1:
14 | ImageLoader.getInstance(MainActivity.this, 3, ImageLoader.Type.LIFO).load(url, holder.imageView, new LoadListener() { 15 | @Override 16 | public void Loading(View view, String path) { 17 | 18 | } 19 | 20 | @Override 21 | public void LoadSuccess(View view, Bitmap bitmap, String path) { 22 | ((ImageView) view).setImageBitmap(bitmap); 23 | } 24 | 25 | @Override 26 | public void LoadError(View view, String path, String errorMsg) { 27 | 28 | } 29 | }); 30 | 31 |
32 | type2:
33 | ImageLoader.getInstance(MainActivity.this, 3, ImageLoader.Type.LIFO).load(url, holder.imageView); 34 | 35 | #Lincense 36 |
37 | Copyright 2015 Tanck 38 | 39 | Licensed under the Apache License, Version 2.0 (the "License"); 40 | you may not use this file except in compliance with the License. 41 | You may obtain a copy of the License at 42 | 43 | http://www.apache.org/licenses/LICENSE-2.0 44 | 45 | Unless required by applicable law or agreed to in writing, software 46 | distributed under the License is distributed on an "AS IS" BASIS, 47 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 48 | See the License for the specific language governing permissions and 49 | limitations under the License. 50 | 51 | 52 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 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 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "23.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.softtanck.imageloader" 9 | minSdkVersion 8 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:22.1.1' 25 | } 26 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\l\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/softtanck/imageloader/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader; 2 | 3 | import android.graphics.Bitmap; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.BaseAdapter; 10 | import android.widget.ImageView; 11 | import android.widget.ListView; 12 | 13 | import com.softtanck.imageloader.imageloader.ImageLoader; 14 | import com.softtanck.imageloader.imageloader.listener.LoadListener; 15 | 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | 20 | private ListView listView; 21 | 22 | private Myadapter myadapter; 23 | 24 | 25 | // 一堆图片链接 26 | public final String[] IMAGES = new String[]{ 27 | "http://e.hiphotos.baidu.com/image/pic/item/2fdda3cc7cd98d10b510fdea233fb80e7aec9021.jpg", 28 | "http://img0.imgtn.bdimg.com/it/u=2512767624,584707746&fm=21&gp=0.jpg", 29 | "http://img5.imgtn.bdimg.com/it/u=3876669459,1702100736&fm=21&gp=0.jpg", 30 | "http://img3.imgtn.bdimg.com/it/u=2071405091,342931090&fm=21&gp=0.jpg", 31 | "http://img1.imgtn.bdimg.com/it/u=3468695763,2338290854&fm=21&gp=0.jpg", 32 | "http://img3.imgtn.bdimg.com/it/u=3661837012,4085429241&fm=21&gp=0.jpg", 33 | "http://img3.imgtn.bdimg.com/it/u=1575536525,1104812014&fm=21&gp=0.jpg", 34 | "http://img2.imgtn.bdimg.com/it/u=394441947,1761554849&fm=21&gp=0.jpg", 35 | "http://img4.imgtn.bdimg.com/it/u=1818036152,2714012184&fm=21&gp=0.jpg", 36 | "http://img4.imgtn.bdimg.com/it/u=2266598397,645131245&fm=21&gp=0.jpg", 37 | "http://img4.imgtn.bdimg.com/it/u=440447379,308109475&fm=21&gp=0.jpg", 38 | "http://img2.imgtn.bdimg.com/it/u=768844916,3097393866&fm=21&gp=0.jpg", 39 | "http://img0.imgtn.bdimg.com/it/u=274839962,119107672&fm=21&gp=0.jpg", 40 | "http://img0.imgtn.bdimg.com/it/u=2327452287,2289737437&fm=21&gp=0.jpg", 41 | "http://img0.imgtn.bdimg.com/it/u=2452896114,2600307190&fm=21&gp=0.jpg", 42 | "http://img3.imgtn.bdimg.com/it/u=1671473477,1404439474&fm=21&gp=0.jpg", 43 | "http://img2.imgtn.bdimg.com/it/u=1868085000,3976235118&fm=21&gp=0.jpg", 44 | "http://img5.imgtn.bdimg.com/it/u=303566868,2154026315&fm=21&gp=0.jpg", 45 | "http://img0.imgtn.bdimg.com/it/u=698396519,2554150123&fm=21&gp=0.jpg", 46 | "http://img4.imgtn.bdimg.com/it/u=2266598397,645131245&fm=21&gp=0.jpg", 47 | }; 48 | 49 | @Override 50 | protected void onCreate(Bundle savedInstanceState) { 51 | super.onCreate(savedInstanceState); 52 | setContentView(R.layout.main); 53 | 54 | listView = (ListView) findViewById(R.id.lv); 55 | myadapter = new Myadapter(); 56 | listView.setAdapter(myadapter); 57 | 58 | } 59 | 60 | private class Myadapter extends BaseAdapter { 61 | 62 | @Override 63 | public int getCount() { 64 | return IMAGES.length; 65 | } 66 | 67 | @Override 68 | public Object getItem(int position) { 69 | return IMAGES[position]; 70 | } 71 | 72 | @Override 73 | public long getItemId(int position) { 74 | return position; 75 | } 76 | 77 | @Override 78 | public View getView(int position, View convertView, ViewGroup parent) { 79 | ViewHolder holder = null; 80 | if (null == convertView) { 81 | holder = new ViewHolder(); 82 | 83 | convertView = View.inflate(MainActivity.this, R.layout.item, null); 84 | holder.imageView = (ImageView) convertView.findViewById(R.id.main_iv); 85 | convertView.setTag(holder); 86 | } else { 87 | holder = (ViewHolder) convertView.getTag(); 88 | } 89 | holder.imageView.setImageResource(R.drawable.pictures_no); 90 | // ImageLoader.getInstance(MainActivity.this, 3, ImageLoader.Type.LIFO).load(IMAGES[position], holder.imageView); 91 | 92 | ImageLoader.getInstance(MainActivity.this, 3, ImageLoader.Type.LIFO).load(IMAGES[position], holder.imageView, new LoadListener() { 93 | @Override 94 | public void Loading(View view, String path) { 95 | 96 | } 97 | 98 | @Override 99 | public void LoadSuccess(View view, Bitmap bitmap, String path) { 100 | // if (view.getTag().toString().equals(path)) { 101 | ((ImageView) view).setImageBitmap(bitmap); 102 | // } 103 | } 104 | 105 | @Override 106 | public void LoadError(View view, String path, String errorMsg) { 107 | Log.d("Tanck","加载失败:"+path); 108 | ((ImageView)view).setImageResource(R.mipmap.ic_launcher); 109 | } 110 | }); 111 | return convertView; 112 | } 113 | 114 | private class ViewHolder { 115 | ImageView imageView; 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/ImageLoader.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader; 2 | 3 | 4 | import android.graphics.Bitmap.Config; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.content.Context; 8 | import android.graphics.Bitmap; 9 | import android.graphics.BitmapFactory; 10 | import android.os.Handler; 11 | import android.os.Looper; 12 | import android.os.Message; 13 | import android.util.DisplayMetrics; 14 | import android.view.ViewGroup.LayoutParams; 15 | import android.widget.ImageView; 16 | 17 | import com.softtanck.imageloader.imageloader.anim.LoadAnimCore; 18 | import com.softtanck.imageloader.imageloader.bean.ViewBeanHolder; 19 | import com.softtanck.imageloader.imageloader.bean.ImageSize; 20 | import com.softtanck.imageloader.imageloader.listener.LoadListener; 21 | import com.softtanck.imageloader.imageloader.disklrucahe.DiskLruCacheHelper; 22 | import com.softtanck.imageloader.imageloader.utils.LruCacheUtils; 23 | import com.softtanck.imageloader.imageloader.utils.MD5Code; 24 | import com.softtanck.imageloader.imageloader.utils.Utils; 25 | 26 | import java.io.File; 27 | import java.io.FileOutputStream; 28 | import java.io.IOException; 29 | import java.io.InputStream; 30 | import java.lang.reflect.Field; 31 | import java.net.HttpURLConnection; 32 | import java.net.MalformedURLException; 33 | import java.net.URL; 34 | import java.util.LinkedList; 35 | import java.util.concurrent.ExecutorService; 36 | import java.util.concurrent.Executors; 37 | import java.util.concurrent.Semaphore; 38 | 39 | /** 40 | * @author : Tanck 41 | * @Description : TODO 42 | * @date 10/19/2015 43 | */ 44 | public class ImageLoader { 45 | 46 | /** 47 | * 加载成功 48 | */ 49 | public static final int LOAD_SUCCESS = 0x111; 50 | 51 | /** 52 | * 加载失败 53 | */ 54 | public static final int LOAD_FAILE = 0x222; 55 | 56 | /** 57 | * 正在加载 58 | */ 59 | public static final int LOAD_ING = 0x333; 60 | 61 | /** 62 | * 关键地址,用来判断是本地还是网络 63 | */ 64 | public static final String DEF_KEY = "http"; 65 | 66 | /** 67 | * 默认低内存 68 | */ 69 | private static int defThreadCount = 1; 70 | 71 | /** 72 | * 队列的调度方式 73 | */ 74 | public static Type mType = Type.LIFO; 75 | 76 | /** 77 | * 工作的线程 78 | */ 79 | private Thread mThread; 80 | 81 | /** 82 | * 线程中的消息处理 83 | */ 84 | private Handler mPoolThreadHandler; 85 | 86 | /** 87 | * 线程池 88 | */ 89 | private ExecutorService mThreadPool; 90 | 91 | /** 92 | * 任务队列 93 | */ 94 | private LinkedList mTask; 95 | 96 | /** 97 | * 引入一个值为1的信号量,防止mPoolThreadHander未初始化完成 98 | */ 99 | private Semaphore mSemapHore = new Semaphore(0); 100 | 101 | /** 102 | * 引入一个值为threadCount的信号量,由于线程池内部也有一个阻塞线程,防止加入任务的速度过快,使LIFO效果不明显 103 | */ 104 | private Semaphore mPoolSemaphore; 105 | 106 | /** 107 | * 图片展示处理 108 | */ 109 | private Handler mDisPlayHandler; 110 | 111 | /** 112 | * 设置存储路径 113 | */ 114 | private String saveLocation; 115 | 116 | /** 117 | * 加载监听 118 | */ 119 | private LoadListener loadListener; 120 | 121 | /** 122 | * 加载动画核心类 123 | */ 124 | private LoadAnimCore loadAnimCore; 125 | 126 | /** 127 | * 默认加载动画 128 | */ 129 | private boolean isNeedAnim = true; 130 | 131 | /** 132 | * 默认加载Config.Rgb_565 133 | */ 134 | private Config loadConfig = Config.RGB_565; 135 | 136 | /** 137 | * 磁盘存储帮助类 138 | */ 139 | private DiskLruCacheHelper helper; 140 | 141 | /** 142 | * 缓存目录名字 143 | */ 144 | private String caCheName = "picCaChe"; 145 | 146 | /** 147 | * 连接超时时间 148 | */ 149 | private int mTimeOut = 1000; 150 | 151 | private static ImageLoader loader; 152 | 153 | /** 154 | * 队列调度模式 155 | */ 156 | public enum Type { 157 | FIFO, LIFO 158 | } 159 | 160 | /** 161 | * 设置网络请求超时 162 | * 163 | * @param mTimeOut 164 | */ 165 | public void setTimeOut(int mTimeOut) { 166 | this.mTimeOut = mTimeOut; 167 | } 168 | 169 | /** 170 | * 设置加载方式RAGB_8888适合大图.RGB_565适合小图 171 | * 172 | * @param loadConfig 173 | */ 174 | public void setLoadConfig(Config loadConfig) { 175 | this.loadConfig = loadConfig; 176 | } 177 | 178 | /** 179 | * 设置是否需要动画. 180 | * 181 | * @param isneed 182 | */ 183 | public void setNeedAnim(boolean isneed) { 184 | this.isNeedAnim = isneed; 185 | } 186 | 187 | /** 188 | * 设置加载监听 189 | * 190 | * @param loadListener 191 | */ 192 | public void setLoadListener(LoadListener loadListener) { 193 | this.loadListener = loadListener; 194 | } 195 | 196 | private ImageLoader() { 197 | 198 | } 199 | 200 | private ImageLoader(Context context, int threadCount, Type type) { 201 | init(context, threadCount, type); 202 | } 203 | 204 | private void init(Context context, int threadCount, Type type) { 205 | //工作线程 206 | mThread = new Thread() { 207 | @Override 208 | public void run() { 209 | Looper.prepare(); 210 | mPoolThreadHandler = new Handler() { 211 | @Override 212 | public void handleMessage(Message msg) { 213 | mThreadPool.execute(getTask()); 214 | try { 215 | mPoolSemaphore.acquire();//信号量 + 1 216 | } catch (InterruptedException e) { 217 | e.printStackTrace(); 218 | } 219 | } 220 | }; 221 | mSemapHore.release();//初始化完成后信号量 -1 222 | Looper.loop(); 223 | } 224 | }; 225 | mThread.start(); 226 | //初始化线程池 227 | mThreadPool = Executors.newScheduledThreadPool(threadCount); 228 | //初始化信号量 229 | mPoolSemaphore = new Semaphore(threadCount); 230 | //初始化线程列表 231 | mTask = new LinkedList<>(); 232 | //初始化调度队列类型 233 | mType = type == null ? Type.LIFO : type; 234 | //设置默认缓存路径 235 | if (null != context) { 236 | saveLocation = context.getExternalCacheDir().getAbsolutePath(); 237 | //初始化磁盘缓存 238 | try { 239 | helper = new DiskLruCacheHelper(context, caCheName); 240 | } catch (IOException e) { 241 | e.printStackTrace(); 242 | } 243 | } 244 | } 245 | 246 | public static ImageLoader getInstance(Context context) { 247 | if (null == loader) { 248 | synchronized (ImageLoader.class) { 249 | if (null == loader) { 250 | loader = new ImageLoader(context, defThreadCount, mType); 251 | } 252 | } 253 | } 254 | return loader; 255 | } 256 | 257 | public static ImageLoader getInstance(Context context, int threadCount, Type type) { 258 | if (null == loader) { 259 | synchronized (ImageLoader.class) { 260 | if (null == loader) { 261 | loader = new ImageLoader(context, threadCount, type); 262 | } 263 | } 264 | } 265 | return loader; 266 | } 267 | 268 | /** 269 | * 加载图片 270 | * 271 | * @param path 272 | * @param view 273 | */ 274 | public void load(final String path, final View view) { 275 | 276 | if (null == path) 277 | throw new RuntimeException("this path is null"); 278 | 279 | view.setTag(path); 280 | //1.从磁盘,2.从内存 281 | if (null == mDisPlayHandler) 282 | mDisPlayHandler = new Handler() { 283 | @Override 284 | public void handleMessage(Message msg) { 285 | ViewBeanHolder holder = (ViewBeanHolder) msg.obj; 286 | final View view = holder.view; 287 | Bitmap bm = holder.bitmap; 288 | String path = holder.path; 289 | if (view.getTag().toString().equals(path) && null != bm) { 290 | ((ImageView) view).setImageBitmap(bm); 291 | if (isNeedAnim) 292 | new LoadAnimCore(view); 293 | } 294 | } 295 | }; 296 | 297 | addTask(path, view); 298 | 299 | } 300 | 301 | 302 | /** 303 | * 加载图片 304 | * 305 | * @param path 306 | * @param view 307 | */ 308 | public void load(final String path, final View view, final LoadListener loadListener) { 309 | 310 | if (null == path) 311 | throw new RuntimeException("this path is null"); 312 | 313 | if (null == loadListener) 314 | throw new RuntimeException("this loadListener is null"); 315 | 316 | view.setTag(path); 317 | //1.从磁盘,2.从内存 318 | if (null == mDisPlayHandler) 319 | mDisPlayHandler = new Handler() { 320 | @Override 321 | public void handleMessage(Message msg) { 322 | int code = msg.what; 323 | ViewBeanHolder holder = (ViewBeanHolder) msg.obj; 324 | final View view = holder.view; 325 | Bitmap bm = holder.bitmap; 326 | String path = holder.path; 327 | switch (code) { 328 | case LOAD_SUCCESS://加载成功 329 | if (view.getTag().toString().equals(path)) { 330 | loadListener.LoadSuccess(view, bm, path); 331 | if (isNeedAnim) 332 | new LoadAnimCore(view); 333 | } 334 | break; 335 | case LOAD_ING://加载中 336 | if (view.getTag().toString().equals(path)) { 337 | loadListener.Loading(view, path); 338 | } 339 | break; 340 | case LOAD_FAILE://加载失败 341 | if (view.getTag().toString().equals(path)) { 342 | loadListener.LoadError(view, path, null);//暂时消息为空 343 | } 344 | break; 345 | } 346 | } 347 | }; 348 | 349 | addTask(path, view); 350 | 351 | } 352 | 353 | /** 354 | * 添加任务 355 | * 356 | * @param path 357 | * @param view 358 | */ 359 | private synchronized void addTask(final String path, final View view) { 360 | 361 | Runnable runnable = new Runnable() { 362 | @Override 363 | public void run() { 364 | ViewBeanHolder holder = new ViewBeanHolder(); 365 | holder.view = view; 366 | holder.path = path; 367 | sendMsg(LOAD_ING, holder); 368 | //TODO 从内存中获取 369 | Bitmap bitmap = LruCacheUtils.getInstance().get(path); 370 | if (null == bitmap) { 371 | //TODO 从磁盘中获取 372 | if (urlIsNetWork(path)) { 373 | //网络图片缓存 374 | bitmap = decodeSampledBitmapFromDisk(path, (ImageView) view); 375 | } else { 376 | //本地图片缓存 377 | bitmap = decodeSampledBitmapFromResource(path, (ImageView) view); 378 | } 379 | // TODO 从网络中获取 380 | if (null == bitmap) { 381 | bitmap = decodeSampledBitmapFromNetWork(path, (ImageView) view); 382 | } 383 | } 384 | //加载成功 385 | if (null != bitmap) { 386 | LruCacheUtils.getInstance().put(path, bitmap); 387 | holder.bitmap = bitmap;//唯一的 388 | sendMsg(LOAD_SUCCESS, holder); 389 | } else { 390 | //加载失败 391 | sendMsg(LOAD_FAILE, holder); 392 | } 393 | } 394 | }; 395 | 396 | if (null == mPoolThreadHandler) { 397 | try { 398 | mSemapHore.acquire(); 399 | } catch (InterruptedException e) { 400 | e.printStackTrace(); 401 | } 402 | } 403 | mTask.add(runnable); 404 | mPoolThreadHandler.sendEmptyMessage(0x1000); 405 | mPoolSemaphore.release();//信号量 -1 406 | } 407 | 408 | /** 409 | * 发送消息 410 | * 411 | * @param code 412 | */ 413 | private void sendMsg(int code, ViewBeanHolder holder) { 414 | Message message = Message.obtain(); 415 | message.obj = holder; 416 | message.what = code; 417 | mDisPlayHandler.sendMessage(message); 418 | } 419 | 420 | /** 421 | * 判断地址是本地还是网络 422 | * 423 | * @param path 424 | */ 425 | private boolean urlIsNetWork(String path) { 426 | if (path.toLowerCase().contains(DEF_KEY)) {//判断文件获取方式 427 | return true; 428 | } 429 | return false; 430 | } 431 | 432 | 433 | /** 434 | * 根据ImageView获得适当的压缩的宽和高 435 | * 436 | * @param imageView 437 | * @return 438 | */ 439 | private ImageSize getImageViewWidth(ImageView imageView) { 440 | ImageSize imageSize = new ImageSize(); 441 | final DisplayMetrics displayMetrics = imageView.getContext() 442 | .getResources().getDisplayMetrics(); 443 | final LayoutParams params = imageView.getLayoutParams(); 444 | 445 | int width = params.width == LayoutParams.WRAP_CONTENT ? 0 : imageView 446 | .getWidth(); // Get actual image width 447 | if (width <= 0) 448 | width = params.width; // Get layout width parameter 449 | if (width <= 0) 450 | width = getImageViewFieldValue(imageView, "mMaxWidth"); // Check 451 | // maxWidth 452 | // parameter 453 | if (width <= 0) 454 | width = displayMetrics.widthPixels; 455 | int height = params.height == LayoutParams.WRAP_CONTENT ? 0 : imageView 456 | .getHeight(); // Get actual image height 457 | if (height <= 0) 458 | height = params.height; // Get layout height parameter 459 | if (height <= 0) 460 | height = getImageViewFieldValue(imageView, "mMaxHeight"); // Check 461 | // maxHeight 462 | // parameter 463 | if (height <= 0) 464 | height = displayMetrics.heightPixels; 465 | imageSize.width = width; 466 | imageSize.height = height; 467 | return imageSize; 468 | 469 | } 470 | 471 | 472 | /** 473 | * 反射获得ImageView设置的最大宽度和高度 474 | * 475 | * @param object 476 | * @param fieldName 477 | * @return 478 | */ 479 | private static int getImageViewFieldValue(Object object, String fieldName) { 480 | int value = 0; 481 | try { 482 | Field field = ImageView.class.getDeclaredField(fieldName); 483 | field.setAccessible(true); 484 | int fieldValue = (Integer) field.get(object); 485 | if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) { 486 | value = fieldValue; 487 | } 488 | } catch (Exception e) { 489 | } 490 | return value; 491 | } 492 | 493 | 494 | /** 495 | * 根据计算的inSampleSize,得到压缩后图片 496 | * 497 | * @param pathName 498 | * @param imageview 499 | * @return 500 | */ 501 | private Bitmap decodeSampledBitmapFromResource(String pathName, ImageView imageview) { 502 | ImageSize imageSize = getImageViewWidth(imageview); 503 | int reqWidth = imageSize.width; 504 | int reqHeight = imageSize.height; 505 | // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小 506 | final BitmapFactory.Options options = new BitmapFactory.Options(); 507 | options.inPreferredConfig = loadConfig; 508 | options.inJustDecodeBounds = true; 509 | BitmapFactory.decodeFile(pathName, options); 510 | // 调用上面定义的方法计算inSampleSize值 511 | options.inSampleSize = calculateInSampleSize(options, reqWidth, 512 | reqHeight); 513 | // 使用获取到的inSampleSize值再次解析图片 514 | options.inJustDecodeBounds = false; 515 | Bitmap bitmap = BitmapFactory.decodeFile(pathName, options); 516 | return bitmap; 517 | } 518 | 519 | 520 | /** 521 | * 根据计算的inSampleSize,得到压缩后图片 522 | * 523 | * @param pathNmae 524 | * @param view 525 | * @return 526 | */ 527 | private Bitmap decodeSampledBitmapFromDisk(String pathNmae, ImageView view) { 528 | // TODO 因为DiskLruCaChe已经判断了文件是否存在,所以不再需要判断文件了. 529 | byte[] data = helper.getAsBytes(pathNmae); 530 | if (null == data) 531 | return null; 532 | ImageSize imageSize = getImageViewWidth(view); 533 | int reqWidth = imageSize.width; 534 | int reqHeight = imageSize.height; 535 | final BitmapFactory.Options options = new BitmapFactory.Options(); 536 | options.inPreferredConfig = loadConfig; 537 | options.inJustDecodeBounds = true; 538 | BitmapFactory.decodeByteArray(data, 0, data.length, options); 539 | options.inSampleSize = calculateInSampleSize(options, reqWidth, 540 | reqHeight); 541 | options.inJustDecodeBounds = false; 542 | Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); 543 | return bitmap; 544 | } 545 | 546 | 547 | /** 548 | * 根据计算的inSampleSize,得到压缩后图片 549 | * 550 | * @param url 551 | * @param imageView 552 | * @return 553 | */ 554 | private Bitmap decodeSampledBitmapFromNetWork(String url, ImageView imageView) { 555 | Bitmap bitmap = null; 556 | String path = getNetWork2Save(url); 557 | if (null != path) { 558 | bitmap = decodeSampledBitmapFromDisk(path, imageView); 559 | } 560 | return bitmap; 561 | } 562 | 563 | 564 | /** 565 | * 获取网络图片 566 | * 567 | * @param urlString 568 | * @return 569 | */ 570 | private String getNetWork2Save(String urlString) { 571 | URL imgUrl = null; 572 | Bitmap bitmap = null; 573 | try { 574 | imgUrl = new URL(urlString); 575 | // 使用HttpURLConnection打开连接 576 | HttpURLConnection urlConn = (HttpURLConnection) imgUrl.openConnection(); 577 | urlConn.setConnectTimeout(mTimeOut);//10秒 578 | urlConn.connect(); 579 | if (200 == urlConn.getResponseCode()) { 580 | // 将得到的数据转化成InputStream 581 | InputStream is = urlConn.getInputStream(); 582 | bitmap = BitmapFactory.decodeStream(is); 583 | helper.put(urlString, Utils.bitmap2Bytes(bitmap)); 584 | is.close(); 585 | if (!bitmap.isRecycled()) 586 | bitmap.recycle(); 587 | return urlString; 588 | } 589 | } catch (MalformedURLException e) { 590 | e.printStackTrace(); 591 | return null; 592 | } catch (IOException e) { 593 | e.printStackTrace(); 594 | return null; 595 | } 596 | return null; 597 | } 598 | 599 | 600 | /** 601 | * 计算inSampleSize,用于压缩图片 602 | * 603 | * @param options 604 | * @param reqWidth 605 | * @param reqHeight 606 | * @return 607 | */ 608 | private int calculateInSampleSize(BitmapFactory.Options options, 609 | int reqWidth, int reqHeight) { 610 | // 源图片的宽度 611 | int width = options.outWidth; 612 | int height = options.outHeight; 613 | int inSampleSize = 1; 614 | 615 | if (width > reqWidth && height > reqHeight) { 616 | // 计算出实际宽度和目标宽度的比率 617 | int widthRatio = Math.round((float) width / (float) reqWidth); 618 | int heightRatio = Math.round((float) width / (float) reqWidth); 619 | inSampleSize = Math.max(widthRatio, heightRatio); 620 | } 621 | return inSampleSize; 622 | } 623 | 624 | 625 | /** 626 | * 获取任务 627 | * 628 | * @return 629 | */ 630 | private synchronized Runnable getTask() { 631 | if (0 < mTask.size()) { 632 | if (mType == Type.LIFO) 633 | return mTask.removeFirst(); 634 | else 635 | return mTask.removeLast(); 636 | } 637 | return null; 638 | } 639 | 640 | /** 641 | * 设置存储路径 642 | * 643 | * @param location 644 | */ 645 | public void setCaCheName(String location) { 646 | this.caCheName = location; 647 | } 648 | } 649 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/ImageLoaderCore.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader; 2 | 3 | /** 4 | * @author : Tanck 5 | * @Description : TODO 加载核心类 6 | * @date 10/20/2015 7 | */ 8 | public class ImageLoaderCore { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/anim/LoadAnimCore.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.anim; 2 | 3 | import android.view.View; 4 | 5 | import com.nineoldandroids.animation.ValueAnimator; 6 | import com.nineoldandroids.view.ViewHelper; 7 | import com.softtanck.imageloader.imageloader.listener.AnimListener; 8 | 9 | /** 10 | * @author : Tanck 11 | * @Description : TODO 加载动画核心 12 | * @date 10/20/2015 13 | */ 14 | public class LoadAnimCore implements ValueAnimator.AnimatorUpdateListener { 15 | 16 | protected ValueAnimator valueAnimator; 17 | 18 | 19 | private View view; 20 | 21 | 22 | public LoadAnimCore(View view) { 23 | this.view = view; 24 | initCore(); 25 | } 26 | 27 | /** 28 | * 初始化设置 29 | */ 30 | private void initCore() { 31 | valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f); 32 | valueAnimator.addUpdateListener(this); 33 | valueAnimator.setDuration(500); 34 | valueAnimator.start(); 35 | } 36 | 37 | @Override 38 | public void onAnimationUpdate(ValueAnimator animation) { 39 | float currentValue = (float) animation.getAnimatedValue(); 40 | ViewHelper.setAlpha(view, currentValue); 41 | // currentValue = 2 * currentValue; 42 | // currentValue = currentValue > 1.0f ? 1.0f : currentValue; 43 | // ViewHelper.setScaleX(view, currentValue); 44 | // ViewHelper.setScaleY(view, currentValue); 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/bean/ImageSize.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.bean; 2 | 3 | /** 4 | * @author : Tanck 5 | * @Description : TODO 6 | * @date 10/19/2015 7 | */ 8 | public class ImageSize { 9 | 10 | public int width; 11 | public int height; 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/bean/ViewBeanHolder.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.bean; 2 | 3 | import android.graphics.Bitmap; 4 | import android.view.View; 5 | 6 | /** 7 | * @author : Tanck 8 | * @Description : TODO 9 | * @date 10/19/2015 10 | */ 11 | public class ViewBeanHolder { 12 | 13 | public String path; 14 | 15 | public Bitmap bitmap; 16 | 17 | public View view; 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/disklrucahe/DiskLruCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.softtanck.imageloader.imageloader.disklrucahe; 18 | 19 | 20 | import java.io.BufferedWriter; 21 | import java.io.Closeable; 22 | import java.io.EOFException; 23 | import java.io.File; 24 | import java.io.FileInputStream; 25 | import java.io.FileNotFoundException; 26 | import java.io.FileOutputStream; 27 | import java.io.FilterOutputStream; 28 | import java.io.IOException; 29 | import java.io.InputStream; 30 | import java.io.InputStreamReader; 31 | import java.io.OutputStream; 32 | import java.io.OutputStreamWriter; 33 | import java.io.Writer; 34 | import java.util.ArrayList; 35 | import java.util.Iterator; 36 | import java.util.LinkedHashMap; 37 | import java.util.Map; 38 | import java.util.concurrent.Callable; 39 | import java.util.concurrent.LinkedBlockingQueue; 40 | import java.util.concurrent.ThreadPoolExecutor; 41 | import java.util.concurrent.TimeUnit; 42 | import java.util.regex.Matcher; 43 | import java.util.regex.Pattern; 44 | 45 | /** 46 | * A cache that uses a bounded amount of space on a filesystem. Each cache 47 | * entry has a string key and a fixed number of values. Each key must match 48 | * the regex [a-z0-9_-]{1,120}. Values are byte sequences, 49 | * accessible as streams or files. Each value must be between {@code 0} and 50 | * {@code Integer.MAX_VALUE} bytes in length. 51 | *

52 | *

The cache stores its data in a directory on the filesystem. This 53 | * directory must be exclusive to the cache; the cache may delete or overwrite 54 | * files from its directory. It is an error for multiple processes to use the 55 | * same cache directory at the same time. 56 | *

57 | *

This cache limits the number of bytes that it will store on the 58 | * filesystem. When the number of stored bytes exceeds the limit, the cache will 59 | * remove entries in the background until the limit is satisfied. The limit is 60 | * not strict: the cache may temporarily exceed it while waiting for files to be 61 | * deleted. The limit does not include filesystem overhead or the cache 62 | * journal so space-sensitive applications should set a conservative limit. 63 | *

64 | *

Clients call {@link #edit} to create or update the values of an entry. An 65 | * entry may have only one editor at one time; if a value is not available to be 66 | * edited then {@link #edit} will return null. 67 | *

    68 | *
  • When an entry is being created it is necessary to 69 | * supply a full set of values; the empty value should be used as a 70 | * placeholder if necessary. 71 | *
  • When an entry is being edited, it is not necessary 72 | * to supply data for every value; values default to their previous 73 | * value. 74 | *
75 | * Every {@link #edit} call must be matched by a call to {@link Editor#commit} 76 | * or {@link Editor#abort}. Committing is atomic: a read observes the full set 77 | * of values as they were before or after the commit, but never a mix of values. 78 | *

79 | *

Clients call {@link #get} to read a snapshot of an entry. The read will 80 | * observe the value at the time that {@link #get} was called. Updates and 81 | * removals after the call do not impact ongoing reads. 82 | *

83 | *

This class is tolerant of some I/O errors. If files are missing from the 84 | * filesystem, the corresponding entries will be dropped from the cache. If 85 | * an error occurs while writing a cache value, the edit will fail silently. 86 | * Callers should handle other problems by catching {@code IOException} and 87 | * responding appropriately. 88 | */ 89 | public final class DiskLruCache implements Closeable { 90 | static final String JOURNAL_FILE = "journal"; 91 | static final String JOURNAL_FILE_TEMP = "journal.tmp"; 92 | static final String JOURNAL_FILE_BACKUP = "journal.bkp"; 93 | static final String MAGIC = "libcore.io.DiskLruCache"; 94 | static final String VERSION_1 = "1"; 95 | static final long ANY_SEQUENCE_NUMBER = -1; 96 | static final String STRING_KEY_PATTERN = "[a-z0-9_-]{1,120}"; 97 | static final Pattern LEGAL_KEY_PATTERN = Pattern.compile(STRING_KEY_PATTERN); 98 | private static final String CLEAN = "CLEAN"; 99 | private static final String DIRTY = "DIRTY"; 100 | private static final String REMOVE = "REMOVE"; 101 | private static final String READ = "READ"; 102 | 103 | /* 104 | * This cache uses a journal file named "journal". A typical journal file 105 | * looks like this: 106 | * libcore.io.DiskLruCache 107 | * 1 108 | * 100 109 | * 2 110 | * 111 | * CLEAN 3400330d1dfc7f3f7f4b8d4d803dfcf6 832 21054 112 | * DIRTY 335c4c6028171cfddfbaae1a9c313c52 113 | * CLEAN 335c4c6028171cfddfbaae1a9c313c52 3934 2342 114 | * REMOVE 335c4c6028171cfddfbaae1a9c313c52 115 | * DIRTY 1ab96a171faeeee38496d8b330771a7a 116 | * CLEAN 1ab96a171faeeee38496d8b330771a7a 1600 234 117 | * READ 335c4c6028171cfddfbaae1a9c313c52 118 | * READ 3400330d1dfc7f3f7f4b8d4d803dfcf6 119 | * 120 | * The first five lines of the journal form its header. They are the 121 | * constant string "libcore.io.DiskLruCache", the disk cache's version, 122 | * the application's version, the value count, and a blank line. 123 | * 124 | * Each of the subsequent lines in the file is a record of the state of a 125 | * cache entry. Each line contains space-separated values: a state, a key, 126 | * and optional state-specific values. 127 | * o DIRTY lines track that an entry is actively being created or updated. 128 | * Every successful DIRTY action should be followed by a CLEAN or REMOVE 129 | * action. DIRTY lines without a matching CLEAN or REMOVE indicate that 130 | * temporary files may need to be deleted. 131 | * o CLEAN lines track a cache entry that has been successfully published 132 | * and may be read. A publish line is followed by the lengths of each of 133 | * its values. 134 | * o READ lines track accesses for LRU. 135 | * o REMOVE lines track entries that have been deleted. 136 | * 137 | * The journal file is appended to as cache operations occur. The journal may 138 | * occasionally be compacted by dropping redundant lines. A temporary file named 139 | * "journal.tmp" will be used during compaction; that file should be deleted if 140 | * it exists when the cache is opened. 141 | */ 142 | 143 | private final File directory; 144 | private final File journalFile; 145 | private final File journalFileTmp; 146 | private final File journalFileBackup; 147 | private final int appVersion; 148 | private long maxSize; 149 | private final int valueCount; 150 | private long size = 0; 151 | private Writer journalWriter; 152 | private final LinkedHashMap lruEntries = 153 | new LinkedHashMap(0, 0.75f, true); 154 | private int redundantOpCount; 155 | 156 | /** 157 | * To differentiate between old and current snapshots, each entry is given 158 | * a sequence number each time an edit is committed. A snapshot is stale if 159 | * its sequence number is not equal to its entry's sequence number. 160 | */ 161 | private long nextSequenceNumber = 0; 162 | 163 | /** 164 | * This cache uses a single background thread to evict entries. 165 | */ 166 | final ThreadPoolExecutor executorService = 167 | new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue()); 168 | private final Callable cleanupCallable = new Callable() { 169 | public Void call() throws Exception { 170 | synchronized (DiskLruCache.this) { 171 | if (journalWriter == null) { 172 | return null; // Closed. 173 | } 174 | trimToSize(); 175 | if (journalRebuildRequired()) { 176 | rebuildJournal(); 177 | redundantOpCount = 0; 178 | } 179 | } 180 | return null; 181 | } 182 | }; 183 | 184 | private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) { 185 | this.directory = directory; 186 | this.appVersion = appVersion; 187 | this.journalFile = new File(directory, JOURNAL_FILE); 188 | this.journalFileTmp = new File(directory, JOURNAL_FILE_TEMP); 189 | this.journalFileBackup = new File(directory, JOURNAL_FILE_BACKUP); 190 | this.valueCount = valueCount; 191 | this.maxSize = maxSize; 192 | } 193 | 194 | /** 195 | * Opens the cache in {@code directory}, creating a cache if none exists 196 | * there. 197 | * 198 | * @param directory a writable directory 199 | * @param valueCount the number of values per cache entry. Must be positive. 200 | * @param maxSize the maximum number of bytes this cache should use to store 201 | * @throws IOException if reading or writing the cache directory fails 202 | */ 203 | public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize) 204 | throws IOException { 205 | if (maxSize <= 0) { 206 | throw new IllegalArgumentException("maxSize <= 0"); 207 | } 208 | if (valueCount <= 0) { 209 | throw new IllegalArgumentException("valueCount <= 0"); 210 | } 211 | 212 | // If a bkp file exists, use it instead. 213 | File backupFile = new File(directory, JOURNAL_FILE_BACKUP); 214 | if (backupFile.exists()) { 215 | File journalFile = new File(directory, JOURNAL_FILE); 216 | // If journal file also exists just delete backup file. 217 | if (journalFile.exists()) { 218 | backupFile.delete(); 219 | } else { 220 | renameTo(backupFile, journalFile, false); 221 | } 222 | } 223 | 224 | // Prefer to pick up where we left off. 225 | DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); 226 | if (cache.journalFile.exists()) { 227 | try { 228 | cache.readJournal(); 229 | cache.processJournal(); 230 | return cache; 231 | } catch (IOException journalIsCorrupt) { 232 | System.out 233 | .println("DiskLruCache " 234 | + directory 235 | + " is corrupt: " 236 | + journalIsCorrupt.getMessage() 237 | + ", removing"); 238 | cache.delete(); 239 | } 240 | } 241 | 242 | // Create a new empty cache. 243 | directory.mkdirs(); 244 | cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); 245 | cache.rebuildJournal(); 246 | return cache; 247 | } 248 | 249 | private void readJournal() throws IOException { 250 | StrictLineReader reader = new StrictLineReader(new FileInputStream(journalFile), Util.US_ASCII); 251 | try { 252 | String magic = reader.readLine(); 253 | String version = reader.readLine(); 254 | String appVersionString = reader.readLine(); 255 | String valueCountString = reader.readLine(); 256 | String blank = reader.readLine(); 257 | if (!MAGIC.equals(magic) 258 | || !VERSION_1.equals(version) 259 | || !Integer.toString(appVersion).equals(appVersionString) 260 | || !Integer.toString(valueCount).equals(valueCountString) 261 | || !"".equals(blank)) { 262 | throw new IOException("unexpected journal header: [" + magic + ", " + version + ", " 263 | + valueCountString + ", " + blank + "]"); 264 | } 265 | 266 | int lineCount = 0; 267 | while (true) { 268 | try { 269 | readJournalLine(reader.readLine()); 270 | lineCount++; 271 | } catch (EOFException endOfJournal) { 272 | break; 273 | } 274 | } 275 | redundantOpCount = lineCount - lruEntries.size(); 276 | 277 | // If we ended on a truncated line, rebuild the journal before appending to it. 278 | if (reader.hasUnterminatedLine()) { 279 | rebuildJournal(); 280 | } else { 281 | journalWriter = new BufferedWriter(new OutputStreamWriter( 282 | new FileOutputStream(journalFile, true), Util.US_ASCII)); 283 | } 284 | } finally { 285 | Util.closeQuietly(reader); 286 | } 287 | } 288 | 289 | private void readJournalLine(String line) throws IOException { 290 | int firstSpace = line.indexOf(' '); 291 | if (firstSpace == -1) { 292 | throw new IOException("unexpected journal line: " + line); 293 | } 294 | 295 | int keyBegin = firstSpace + 1; 296 | int secondSpace = line.indexOf(' ', keyBegin); 297 | final String key; 298 | if (secondSpace == -1) { 299 | key = line.substring(keyBegin); 300 | if (firstSpace == REMOVE.length() && line.startsWith(REMOVE)) { 301 | lruEntries.remove(key); 302 | return; 303 | } 304 | } else { 305 | key = line.substring(keyBegin, secondSpace); 306 | } 307 | 308 | Entry entry = lruEntries.get(key); 309 | if (entry == null) { 310 | entry = new Entry(key); 311 | lruEntries.put(key, entry); 312 | } 313 | 314 | if (secondSpace != -1 && firstSpace == CLEAN.length() && line.startsWith(CLEAN)) { 315 | String[] parts = line.substring(secondSpace + 1).split(" "); 316 | entry.readable = true; 317 | entry.currentEditor = null; 318 | entry.setLengths(parts); 319 | } else if (secondSpace == -1 && firstSpace == DIRTY.length() && line.startsWith(DIRTY)) { 320 | entry.currentEditor = new Editor(entry); 321 | } else if (secondSpace == -1 && firstSpace == READ.length() && line.startsWith(READ)) { 322 | // This work was already done by calling lruEntries.get(). 323 | } else { 324 | throw new IOException("unexpected journal line: " + line); 325 | } 326 | } 327 | 328 | /** 329 | * Computes the initial size and collects garbage as a part of opening the 330 | * cache. Dirty entries are assumed to be inconsistent and will be deleted. 331 | */ 332 | private void processJournal() throws IOException { 333 | deleteIfExists(journalFileTmp); 334 | for (Iterator i = lruEntries.values().iterator(); i.hasNext(); ) { 335 | Entry entry = i.next(); 336 | if (entry.currentEditor == null) { 337 | for (int t = 0; t < valueCount; t++) { 338 | size += entry.lengths[t]; 339 | } 340 | } else { 341 | entry.currentEditor = null; 342 | for (int t = 0; t < valueCount; t++) { 343 | deleteIfExists(entry.getCleanFile(t)); 344 | deleteIfExists(entry.getDirtyFile(t)); 345 | } 346 | i.remove(); 347 | } 348 | } 349 | } 350 | 351 | /** 352 | * Creates a new journal that omits redundant information. This replaces the 353 | * current journal if it exists. 354 | */ 355 | private synchronized void rebuildJournal() throws IOException { 356 | if (journalWriter != null) { 357 | journalWriter.close(); 358 | } 359 | 360 | Writer writer = new BufferedWriter( 361 | new OutputStreamWriter(new FileOutputStream(journalFileTmp), Util.US_ASCII)); 362 | try { 363 | writer.write(MAGIC); 364 | writer.write("\n"); 365 | writer.write(VERSION_1); 366 | writer.write("\n"); 367 | writer.write(Integer.toString(appVersion)); 368 | writer.write("\n"); 369 | writer.write(Integer.toString(valueCount)); 370 | writer.write("\n"); 371 | writer.write("\n"); 372 | 373 | for (Entry entry : lruEntries.values()) { 374 | if (entry.currentEditor != null) { 375 | writer.write(DIRTY + ' ' + entry.key + '\n'); 376 | } else { 377 | writer.write(CLEAN + ' ' + entry.key + entry.getLengths() + '\n'); 378 | } 379 | } 380 | } finally { 381 | writer.close(); 382 | } 383 | 384 | if (journalFile.exists()) { 385 | renameTo(journalFile, journalFileBackup, true); 386 | } 387 | renameTo(journalFileTmp, journalFile, false); 388 | journalFileBackup.delete(); 389 | 390 | journalWriter = new BufferedWriter( 391 | new OutputStreamWriter(new FileOutputStream(journalFile, true), Util.US_ASCII)); 392 | } 393 | 394 | private static void deleteIfExists(File file) throws IOException { 395 | if (file.exists() && !file.delete()) { 396 | throw new IOException(); 397 | } 398 | } 399 | 400 | private static void renameTo(File from, File to, boolean deleteDestination) throws IOException { 401 | if (deleteDestination) { 402 | deleteIfExists(to); 403 | } 404 | if (!from.renameTo(to)) { 405 | throw new IOException(); 406 | } 407 | } 408 | 409 | /** 410 | * Returns a snapshot of the entry named {@code key}, or null if it doesn't 411 | * exist is not currently readable. If a value is returned, it is moved to 412 | * the head of the LRU queue. 413 | */ 414 | public synchronized Snapshot get(String key) throws IOException { 415 | checkNotClosed(); 416 | validateKey(key); 417 | Entry entry = lruEntries.get(key); 418 | if (entry == null) { 419 | return null; 420 | } 421 | 422 | if (!entry.readable) { 423 | return null; 424 | } 425 | 426 | // Open all streams eagerly to guarantee that we see a single published 427 | // snapshot. If we opened streams lazily then the streams could come 428 | // from different edits. 429 | InputStream[] ins = new InputStream[valueCount]; 430 | try { 431 | for (int i = 0; i < valueCount; i++) { 432 | ins[i] = new FileInputStream(entry.getCleanFile(i)); 433 | } 434 | } catch (FileNotFoundException e) { 435 | // A file must have been deleted manually! 436 | for (int i = 0; i < valueCount; i++) { 437 | if (ins[i] != null) { 438 | Util.closeQuietly(ins[i]); 439 | } else { 440 | break; 441 | } 442 | } 443 | return null; 444 | } 445 | 446 | redundantOpCount++; 447 | journalWriter.append(READ + ' ' + key + '\n'); 448 | if (journalRebuildRequired()) { 449 | executorService.submit(cleanupCallable); 450 | } 451 | 452 | return new Snapshot(key, entry.sequenceNumber, ins, entry.lengths); 453 | } 454 | 455 | /** 456 | * Returns an editor for the entry named {@code key}, or null if another 457 | * edit is in progress. 458 | */ 459 | public Editor edit(String key) throws IOException { 460 | return edit(key, ANY_SEQUENCE_NUMBER); 461 | } 462 | 463 | private synchronized Editor edit(String key, long expectedSequenceNumber) throws IOException { 464 | checkNotClosed(); 465 | validateKey(key); 466 | Entry entry = lruEntries.get(key); 467 | if (expectedSequenceNumber != ANY_SEQUENCE_NUMBER && (entry == null 468 | || entry.sequenceNumber != expectedSequenceNumber)) { 469 | return null; // Snapshot is stale. 470 | } 471 | if (entry == null) { 472 | entry = new Entry(key); 473 | lruEntries.put(key, entry); 474 | } else if (entry.currentEditor != null) { 475 | return null; // Another edit is in progress. 476 | } 477 | 478 | Editor editor = new Editor(entry); 479 | entry.currentEditor = editor; 480 | 481 | // Flush the journal before creating files to prevent file leaks. 482 | journalWriter.write(DIRTY + ' ' + key + '\n'); 483 | journalWriter.flush(); 484 | return editor; 485 | } 486 | 487 | /** 488 | * Returns the directory where this cache stores its data. 489 | */ 490 | public File getDirectory() { 491 | return directory; 492 | } 493 | 494 | /** 495 | * Returns the maximum number of bytes that this cache should use to store 496 | * its data. 497 | */ 498 | public synchronized long getMaxSize() { 499 | return maxSize; 500 | } 501 | 502 | /** 503 | * Changes the maximum number of bytes the cache can store and queues a job 504 | * to trim the existing store, if necessary. 505 | */ 506 | public synchronized void setMaxSize(long maxSize) { 507 | this.maxSize = maxSize; 508 | executorService.submit(cleanupCallable); 509 | } 510 | 511 | /** 512 | * Returns the number of bytes currently being used to store the values in 513 | * this cache. This may be greater than the max size if a background 514 | * deletion is pending. 515 | */ 516 | public synchronized long size() { 517 | return size; 518 | } 519 | 520 | private synchronized void completeEdit(Editor editor, boolean success) throws IOException { 521 | Entry entry = editor.entry; 522 | if (entry.currentEditor != editor) { 523 | throw new IllegalStateException(); 524 | } 525 | 526 | // If this edit is creating the entry for the first time, every index must have a value. 527 | if (success && !entry.readable) { 528 | for (int i = 0; i < valueCount; i++) { 529 | if (!editor.written[i]) { 530 | editor.abort(); 531 | throw new IllegalStateException("Newly created entry didn't create value for index " + i); 532 | } 533 | if (!entry.getDirtyFile(i).exists()) { 534 | editor.abort(); 535 | return; 536 | } 537 | } 538 | } 539 | 540 | for (int i = 0; i < valueCount; i++) { 541 | File dirty = entry.getDirtyFile(i); 542 | if (success) { 543 | if (dirty.exists()) { 544 | File clean = entry.getCleanFile(i); 545 | dirty.renameTo(clean); 546 | long oldLength = entry.lengths[i]; 547 | long newLength = clean.length(); 548 | entry.lengths[i] = newLength; 549 | size = size - oldLength + newLength; 550 | } 551 | } else { 552 | deleteIfExists(dirty); 553 | } 554 | } 555 | 556 | redundantOpCount++; 557 | entry.currentEditor = null; 558 | if (entry.readable | success) { 559 | entry.readable = true; 560 | journalWriter.write(CLEAN + ' ' + entry.key + entry.getLengths() + '\n'); 561 | if (success) { 562 | entry.sequenceNumber = nextSequenceNumber++; 563 | } 564 | } else { 565 | lruEntries.remove(entry.key); 566 | journalWriter.write(REMOVE + ' ' + entry.key + '\n'); 567 | } 568 | journalWriter.flush(); 569 | 570 | if (size > maxSize || journalRebuildRequired()) { 571 | executorService.submit(cleanupCallable); 572 | } 573 | } 574 | 575 | /** 576 | * We only rebuild the journal when it will halve the size of the journal 577 | * and eliminate at least 2000 ops. 578 | */ 579 | private boolean journalRebuildRequired() { 580 | final int redundantOpCompactThreshold = 2000; 581 | return redundantOpCount >= redundantOpCompactThreshold // 582 | && redundantOpCount >= lruEntries.size(); 583 | } 584 | 585 | /** 586 | * Drops the entry for {@code key} if it exists and can be removed. Entries 587 | * actively being edited cannot be removed. 588 | * 589 | * @return true if an entry was removed. 590 | */ 591 | public synchronized boolean remove(String key) throws IOException { 592 | checkNotClosed(); 593 | validateKey(key); 594 | Entry entry = lruEntries.get(key); 595 | if (entry == null || entry.currentEditor != null) { 596 | return false; 597 | } 598 | 599 | for (int i = 0; i < valueCount; i++) { 600 | File file = entry.getCleanFile(i); 601 | if (file.exists() && !file.delete()) { 602 | throw new IOException("failed to delete " + file); 603 | } 604 | size -= entry.lengths[i]; 605 | entry.lengths[i] = 0; 606 | } 607 | 608 | redundantOpCount++; 609 | journalWriter.append(REMOVE + ' ' + key + '\n'); 610 | lruEntries.remove(key); 611 | 612 | if (journalRebuildRequired()) { 613 | executorService.submit(cleanupCallable); 614 | } 615 | 616 | return true; 617 | } 618 | 619 | /** 620 | * Returns true if this cache has been closed. 621 | */ 622 | public synchronized boolean isClosed() { 623 | return journalWriter == null; 624 | } 625 | 626 | private void checkNotClosed() { 627 | if (journalWriter == null) { 628 | throw new IllegalStateException("cache is closed"); 629 | } 630 | } 631 | 632 | /** 633 | * Force buffered operations to the filesystem. 634 | */ 635 | public synchronized void flush() throws IOException { 636 | checkNotClosed(); 637 | trimToSize(); 638 | journalWriter.flush(); 639 | } 640 | 641 | /** 642 | * Closes this cache. Stored values will remain on the filesystem. 643 | */ 644 | public synchronized void close() throws IOException { 645 | if (journalWriter == null) { 646 | return; // Already closed. 647 | } 648 | for (Entry entry : new ArrayList(lruEntries.values())) { 649 | if (entry.currentEditor != null) { 650 | entry.currentEditor.abort(); 651 | } 652 | } 653 | trimToSize(); 654 | journalWriter.close(); 655 | journalWriter = null; 656 | } 657 | 658 | private void trimToSize() throws IOException { 659 | while (size > maxSize) { 660 | Map.Entry toEvict = lruEntries.entrySet().iterator().next(); 661 | remove(toEvict.getKey()); 662 | } 663 | } 664 | 665 | /** 666 | * Closes the cache and deletes all of its stored values. This will delete 667 | * all files in the cache directory including files that weren't created by 668 | * the cache. 669 | */ 670 | public void delete() throws IOException { 671 | close(); 672 | Util.deleteContents(directory); 673 | } 674 | 675 | private void validateKey(String key) { 676 | Matcher matcher = LEGAL_KEY_PATTERN.matcher(key); 677 | if (!matcher.matches()) { 678 | throw new IllegalArgumentException("keys must match regex " 679 | + STRING_KEY_PATTERN + ": \"" + key + "\""); 680 | } 681 | } 682 | 683 | private static String inputStreamToString(InputStream in) throws IOException { 684 | return Util.readFully(new InputStreamReader(in, Util.UTF_8)); 685 | } 686 | 687 | /** 688 | * A snapshot of the values for an entry. 689 | */ 690 | public final class Snapshot implements Closeable { 691 | private final String key; 692 | private final long sequenceNumber; 693 | private final InputStream[] ins; 694 | private final long[] lengths; 695 | 696 | private Snapshot(String key, long sequenceNumber, InputStream[] ins, long[] lengths) { 697 | this.key = key; 698 | this.sequenceNumber = sequenceNumber; 699 | this.ins = ins; 700 | this.lengths = lengths; 701 | } 702 | 703 | /** 704 | * Returns an editor for this snapshot's entry, or null if either the 705 | * entry has changed since this snapshot was created or if another edit 706 | * is in progress. 707 | */ 708 | public Editor edit() throws IOException { 709 | return DiskLruCache.this.edit(key, sequenceNumber); 710 | } 711 | 712 | /** 713 | * Returns the unbuffered stream with the value for {@code index}. 714 | */ 715 | public InputStream getInputStream(int index) { 716 | return ins[index]; 717 | } 718 | 719 | /** 720 | * Returns the string value for {@code index}. 721 | */ 722 | public String getString(int index) throws IOException { 723 | return inputStreamToString(getInputStream(index)); 724 | } 725 | 726 | /** 727 | * Returns the byte length of the value for {@code index}. 728 | */ 729 | public long getLength(int index) { 730 | return lengths[index]; 731 | } 732 | 733 | public void close() { 734 | for (InputStream in : ins) { 735 | Util.closeQuietly(in); 736 | } 737 | } 738 | } 739 | 740 | private static final OutputStream NULL_OUTPUT_STREAM = new OutputStream() { 741 | @Override 742 | public void write(int b) throws IOException { 743 | // Eat all writes silently. Nom nom. 744 | } 745 | }; 746 | 747 | /** 748 | * Edits the values for an entry. 749 | */ 750 | public final class Editor { 751 | private final Entry entry; 752 | private final boolean[] written; 753 | private boolean hasErrors; 754 | private boolean committed; 755 | 756 | private Editor(Entry entry) { 757 | this.entry = entry; 758 | this.written = (entry.readable) ? null : new boolean[valueCount]; 759 | } 760 | 761 | /** 762 | * Returns an unbuffered input stream to read the last committed value, 763 | * or null if no value has been committed. 764 | */ 765 | public InputStream newInputStream(int index) throws IOException { 766 | synchronized (DiskLruCache.this) { 767 | if (entry.currentEditor != this) { 768 | throw new IllegalStateException(); 769 | } 770 | if (!entry.readable) { 771 | return null; 772 | } 773 | try { 774 | return new FileInputStream(entry.getCleanFile(index)); 775 | } catch (FileNotFoundException e) { 776 | return null; 777 | } 778 | } 779 | } 780 | 781 | /** 782 | * Returns the last committed value as a string, or null if no value 783 | * has been committed. 784 | */ 785 | public String getString(int index) throws IOException { 786 | InputStream in = newInputStream(index); 787 | return in != null ? inputStreamToString(in) : null; 788 | } 789 | 790 | /** 791 | * Returns a new unbuffered output stream to write the value at 792 | * {@code index}. If the underlying output stream encounters errors 793 | * when writing to the filesystem, this edit will be aborted when 794 | * {@link #commit} is called. The returned output stream does not throw 795 | * IOExceptions. 796 | */ 797 | public OutputStream newOutputStream(int index) throws IOException { 798 | if (index < 0 || index >= valueCount) { 799 | throw new IllegalArgumentException("Expected index " + index + " to " 800 | + "be greater than 0 and less than the maximum value count " 801 | + "of " + valueCount); 802 | } 803 | synchronized (DiskLruCache.this) { 804 | if (entry.currentEditor != this) { 805 | throw new IllegalStateException(); 806 | } 807 | if (!entry.readable) { 808 | written[index] = true; 809 | } 810 | File dirtyFile = entry.getDirtyFile(index); 811 | FileOutputStream outputStream; 812 | try { 813 | outputStream = new FileOutputStream(dirtyFile); 814 | } catch (FileNotFoundException e) { 815 | // Attempt to recreate the cache directory. 816 | directory.mkdirs(); 817 | try { 818 | outputStream = new FileOutputStream(dirtyFile); 819 | } catch (FileNotFoundException e2) { 820 | // We are unable to recover. Silently eat the writes. 821 | return NULL_OUTPUT_STREAM; 822 | } 823 | } 824 | return new FaultHidingOutputStream(outputStream); 825 | } 826 | } 827 | 828 | /** 829 | * Sets the value at {@code index} to {@code value}. 830 | */ 831 | public void set(int index, String value) throws IOException { 832 | Writer writer = null; 833 | try { 834 | writer = new OutputStreamWriter(newOutputStream(index), Util.UTF_8); 835 | writer.write(value); 836 | } finally { 837 | Util.closeQuietly(writer); 838 | } 839 | } 840 | 841 | /** 842 | * Commits this edit so it is visible to readers. This releases the 843 | * edit lock so another edit may be started on the same key. 844 | */ 845 | public void commit() throws IOException { 846 | if (hasErrors) { 847 | completeEdit(this, false); 848 | remove(entry.key); // The previous entry is stale. 849 | } else { 850 | completeEdit(this, true); 851 | } 852 | committed = true; 853 | } 854 | 855 | /** 856 | * Aborts this edit. This releases the edit lock so another edit may be 857 | * started on the same key. 858 | */ 859 | public void abort() throws IOException { 860 | completeEdit(this, false); 861 | } 862 | 863 | public void abortUnlessCommitted() { 864 | if (!committed) { 865 | try { 866 | abort(); 867 | } catch (IOException ignored) { 868 | } 869 | } 870 | } 871 | 872 | private class FaultHidingOutputStream extends FilterOutputStream { 873 | private FaultHidingOutputStream(OutputStream out) { 874 | super(out); 875 | } 876 | 877 | @Override 878 | public void write(int oneByte) { 879 | try { 880 | out.write(oneByte); 881 | } catch (IOException e) { 882 | hasErrors = true; 883 | } 884 | } 885 | 886 | @Override 887 | public void write(byte[] buffer, int offset, int length) { 888 | try { 889 | out.write(buffer, offset, length); 890 | } catch (IOException e) { 891 | hasErrors = true; 892 | } 893 | } 894 | 895 | @Override 896 | public void close() { 897 | try { 898 | out.close(); 899 | } catch (IOException e) { 900 | hasErrors = true; 901 | } 902 | } 903 | 904 | @Override 905 | public void flush() { 906 | try { 907 | out.flush(); 908 | } catch (IOException e) { 909 | hasErrors = true; 910 | } 911 | } 912 | } 913 | } 914 | 915 | private final class Entry { 916 | private final String key; 917 | 918 | /** 919 | * Lengths of this entry's files. 920 | */ 921 | private final long[] lengths; 922 | 923 | /** 924 | * True if this entry has ever been published. 925 | */ 926 | private boolean readable; 927 | 928 | /** 929 | * The ongoing edit or null if this entry is not being edited. 930 | */ 931 | private Editor currentEditor; 932 | 933 | /** 934 | * The sequence number of the most recently committed edit to this entry. 935 | */ 936 | private long sequenceNumber; 937 | 938 | private Entry(String key) { 939 | this.key = key; 940 | this.lengths = new long[valueCount]; 941 | } 942 | 943 | public String getLengths() throws IOException { 944 | StringBuilder result = new StringBuilder(); 945 | for (long size : lengths) { 946 | result.append(' ').append(size); 947 | } 948 | return result.toString(); 949 | } 950 | 951 | /** 952 | * Set lengths using decimal numbers like "10123". 953 | */ 954 | private void setLengths(String[] strings) throws IOException { 955 | if (strings.length != valueCount) { 956 | throw invalidLengths(strings); 957 | } 958 | 959 | try { 960 | for (int i = 0; i < strings.length; i++) { 961 | lengths[i] = Long.parseLong(strings[i]); 962 | } 963 | } catch (NumberFormatException e) { 964 | throw invalidLengths(strings); 965 | } 966 | } 967 | 968 | private IOException invalidLengths(String[] strings) throws IOException { 969 | throw new IOException("unexpected journal line: " + java.util.Arrays.toString(strings)); 970 | } 971 | 972 | public File getCleanFile(int i) { 973 | return new File(directory, key + "." + i); 974 | } 975 | 976 | public File getDirtyFile(int i) { 977 | return new File(directory, key + "." + i + ".tmp"); 978 | } 979 | } 980 | } 981 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/disklrucahe/DiskLruCacheHelper.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.disklrucahe; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.drawable.Drawable; 6 | import android.os.Environment; 7 | import android.util.Log; 8 | 9 | import com.softtanck.imageloader.imageloader.utils.Utils; 10 | 11 | import org.json.JSONArray; 12 | import org.json.JSONException; 13 | import org.json.JSONObject; 14 | 15 | import java.io.BufferedWriter; 16 | import java.io.ByteArrayOutputStream; 17 | import java.io.File; 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.io.ObjectInputStream; 21 | import java.io.ObjectOutputStream; 22 | import java.io.OutputStream; 23 | import java.io.OutputStreamWriter; 24 | import java.io.Serializable; 25 | 26 | /** 27 | * Created by zhy on 15/7/28. 28 | */ 29 | public class DiskLruCacheHelper { 30 | private static final String DIR_NAME = "diskCache"; 31 | private static final int MAX_COUNT = 5 * 1024 * 1024; 32 | private static final int DEFAULT_APP_VERSION = 1; 33 | 34 | private static final String TAG = "DiskLruCacheHelper"; 35 | 36 | private DiskLruCache mDiskLruCache; 37 | 38 | public DiskLruCacheHelper(Context context) throws IOException { 39 | mDiskLruCache = generateCache(context, DIR_NAME, MAX_COUNT); 40 | } 41 | 42 | public DiskLruCacheHelper(Context context, String dirName) throws IOException { 43 | mDiskLruCache = generateCache(context, dirName, MAX_COUNT); 44 | } 45 | 46 | public DiskLruCacheHelper(Context context, String dirName, int maxCount) throws IOException { 47 | mDiskLruCache = generateCache(context, dirName, maxCount); 48 | } 49 | 50 | //custom cache dir 51 | public DiskLruCacheHelper(File dir) throws IOException { 52 | mDiskLruCache = generateCache(null, dir, MAX_COUNT); 53 | } 54 | 55 | public DiskLruCacheHelper(Context context, File dir) throws IOException { 56 | mDiskLruCache = generateCache(context, dir, MAX_COUNT); 57 | } 58 | 59 | public DiskLruCacheHelper(Context context, File dir, int maxCount) throws IOException { 60 | mDiskLruCache = generateCache(context, dir, maxCount); 61 | } 62 | 63 | private DiskLruCache generateCache(Context context, File dir, int maxCount) throws IOException { 64 | if (!dir.exists() || !dir.isDirectory()) { 65 | throw new IllegalArgumentException( 66 | dir + " is not a directory or does not exists. "); 67 | } 68 | 69 | int appVersion = context == null ? DEFAULT_APP_VERSION : Utils.getAppVersion(context); 70 | 71 | DiskLruCache diskLruCache = DiskLruCache.open( 72 | dir, 73 | appVersion, 74 | DEFAULT_APP_VERSION, 75 | maxCount); 76 | 77 | return diskLruCache; 78 | } 79 | 80 | private DiskLruCache generateCache(Context context, String dirName, int maxCount) throws IOException { 81 | DiskLruCache diskLruCache = DiskLruCache.open( 82 | getDiskCacheDir(context, dirName), 83 | Utils.getAppVersion(context), 84 | DEFAULT_APP_VERSION, 85 | maxCount); 86 | return diskLruCache; 87 | } 88 | // ======================================= 89 | // ============== String 数据 读写 ============= 90 | // ======================================= 91 | 92 | public void put(String key, String value) { 93 | DiskLruCache.Editor edit = null; 94 | BufferedWriter bw = null; 95 | try { 96 | edit = editor(key); 97 | if (edit == null) return; 98 | OutputStream os = edit.newOutputStream(0); 99 | bw = new BufferedWriter(new OutputStreamWriter(os)); 100 | bw.write(value); 101 | edit.commit();//write CLEAN 102 | } catch (IOException e) { 103 | e.printStackTrace(); 104 | try { 105 | //s 106 | edit.abort();//write REMOVE 107 | } catch (IOException e1) { 108 | e1.printStackTrace(); 109 | } 110 | } finally { 111 | try { 112 | if (bw != null) 113 | bw.close(); 114 | } catch (IOException e) { 115 | e.printStackTrace(); 116 | } 117 | } 118 | } 119 | 120 | public String getAsString(String key) { 121 | InputStream inputStream = null; 122 | try { 123 | //write READ 124 | inputStream = get(key); 125 | if (inputStream == null) return null; 126 | StringBuilder sb = new StringBuilder(); 127 | int len = 0; 128 | byte[] buf = new byte[128]; 129 | while ((len = inputStream.read(buf)) != -1) { 130 | sb.append(new String(buf, 0, len)); 131 | } 132 | return sb.toString(); 133 | 134 | 135 | } catch (IOException e) { 136 | e.printStackTrace(); 137 | if (inputStream != null) 138 | try { 139 | inputStream.close(); 140 | } catch (IOException e1) { 141 | e1.printStackTrace(); 142 | } 143 | } 144 | return null; 145 | } 146 | 147 | 148 | public void put(String key, JSONObject jsonObject) { 149 | put(key, jsonObject.toString()); 150 | } 151 | 152 | public JSONObject getAsJson(String key) { 153 | String val = getAsString(key); 154 | try { 155 | if (val != null) 156 | return new JSONObject(val); 157 | } catch (JSONException e) { 158 | e.printStackTrace(); 159 | } 160 | return null; 161 | } 162 | 163 | // ======================================= 164 | // ============ JSONArray 数据 读写 ============= 165 | // ======================================= 166 | 167 | public void put(String key, JSONArray jsonArray) { 168 | put(key, jsonArray.toString()); 169 | } 170 | 171 | public JSONArray getAsJSONArray(String key) { 172 | String JSONString = getAsString(key); 173 | try { 174 | JSONArray obj = new JSONArray(JSONString); 175 | return obj; 176 | } catch (Exception e) { 177 | e.printStackTrace(); 178 | return null; 179 | } 180 | } 181 | 182 | // ======================================= 183 | // ============== byte 数据 读写 ============= 184 | // ======================================= 185 | 186 | /** 187 | * 保存 byte数据 到 缓存中 188 | * 189 | * @param key 保存的key 190 | * @param value 保存的数据 191 | */ 192 | public void put(String key, byte[] value) { 193 | OutputStream out = null; 194 | DiskLruCache.Editor editor = null; 195 | try { 196 | editor = editor(key); 197 | if (editor == null) { 198 | return; 199 | } 200 | out = editor.newOutputStream(0); 201 | out.write(value); 202 | out.flush(); 203 | editor.commit();//write CLEAN 204 | } catch (Exception e) { 205 | e.printStackTrace(); 206 | try { 207 | editor.abort();//write REMOVE 208 | } catch (IOException e1) { 209 | e1.printStackTrace(); 210 | } 211 | 212 | } finally { 213 | if (out != null) { 214 | try { 215 | out.close(); 216 | } catch (IOException e) { 217 | e.printStackTrace(); 218 | } 219 | } 220 | } 221 | } 222 | 223 | 224 | public byte[] getAsBytes(String key) { 225 | byte[] res = null; 226 | InputStream is = get(key); 227 | if (is == null) return null; 228 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 229 | try { 230 | byte[] buf = new byte[256]; 231 | int len = 0; 232 | while ((len = is.read(buf)) != -1) { 233 | baos.write(buf, 0, len); 234 | } 235 | res = baos.toByteArray(); 236 | } catch (IOException e) { 237 | e.printStackTrace(); 238 | } 239 | return res; 240 | } 241 | 242 | 243 | // ======================================= 244 | // ============== 序列化 数据 读写 ============= 245 | // ======================================= 246 | public void put(String key, Serializable value) { 247 | DiskLruCache.Editor editor = editor(key); 248 | ObjectOutputStream oos = null; 249 | if (editor == null) return; 250 | try { 251 | OutputStream os = editor.newOutputStream(0); 252 | oos = new ObjectOutputStream(os); 253 | oos.writeObject(value); 254 | oos.flush(); 255 | editor.commit(); 256 | } catch (IOException e) { 257 | e.printStackTrace(); 258 | try { 259 | editor.abort(); 260 | } catch (IOException e1) { 261 | e1.printStackTrace(); 262 | } 263 | } finally { 264 | try { 265 | if (oos != null) 266 | oos.close(); 267 | } catch (IOException e) { 268 | e.printStackTrace(); 269 | } 270 | } 271 | } 272 | 273 | public T getAsSerializable(String key) { 274 | T t = null; 275 | InputStream is = get(key); 276 | ObjectInputStream ois = null; 277 | if (is == null) return null; 278 | try { 279 | ois = new ObjectInputStream(is); 280 | t = (T) ois.readObject(); 281 | } catch (ClassNotFoundException e) { 282 | e.printStackTrace(); 283 | } catch (IOException e) { 284 | e.printStackTrace(); 285 | } finally { 286 | try { 287 | if (ois != null) 288 | ois.close(); 289 | } catch (IOException e) { 290 | e.printStackTrace(); 291 | } 292 | } 293 | return t; 294 | } 295 | 296 | // ======================================= 297 | // ============== bitmap 数据 读写 ============= 298 | // ======================================= 299 | public void put(String key, Bitmap bitmap) { 300 | put(key, Utils.bitmap2Bytes(bitmap)); 301 | } 302 | 303 | public Bitmap getAsBitmap(String key) { 304 | byte[] bytes = getAsBytes(key); 305 | if (bytes == null) return null; 306 | return Utils.bytes2Bitmap(bytes); 307 | } 308 | 309 | // ======================================= 310 | // ============= drawable 数据 读写 ============= 311 | // ======================================= 312 | public void put(String key, Drawable value) { 313 | put(key, Utils.drawable2Bitmap(value)); 314 | } 315 | 316 | public Drawable getAsDrawable(String key) { 317 | byte[] bytes = getAsBytes(key); 318 | if (bytes == null) { 319 | return null; 320 | } 321 | return Utils.bitmap2Drawable(Utils.bytes2Bitmap(bytes)); 322 | } 323 | 324 | // ======================================= 325 | // ============= other methods ============= 326 | // ======================================= 327 | public boolean remove(String key) { 328 | try { 329 | key = Utils.hashKeyForDisk(key); 330 | return mDiskLruCache.remove(key); 331 | } catch (IOException e) { 332 | e.printStackTrace(); 333 | } 334 | return false; 335 | } 336 | 337 | public void close() throws IOException { 338 | mDiskLruCache.close(); 339 | } 340 | 341 | public void delete() throws IOException { 342 | mDiskLruCache.delete(); 343 | } 344 | 345 | public void flush() throws IOException { 346 | mDiskLruCache.flush(); 347 | } 348 | 349 | public boolean isClosed() { 350 | return mDiskLruCache.isClosed(); 351 | } 352 | 353 | public long size() { 354 | return mDiskLruCache.size(); 355 | } 356 | 357 | public void setMaxSize(long maxSize) { 358 | mDiskLruCache.setMaxSize(maxSize); 359 | } 360 | 361 | public File getDirectory() { 362 | return mDiskLruCache.getDirectory(); 363 | } 364 | 365 | public long getMaxSize() { 366 | return mDiskLruCache.getMaxSize(); 367 | } 368 | 369 | 370 | // ======================================= 371 | // ===遇到文件比较大的,可以直接通过流读写 ===== 372 | // ======================================= 373 | //basic editor 374 | public DiskLruCache.Editor editor(String key) { 375 | try { 376 | key = Utils.hashKeyForDisk(key); 377 | //wirte DIRTY 378 | DiskLruCache.Editor edit = mDiskLruCache.edit(key); 379 | //edit maybe null :the entry is editing 380 | if (edit == null) { 381 | Log.w(TAG, "the entry spcified key:" + key + " is editing by other . "); 382 | } 383 | return edit; 384 | } catch (IOException e) { 385 | e.printStackTrace(); 386 | } 387 | 388 | return null; 389 | } 390 | 391 | 392 | //basic get 393 | public InputStream get(String key) { 394 | try { 395 | DiskLruCache.Snapshot snapshot = mDiskLruCache.get(Utils.hashKeyForDisk(key)); 396 | if (snapshot == null) //not find entry , or entry.readable = false 397 | { 398 | Log.e(TAG, "not find entry , or entry.readable = false"); 399 | return null; 400 | } 401 | //write READ 402 | return snapshot.getInputStream(0); 403 | 404 | } catch (IOException e) { 405 | e.printStackTrace(); 406 | return null; 407 | } 408 | 409 | } 410 | 411 | 412 | // ======================================= 413 | // ============== 序列化 数据 读写 ============= 414 | // ======================================= 415 | 416 | private File getDiskCacheDir(Context context, String uniqueName) { 417 | String cachePath; 418 | if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) 419 | || !Environment.isExternalStorageRemovable()) { 420 | cachePath = context.getExternalCacheDir().getPath(); 421 | } else { 422 | cachePath = context.getCacheDir().getPath(); 423 | } 424 | return new File(cachePath + File.separator + uniqueName); 425 | } 426 | 427 | } 428 | 429 | 430 | 431 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/disklrucahe/StrictLineReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.softtanck.imageloader.imageloader.disklrucahe; 18 | 19 | import java.io.ByteArrayOutputStream; 20 | import java.io.Closeable; 21 | import java.io.EOFException; 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | import java.io.UnsupportedEncodingException; 25 | import java.nio.charset.Charset; 26 | 27 | /** 28 | * Buffers input from an {@link InputStream} for reading lines. 29 | * 30 | *

This class is used for buffered reading of lines. For purposes of this class, a line ends 31 | * with "\n" or "\r\n". End of input is reported by throwing {@code EOFException}. Unterminated 32 | * line at end of input is invalid and will be ignored, the caller may use {@code 33 | * hasUnterminatedLine()} to detect it after catching the {@code EOFException}. 34 | * 35 | *

This class is intended for reading input that strictly consists of lines, such as line-based 36 | * cache entries or cache journal. Unlike the {@link java.io.BufferedReader} which in conjunction 37 | * with {@link java.io.InputStreamReader} provides similar functionality, this class uses different 38 | * end-of-input reporting and a more restrictive definition of a line. 39 | * 40 | *

This class supports only charsets that encode '\r' and '\n' as a single byte with value 13 41 | * and 10, respectively, and the representation of no other character contains these values. 42 | * We currently check in constructor that the charset is one of US-ASCII, UTF-8 and ISO-8859-1. 43 | * The default charset is US_ASCII. 44 | */ 45 | class StrictLineReader implements Closeable { 46 | private static final byte CR = (byte) '\r'; 47 | private static final byte LF = (byte) '\n'; 48 | 49 | private final InputStream in; 50 | private final Charset charset; 51 | 52 | /* 53 | * Buffered data is stored in {@code buf}. As long as no exception occurs, 0 <= pos <= end 54 | * and the data in the range [pos, end) is buffered for reading. At end of input, if there is 55 | * an unterminated line, we set end == -1, otherwise end == pos. If the underlying 56 | * {@code InputStream} throws an {@code IOException}, end may remain as either pos or -1. 57 | */ 58 | private byte[] buf; 59 | private int pos; 60 | private int end; 61 | 62 | /** 63 | * Constructs a new {@code LineReader} with the specified charset and the default capacity. 64 | * 65 | * @param in the {@code InputStream} to read data from. 66 | * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are 67 | * supported. 68 | * @throws NullPointerException if {@code in} or {@code charset} is null. 69 | * @throws IllegalArgumentException if the specified charset is not supported. 70 | */ 71 | public StrictLineReader(InputStream in, Charset charset) { 72 | this(in, 8192, charset); 73 | } 74 | 75 | /** 76 | * Constructs a new {@code LineReader} with the specified capacity and charset. 77 | * 78 | * @param in the {@code InputStream} to read data from. 79 | * @param capacity the capacity of the buffer. 80 | * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are 81 | * supported. 82 | * @throws NullPointerException if {@code in} or {@code charset} is null. 83 | * @throws IllegalArgumentException if {@code capacity} is negative or zero 84 | * or the specified charset is not supported. 85 | */ 86 | public StrictLineReader(InputStream in, int capacity, Charset charset) { 87 | if (in == null || charset == null) { 88 | throw new NullPointerException(); 89 | } 90 | if (capacity < 0) { 91 | throw new IllegalArgumentException("capacity <= 0"); 92 | } 93 | if (!(charset.equals(Util.US_ASCII))) { 94 | throw new IllegalArgumentException("Unsupported encoding"); 95 | } 96 | 97 | this.in = in; 98 | this.charset = charset; 99 | buf = new byte[capacity]; 100 | } 101 | 102 | /** 103 | * Closes the reader by closing the underlying {@code InputStream} and 104 | * marking this reader as closed. 105 | * 106 | * @throws IOException for errors when closing the underlying {@code InputStream}. 107 | */ 108 | public void close() throws IOException { 109 | synchronized (in) { 110 | if (buf != null) { 111 | buf = null; 112 | in.close(); 113 | } 114 | } 115 | } 116 | 117 | /** 118 | * Reads the next line. A line ends with {@code "\n"} or {@code "\r\n"}, 119 | * this end of line marker is not included in the result. 120 | * 121 | * @return the next line from the input. 122 | * @throws IOException for underlying {@code InputStream} errors. 123 | * @throws EOFException for the end of source stream. 124 | */ 125 | public String readLine() throws IOException { 126 | synchronized (in) { 127 | if (buf == null) { 128 | throw new IOException("LineReader is closed"); 129 | } 130 | 131 | // Read more data if we are at the end of the buffered data. 132 | // Though it's an error to read after an exception, we will let {@code fillBuf()} 133 | // throw again if that happens; thus we need to handle end == -1 as well as end == pos. 134 | if (pos >= end) { 135 | fillBuf(); 136 | } 137 | // Try to find LF in the buffered data and return the line if successful. 138 | for (int i = pos; i != end; ++i) { 139 | if (buf[i] == LF) { 140 | int lineEnd = (i != pos && buf[i - 1] == CR) ? i - 1 : i; 141 | String res = new String(buf, pos, lineEnd - pos, charset.name()); 142 | pos = i + 1; 143 | return res; 144 | } 145 | } 146 | 147 | // Let's anticipate up to 80 characters on top of those already read. 148 | ByteArrayOutputStream out = new ByteArrayOutputStream(end - pos + 80) { 149 | @Override 150 | public String toString() { 151 | int length = (count > 0 && buf[count - 1] == CR) ? count - 1 : count; 152 | try { 153 | return new String(buf, 0, length, charset.name()); 154 | } catch (UnsupportedEncodingException e) { 155 | throw new AssertionError(e); // Since we control the charset this will never happen. 156 | } 157 | } 158 | }; 159 | 160 | while (true) { 161 | out.write(buf, pos, end - pos); 162 | // Mark unterminated line in case fillBuf throws EOFException or IOException. 163 | end = -1; 164 | fillBuf(); 165 | // Try to find LF in the buffered data and return the line if successful. 166 | for (int i = pos; i != end; ++i) { 167 | if (buf[i] == LF) { 168 | if (i != pos) { 169 | out.write(buf, pos, i - pos); 170 | } 171 | pos = i + 1; 172 | return out.toString(); 173 | } 174 | } 175 | } 176 | } 177 | } 178 | 179 | public boolean hasUnterminatedLine() { 180 | return end == -1; 181 | } 182 | 183 | /** 184 | * Reads new input data into the buffer. Call only with pos == end or end == -1, 185 | * depending on the desired outcome if the function throws. 186 | */ 187 | private void fillBuf() throws IOException { 188 | int result = in.read(buf, 0, buf.length); 189 | if (result == -1) { 190 | throw new EOFException(); 191 | } 192 | pos = 0; 193 | end = result; 194 | } 195 | } 196 | 197 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/disklrucahe/Util.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.softtanck.imageloader.imageloader.disklrucahe; 18 | 19 | import java.io.Closeable; 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.io.Reader; 23 | import java.io.StringWriter; 24 | import java.nio.charset.Charset; 25 | 26 | /** 27 | * Junk drawer of utility methods. 28 | */ 29 | final class Util { 30 | static final Charset US_ASCII = Charset.forName("US-ASCII"); 31 | static final Charset UTF_8 = Charset.forName("UTF-8"); 32 | 33 | private Util() { 34 | } 35 | 36 | static String readFully(Reader reader) throws IOException { 37 | try { 38 | StringWriter writer = new StringWriter(); 39 | char[] buffer = new char[1024]; 40 | int count; 41 | while ((count = reader.read(buffer)) != -1) { 42 | writer.write(buffer, 0, count); 43 | } 44 | return writer.toString(); 45 | } finally { 46 | reader.close(); 47 | } 48 | } 49 | 50 | /** 51 | * Deletes the contents of {@code dir}. Throws an IOException if any file 52 | * could not be deleted, or if {@code dir} is not a readable directory. 53 | */ 54 | static void deleteContents(File dir) throws IOException { 55 | File[] files = dir.listFiles(); 56 | if (files == null) { 57 | throw new IOException("not a readable directory: " + dir); 58 | } 59 | for (File file : files) { 60 | if (file.isDirectory()) { 61 | deleteContents(file); 62 | } 63 | if (!file.delete()) { 64 | throw new IOException("failed to delete file: " + file); 65 | } 66 | } 67 | } 68 | 69 | static void closeQuietly(/*Auto*/Closeable closeable) { 70 | if (closeable != null) { 71 | try { 72 | closeable.close(); 73 | } catch (RuntimeException rethrown) { 74 | throw rethrown; 75 | } catch (Exception ignored) { 76 | } 77 | } 78 | } 79 | 80 | 81 | } 82 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/listener/AnimListener.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.listener; 2 | 3 | import android.view.View; 4 | 5 | /** 6 | * @author : Tanck 7 | * @Description : TODO 动画接口类 8 | * @date 10/20/2015 9 | */ 10 | public interface AnimListener { 11 | 12 | /** 13 | * 获取动画视图 14 | * 15 | * @return 16 | */ 17 | public View getAnimView(); 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/listener/LoadListener.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.listener; 2 | 3 | import android.graphics.Bitmap; 4 | import android.view.View; 5 | 6 | /** 7 | * @author : Tanck 8 | * @Description : TODO 图片加载接口 9 | * @date 10/20/2015 10 | */ 11 | public interface LoadListener { 12 | 13 | /** 14 | * 加载中 15 | * 16 | * @param view 17 | * @param path 18 | * @param 19 | */ 20 | public void Loading(View view, String path); 21 | 22 | /** 23 | * 加载成功 24 | * 25 | * @param view 26 | * @param bitmap 27 | * @param path 28 | * @param 29 | */ 30 | public void LoadSuccess(View view, Bitmap bitmap, String path); 31 | 32 | /** 33 | * 加载失败 34 | * 35 | * @param view 36 | * @param path 37 | * @param errorMsg 38 | * @param 39 | */ 40 | public void LoadError(View view, String path, String errorMsg); 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/utils/BaseCache.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.utils; 2 | 3 | import android.graphics.Bitmap; 4 | 5 | /** 6 | * @author : Tanck 7 | * @Description : TODO 8 | * @date 10/19/2015 9 | */ 10 | public interface BaseCache { 11 | 12 | /** 13 | * 获取缓存 14 | * 15 | * @param key 16 | * @return 17 | */ 18 | public Bitmap get(String key); 19 | 20 | /** 21 | * 设置缓存 22 | * 23 | * @param key 24 | * @param value 25 | * @return 26 | */ 27 | public void put(String key, Bitmap value); 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/utils/ImageSizeUtils.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.utils; 2 | 3 | /** 4 | * @author : Tanck 5 | * @Description : TODO 6 | * @date 10/20/2015 7 | */ 8 | public class ImageSizeUtils { 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/utils/LruCacheUtils.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.utils; 2 | 3 | import android.graphics.Bitmap; 4 | import android.support.v4.util.LruCache; 5 | 6 | /** 7 | * @author : Tanck 8 | * @Description : TODO 9 | * @date 10/19/2015 10 | */ 11 | public class LruCacheUtils implements BaseCache { 12 | 13 | /** 14 | * 内存缓存 15 | */ 16 | private LruCache mImageCache; 17 | 18 | 19 | private static LruCacheUtils lruCacheUtils; 20 | 21 | private LruCacheUtils() { 22 | 23 | initCache(); 24 | 25 | } 26 | 27 | private void initCache() { 28 | //内存 29 | int maxMemory = (int) Runtime.getRuntime().maxMemory(); 30 | maxMemory = maxMemory / 4; 31 | mImageCache = new LruCache(maxMemory) { 32 | @Override 33 | protected int sizeOf(String key, Bitmap value) { 34 | return value.getRowBytes() * value.getHeight() / 1024; 35 | } 36 | }; 37 | 38 | } 39 | 40 | public static LruCacheUtils getInstance() { 41 | if (null == lruCacheUtils) { 42 | lruCacheUtils = new LruCacheUtils(); 43 | } 44 | return lruCacheUtils; 45 | } 46 | 47 | public void put(String key, Bitmap value) { 48 | if (null == key) 49 | throw new RuntimeException("this key is null"); 50 | synchronized (LruCacheUtils.class) { 51 | //判断是否需要缓存到磁盘 52 | mImageCache.put(key, value); 53 | } 54 | } 55 | 56 | public Bitmap get(String key) { 57 | if (null == key) 58 | throw new RuntimeException("this key is null"); 59 | synchronized (LruCacheUtils.class) { 60 | //判断是否需要从磁盘中获取 61 | return mImageCache.get(key); 62 | } 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/utils/MD5Code.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.utils; 2 | 3 | import java.security.MessageDigest; 4 | 5 | /** 6 | * @author : Tanck 7 | * @Description : TODO 8 | * @date 10/20/2015 9 | */ 10 | public class MD5Code { 11 | 12 | public final static String MD5(String s) { 13 | char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 14 | try { 15 | byte[] btInput = s.getBytes(); 16 | // 获得MD5摘要算法的 MessageDigest 对象 17 | MessageDigest mdInst = MessageDigest.getInstance("MD5"); 18 | // 使用指定的字节更新摘要 19 | mdInst.update(btInput); 20 | // 获得密文 21 | byte[] md = mdInst.digest(); 22 | // 把密文转换成十六进制的字符串形式 23 | int j = md.length; 24 | char str[] = new char[j * 2]; 25 | int k = 0; 26 | for (int i = 0; i < j; i++) { 27 | byte byte0 = md[i]; 28 | str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 29 | str[k++] = hexDigits[byte0 & 0xf]; 30 | } 31 | return new String(str); 32 | } catch (Exception e) { 33 | e.printStackTrace(); 34 | return null; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/softtanck/imageloader/imageloader/utils/Utils.java: -------------------------------------------------------------------------------- 1 | package com.softtanck.imageloader.imageloader.utils; 2 | 3 | import android.content.Context; 4 | import android.content.pm.PackageInfo; 5 | import android.content.pm.PackageManager; 6 | import android.graphics.Bitmap; 7 | import android.graphics.BitmapFactory; 8 | import android.graphics.Canvas; 9 | import android.graphics.PixelFormat; 10 | import android.graphics.drawable.BitmapDrawable; 11 | import android.graphics.drawable.Drawable; 12 | 13 | import java.io.ByteArrayOutputStream; 14 | import java.security.MessageDigest; 15 | import java.security.NoSuchAlgorithmException; 16 | 17 | public class Utils { 18 | public static int getAppVersion(Context context) { 19 | try { 20 | PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); 21 | return info.versionCode; 22 | } catch (PackageManager.NameNotFoundException e) { 23 | e.printStackTrace(); 24 | } 25 | return 1; 26 | } 27 | 28 | 29 | public static String hashKeyForDisk(String key) { 30 | String cacheKey; 31 | try { 32 | final MessageDigest mDigest = MessageDigest.getInstance("MD5"); 33 | mDigest.update(key.getBytes()); 34 | cacheKey = bytesToHexString(mDigest.digest()); 35 | } catch (NoSuchAlgorithmException e) { 36 | cacheKey = String.valueOf(key.hashCode()); 37 | } 38 | return cacheKey; 39 | } 40 | 41 | public static String bytesToHexString(byte[] bytes) { 42 | StringBuilder sb = new StringBuilder(); 43 | for (int i = 0; i < bytes.length; i++) { 44 | String hex = Integer.toHexString(0xFF & bytes[i]); 45 | if (hex.length() == 1) { 46 | sb.append('0'); 47 | } 48 | sb.append(hex); 49 | } 50 | return sb.toString(); 51 | } 52 | 53 | public static byte[] bitmap2Bytes(Bitmap bm) { 54 | if (bm == null) { 55 | return null; 56 | } 57 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 58 | bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 59 | return baos.toByteArray(); 60 | } 61 | 62 | public static Bitmap bytes2Bitmap(byte[] bytes) { 63 | return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 64 | } 65 | 66 | 67 | /** 68 | * Drawable → Bitmap 69 | */ 70 | public static Bitmap drawable2Bitmap(Drawable drawable) { 71 | if (drawable == null) { 72 | return null; 73 | } 74 | // 取 drawable 的长宽 75 | int w = drawable.getIntrinsicWidth(); 76 | int h = drawable.getIntrinsicHeight(); 77 | // 取 drawable 的颜色格式 78 | Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; 79 | // 建立对应 bitmap 80 | Bitmap bitmap = Bitmap.createBitmap(w, h, config); 81 | // 建立对应 bitmap 的画布 82 | Canvas canvas = new Canvas(bitmap); 83 | drawable.setBounds(0, 0, w, h); 84 | // 把 drawable 内容画到画布中 85 | drawable.draw(canvas); 86 | return bitmap; 87 | } 88 | 89 | /* 90 | * Bitmap → Drawable 91 | */ 92 | @SuppressWarnings("deprecation") 93 | public static Drawable bitmap2Drawable(Bitmap bm) { 94 | if (bm == null) { 95 | return null; 96 | } 97 | BitmapDrawable bd = new BitmapDrawable(bm); 98 | bd.setTargetDensity(bm.getDensity()); 99 | return new BitmapDrawable(bm); 100 | } 101 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/pictures_no.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Softtanck/ImageLoader/61fa1df7f9689d31cc955db695cfef6f10fcfbcf/app/src/main/res/drawable/pictures_no.png -------------------------------------------------------------------------------- /app/src/main/res/layout/item.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 |

4 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Softtanck/ImageLoader/61fa1df7f9689d31cc955db695cfef6f10fcfbcf/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Softtanck/ImageLoader/61fa1df7f9689d31cc955db695cfef6f10fcfbcf/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Softtanck/ImageLoader/61fa1df7f9689d31cc955db695cfef6f10fcfbcf/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Softtanck/ImageLoader/61fa1df7f9689d31cc955db695cfef6f10fcfbcf/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ImageLoader 3 | 4 | Hello world! 5 | Settings 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.2.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Softtanck/ImageLoader/61fa1df7f9689d31cc955db695cfef6f10fcfbcf/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /test.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Softtanck/ImageLoader/61fa1df7f9689d31cc955db695cfef6f10fcfbcf/test.gif --------------------------------------------------------------------------------