├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ └── styles.xml │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ ├── icon_pic_errow.png │ │ │ └── icon_pic_loding.png │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ └── layout │ │ │ └── activity_main.xml │ │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── vankain │ │ │ └── imageloaderfactory │ │ │ ├── ImageFrameworkFactory.java │ │ │ ├── ImageLoaderProduct.java │ │ │ ├── ImageConfigProduct.java │ │ │ ├── UILFraworkFactory.java │ │ │ ├── GlideFrameworkFactory.java │ │ │ ├── PicassoFramworkFactory.java │ │ │ ├── UILLoaderProduct.java │ │ │ ├── ImageLoaderUtils.java │ │ │ ├── PicassoWrapper.java │ │ │ ├── PicassoLoaderProduct.java │ │ │ ├── GlideLoaderProduct.java │ │ │ ├── GlideWrapper.java │ │ │ ├── GlideConfigProduct.java │ │ │ ├── PicassoConfigProduct.java │ │ │ ├── UILConfigProduct.java │ │ │ ├── BaseApplication.java │ │ │ ├── MainActivity.java │ │ │ ├── ImageLoaderWrapper.java │ │ │ └── BitmapUtils.java │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── .gitignore └── README.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ImageLoaderFactory 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VankaIn/ImageLoaderFactory/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VankaIn/ImageLoaderFactory/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VankaIn/ImageLoaderFactory/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_pic_errow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VankaIn/ImageLoaderFactory/HEAD/app/src/main/res/mipmap-xhdpi/icon_pic_errow.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VankaIn/ImageLoaderFactory/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VankaIn/ImageLoaderFactory/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_pic_loding.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VankaIn/ImageLoaderFactory/HEAD/app/src/main/res/mipmap-xhdpi/icon_pic_loding.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/ImageFrameworkFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | /** 4 | * Created by Administrator on 2016/2/29. 5 | */ 6 | public interface ImageFrameworkFactory { 7 | ImageLoaderProduct createImageLoader(); 8 | ImageConfigProduct createImageConfig(); 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ImageLoaderFactory 2 | 这个项目统一了图片 显示的方法 3 | 4 | # 有什么用? 5 | 当我们用了[Android-Universal-Image-Loader](https://github.com/nostra13/Android-Universal-Image-Loader/).加载图片的时候。
6 | 突然我们想更换加载图片的框架([Glide](https://github.com/bumptech/glide)),我们只能去修改我们之前自己再封装Android-Universal-Image-Loader的代码。
7 | 假如你没有再分装Android-Universal-Image-Loader,那更可怕,你只能每个activity都去改。
8 | 而这个框架是用抽象工厂分装好的,如果你想更换glide框架,只要建立glide的工厂然后进行对应的实现就可以,这样就不用修改原来的代码了。 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/ImageLoaderProduct.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | import android.widget.ImageView; 4 | 5 | /** 6 | * Created by Administrator on 2016/2/29. 7 | */ 8 | public interface ImageLoaderProduct { 9 | void display(String imageUri, ImageView imageView); 10 | void display(String imageUrl, ImageView imageView, ImageConfigProduct config); 11 | void cleanImageCache(String imageUrl); 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/ImageConfigProduct.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | /** 4 | * Created by Administrator on 2016/2/29. 5 | */ 6 | public interface ImageConfigProduct { 7 | void setDefaulRes(int defaulRes); 8 | void setLoadingRes(int loadingRes); 9 | void setFailRes(int failRes); 10 | void setsupportMemoryCache(boolean flag); 11 | void setsupportDiskCache(boolean flag); 12 | void setFadeIn(int duration); 13 | Object get(); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/UILFraworkFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | /** 4 | * Created by Administrator on 2016/2/29. 5 | */ 6 | public class UILFraworkFactory implements ImageFrameworkFactory { 7 | @Override 8 | public ImageLoaderProduct createImageLoader() { 9 | return new UILLoaderProduct(); 10 | } 11 | 12 | @Override 13 | public ImageConfigProduct createImageConfig(){ 14 | return new UILConfigProduct(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/GlideFrameworkFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | /** 4 | * Created by Administrator on 2016/2/29. 5 | */ 6 | public class GlideFrameworkFactory implements ImageFrameworkFactory { 7 | 8 | @Override 9 | public ImageLoaderProduct createImageLoader() { 10 | return new GlideLoaderProduct(); 11 | } 12 | 13 | @Override 14 | public ImageConfigProduct createImageConfig() { 15 | return new GlideConfigProduct(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/PicassoFramworkFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | /** 4 | * Created by Administrator on 2016/3/2. 5 | */ 6 | public class PicassoFramworkFactory implements ImageFrameworkFactory { 7 | @Override 8 | public ImageLoaderProduct createImageLoader() { 9 | return new PicassoLoaderProduct(); 10 | } 11 | 12 | @Override 13 | public ImageConfigProduct createImageConfig() { 14 | return new PicassoConfigProduct(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /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 C:\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/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.github.vankain.imageloaderfactory" 9 | minSdkVersion 14 10 | targetSdkVersion 23 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 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.1.1' 26 | compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3' 27 | compile 'com.github.bumptech.glide:glide:3.6.1' 28 | compile 'com.squareup.picasso:picasso:2.5.2' 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/UILLoaderProduct.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | import android.widget.ImageView; 4 | 5 | /** 6 | * Created by Administrator on 2016/2/29. 7 | */ 8 | public class UILLoaderProduct implements ImageLoaderProduct { 9 | 10 | @Override 11 | public void display(String imageUri, ImageView imageView) { 12 | ImageLoaderWrapper.getDefault().displayImage(imageUri, imageView); 13 | } 14 | 15 | @Override 16 | public void display(String imageUrl, ImageView imageView, ImageConfigProduct config) { 17 | ImageLoaderWrapper.DisplayConfig mConfig = (ImageLoaderWrapper.DisplayConfig) config.get(); 18 | ImageLoaderWrapper.getDefault().displayImage(imageUrl, imageView, mConfig); 19 | } 20 | 21 | @Override 22 | public void cleanImageCache(String url) { 23 | ImageLoaderWrapper.getDefault().clearDefaultLoaderCache(url); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/ImageLoaderUtils.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | import android.content.Context; 4 | 5 | import java.io.File; 6 | 7 | /** 8 | * Created by Administrator on 2016/2/29. 9 | */ 10 | public class ImageLoaderUtils { 11 | public static void init(Context context, File file, boolean debug) { 12 | // if() 写在配置文件,根据不同的配置创建不同的图片加载器 13 | // 初始化图片加载器模块 14 | ImageLoaderWrapper.initDefault(context, file, 15 | debug); 16 | GlideWrapper.init(context); 17 | PicassoWrapper.init(context); 18 | } 19 | 20 | public static ImageFrameworkFactory getFramework(int frameworkType) { 21 | if (frameworkType == 1) { 22 | return new UILFraworkFactory(); 23 | } else if (frameworkType == 2) { 24 | return new GlideFrameworkFactory(); 25 | } else { 26 | return new PicassoFramworkFactory(); 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/PicassoWrapper.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | import android.content.Context; 4 | 5 | import com.squareup.picasso.Picasso; 6 | 7 | /** 8 | * Created by Administrator on 2016/3/1. 9 | */ 10 | public class PicassoWrapper { 11 | private static PicassoWrapper sDefaultInstance; 12 | private Context mContext; 13 | 14 | public PicassoWrapper(Context context) { 15 | mContext = context; 16 | } 17 | 18 | public static PicassoWrapper init(Context context) { 19 | if (sDefaultInstance == null) { 20 | sDefaultInstance = new PicassoWrapper(context); 21 | } 22 | return sDefaultInstance; 23 | } 24 | 25 | 26 | public static PicassoWrapper getDefalt() { 27 | if (sDefaultInstance == null) { 28 | throw new RuntimeException( 29 | "Must be call init(Context) befor!"); 30 | } 31 | return sDefaultInstance; 32 | } 33 | 34 | 35 | public Picasso getPicasso(){ 36 | Picasso.with(mContext).setIndicatorsEnabled(true); 37 | return Picasso.with(mContext); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/PicassoLoaderProduct.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | import android.widget.ImageView; 4 | 5 | /** 6 | * Created by Administrator on 2016/3/2. 7 | */ 8 | public class PicassoLoaderProduct implements ImageLoaderProduct { 9 | @Override 10 | public void display(String imageUri, ImageView imageView) { 11 | PicassoWrapper.getDefalt().getPicasso() 12 | .load(imageUri).into(imageView); 13 | } 14 | 15 | @Override 16 | public void display(String imageUrl, ImageView imageView, ImageConfigProduct config) { 17 | PicassoConfigProduct mConfig = (PicassoConfigProduct) config.get(); 18 | PicassoWrapper.getDefalt() 19 | .getPicasso() 20 | .load(imageUrl) 21 | .placeholder(mConfig.loadingRes) // can also be a drawable 22 | .error(mConfig.failRes) // will be displayed if the image cannot be loaded 23 | .into(imageView); 24 | } 25 | 26 | @Override 27 | public void cleanImageCache(String url) { 28 | PicassoWrapper.getDefalt() 29 | .getPicasso() 30 | .invalidate(url); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/GlideLoaderProduct.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | import android.widget.ImageView; 4 | 5 | /** 6 | * Created by Administrator on 2016/3/1. 7 | */ 8 | public class GlideLoaderProduct implements ImageLoaderProduct { 9 | @Override 10 | public void display(String imageUri, ImageView imageView) { 11 | GlideWrapper.getDefalt() 12 | .getGlide() 13 | .load(imageUri) 14 | .centerCrop() 15 | .into(imageView); 16 | } 17 | 18 | @Override 19 | public void display(String imageUrl, ImageView imageView, ImageConfigProduct config) { 20 | GlideConfigProduct mConfig = (GlideConfigProduct) config.get(); 21 | GlideWrapper.getDefalt() 22 | .getGlide() 23 | .load(imageUrl) 24 | .centerCrop() 25 | .placeholder(mConfig.loadingRes) // can also be a drawable 26 | .error(mConfig.failRes) // will be displayed if the image cannot be loaded 27 | .crossFade(mConfig.duration) 28 | .into(imageView); 29 | } 30 | 31 | @Override 32 | public void cleanImageCache(String url) { 33 | GlideWrapper.getDefalt().cleanCache(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/GlideWrapper.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | import android.content.Context; 4 | 5 | import com.bumptech.glide.Glide; 6 | import com.bumptech.glide.RequestManager; 7 | 8 | /** 9 | * Created by Administrator on 2016/3/1. 10 | */ 11 | public class GlideWrapper { 12 | private static GlideWrapper sDefaultInstance; 13 | private Context mContext; 14 | 15 | public GlideWrapper(Context context) { 16 | mContext = context; 17 | } 18 | 19 | public static GlideWrapper init(Context context) { 20 | if (sDefaultInstance == null) { 21 | sDefaultInstance = new GlideWrapper(context); 22 | } 23 | return sDefaultInstance; 24 | } 25 | 26 | 27 | public static GlideWrapper getDefalt() { 28 | if (sDefaultInstance == null) { 29 | throw new RuntimeException( 30 | "Must be call init(Context) befor!"); 31 | } 32 | return sDefaultInstance; 33 | } 34 | 35 | 36 | public RequestManager getGlide(){ 37 | return Glide.with(mContext); 38 | } 39 | 40 | public void cleanCache(){ 41 | // crash: method on a background thread 42 | // you should put it on threadpool 43 | new Thread(new Runnable() { 44 | @Override 45 | public void run() { 46 | Glide.get(mContext).clearMemory(); 47 | Glide.get(mContext).clearDiskCache(); 48 | } 49 | }); 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/GlideConfigProduct.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | /** 4 | * Created by Administrator on 2016/3/1. 5 | */ 6 | public class GlideConfigProduct implements ImageConfigProduct { 7 | //default true 8 | public boolean isSupportMemoryCache; 9 | public boolean isSupportDiskCache; 10 | public int defaulRes; 11 | public int loadingRes; 12 | public int failRes; 13 | public int duration; 14 | 15 | 16 | public GlideConfigProduct() { 17 | this.isSupportMemoryCache = true; 18 | this.isSupportDiskCache = true; 19 | this.loadingRes = 0; 20 | this.failRes = 0; 21 | this.duration = 0; 22 | } 23 | 24 | @Override 25 | public void setDefaulRes(int defaulRes) { 26 | this.defaulRes = defaulRes; 27 | } 28 | 29 | @Override 30 | public void setLoadingRes(int loadingRes) { 31 | this.loadingRes = loadingRes; 32 | } 33 | 34 | @Override 35 | public void setFailRes(int failRes) { 36 | this.failRes = failRes; 37 | } 38 | 39 | @Override 40 | public void setsupportMemoryCache(boolean flag) { 41 | this.isSupportMemoryCache = flag; 42 | } 43 | 44 | @Override 45 | public void setsupportDiskCache(boolean flag) { 46 | this.isSupportDiskCache = flag; 47 | } 48 | 49 | @Override 50 | public void setFadeIn(int duration) { 51 | this.duration = duration; 52 | } 53 | 54 | @Override 55 | public Object get() { 56 | return this; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/vankain/imageloaderfactory/PicassoConfigProduct.java: -------------------------------------------------------------------------------- 1 | package com.github.vankain.imageloaderfactory; 2 | 3 | /** 4 | * Created by Administrator on 2016/3/2. 5 | */ 6 | public class PicassoConfigProduct implements ImageConfigProduct { 7 | //default true 8 | public boolean isSupportMemoryCache; 9 | public boolean isSupportDiskCache; 10 | public int defaulRes; 11 | public int loadingRes; 12 | public int failRes; 13 | public int duration; 14 | 15 | 16 | public PicassoConfigProduct() { 17 | this.isSupportMemoryCache = true; 18 | this.isSupportDiskCache = true; 19 | this.loadingRes = 0; 20 | this.failRes = 0; 21 | this.duration = 0; 22 | } 23 | 24 | @Override 25 | public void setDefaulRes(int defaulRes) { 26 | this.defaulRes = defaulRes; 27 | } 28 | 29 | @Override 30 | public void setLoadingRes(int loadingRes) { 31 | this.loadingRes = loadingRes; 32 | } 33 | 34 | @Override 35 | public void setFailRes(int failRes) { 36 | this.failRes = failRes; 37 | } 38 | 39 | @Override 40 | public void setsupportMemoryCache(boolean flag) { 41 | this.isSupportMemoryCache = flag; 42 | } 43 | 44 | @Override 45 | public void setsupportDiskCache(boolean flag) { 46 | this.isSupportDiskCache = flag; 47 | } 48 | 49 | @Override 50 | public void setFadeIn(int duration) { 51 | this.duration = duration; 52 | } 53 | 54 | @Override 55 | public Object get() { 56 | return this; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 18 | 19 |