├── BaseImageLoader
├── build.gradle
├── consumer-rules.pro
├── jcenter.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── me
│ │ └── alex
│ │ └── baseimageloader
│ │ ├── BaseImageLoader.java
│ │ ├── config
│ │ ├── BaseImageConfig.java
│ │ ├── BaseImageSetting.java
│ │ └── Builder.java
│ │ ├── entity
│ │ └── ImageResult.java
│ │ ├── listener
│ │ ├── OnAsListener.java
│ │ ├── OnBitmapResult.java
│ │ ├── OnDrawableResult.java
│ │ ├── OnFileResult.java
│ │ ├── OnGifResult.java
│ │ └── OnLoadListener.java
│ │ ├── module
│ │ └── MyAppGlideModule.java
│ │ ├── observer
│ │ └── BaseLifeObserver.java
│ │ ├── srtategy
│ │ ├── BaseImageLoaderStrategy.java
│ │ └── CacheStrategy.java
│ │ ├── tools
│ │ ├── AutoLoadTools.java
│ │ └── Tools.java
│ │ ├── transform
│ │ ├── RoundAndCenterCropTransform.java
│ │ └── RoundedCornersTransform.java
│ │ └── view
│ │ └── BaseImageView.java
│ └── res
│ └── values
│ └── attrs.xml
├── README.md
├── app
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── assets
│ └── imgs.zip
│ ├── java
│ └── me
│ │ └── alex
│ │ └── baseimageloaderdemo
│ │ ├── App.java
│ │ ├── ImageConfig.java
│ │ ├── ImageData.java
│ │ ├── MainActivity.java
│ │ └── fragment
│ │ ├── AutoLoadFragment.java
│ │ ├── BaseImageViewFragment.java
│ │ ├── CustomFragment.java
│ │ └── ImageFragment.java
│ └── res
│ ├── drawable
│ ├── ic_baseline_adb_24.xml
│ └── ic_launcher_background.xml
│ ├── layout
│ ├── activity_main.xml
│ ├── fragment_autoload.xml
│ ├── fragment_baseimageview.xml
│ ├── fragment_custom.xml
│ └── fragment_image.xml
│ ├── mipmap-hdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
│ └── xml
│ └── network_security_config.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/BaseImageLoader/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.3"
6 |
7 | defaultConfig {
8 | minSdkVersion 19
9 | targetSdkVersion 29
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
14 | consumerProguardFiles "consumer-rules.pro"
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: "libs", include: ["*.jar"])
27 | api 'com.github.bumptech.glide:glide:4.11.0'
28 | annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
29 | }
30 | apply from: 'jcenter.gradle'
--------------------------------------------------------------------------------
/BaseImageLoader/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AlexFugui/BaseImageLoader/1422c4b2e91acb7295da56200a651de64f931803/BaseImageLoader/consumer-rules.pro
--------------------------------------------------------------------------------
/BaseImageLoader/jcenter.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.github.dcendents.android-maven'
2 | apply plugin: 'com.jfrog.bintray'
3 |
4 | def siteUrl = 'https://github.com/AlexFugui/BaseImageLoader' //项目在github主页地址
5 | def gitUrl = 'https://github.com/AlexFugui/BaseImageLoader.git' //Git仓库的地址
6 | group = "com.alex"
7 | version = "1.1.0"
8 |
9 |
10 | install {
11 | repositories.mavenInstaller {
12 | pom {
13 | project {
14 | packaging 'aar'//打包类型 aar文件
15 | name 'BaseImageLoader'// 在pom库中的包名
16 | description 'Android BaseImageLoader'// 可选,项目描述。
17 | url siteUrl // 项目主页
18 |
19 | licenses {
20 | license {
21 | name 'The Apache Software License, Version 2.0'
22 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
23 | }
24 | }
25 |
26 | developers {
27 | developer {
28 | id 'AlexFugui' // 开发者的id。
29 | name 'AlexFugui' // 开发者名字。
30 | email '329406095@qq.com' // 开发者邮箱。
31 | }
32 | }
33 |
34 | scm {
35 | connection gitUrl // Git仓库地址。
36 | developerConnection gitUrl // Git仓库地址。
37 | url siteUrl // 项目主页。
38 | }
39 | }
40 | }
41 | }
42 | }
43 |
44 | task sourcesJar(type: Jar) {
45 | from android.sourceSets.main.java.srcDirs
46 | getArchiveClassifier().convention("sources")
47 | getArchiveClassifier().set("sources")
48 | }
49 |
50 | task javadoc(type: Javadoc) {
51 | source = android.sourceSets.main.java.srcDirs
52 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
53 | failOnError false
54 | }
55 |
56 | task javadocJar(type: Jar, dependsOn: javadoc) {
57 | getArchiveClassifier().convention("javadoc")
58 | getArchiveClassifier().set("javadoc")
59 | from javadoc.destinationDir
60 | }
61 | artifacts {
62 | archives javadocJar
63 | archives sourcesJar
64 | }
65 |
66 | Properties properties = new Properties()
67 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
68 | bintray {
69 | user = properties.getProperty("bintray.user")
70 | key = properties.getProperty("bintray.apikey")
71 |
72 | configurations = ['archives']
73 | pkg {
74 | repo = "maven"
75 | name = "BaseImageLoader"//项目上传以后在名为maven的库中的包名
76 | userOrg = 'alexfugui'//Bintray的组织中 id
77 | websiteUrl = siteUrl
78 | vcsUrl = gitUrl
79 | licenses = ["Apache-2.0"]
80 | publish = true // 是否是公开项目。
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/BaseImageLoader/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
54 | * Created by Alex on 2020/12/4 0004 55 | *
56 | * 页面内容介绍: 可自行实现 {@link BaseImageLoaderStrategy} 和 {@link BaseImageConfig} 替换现有策略 57 | *
58 | * ================================================
59 | */
60 | public class BaseImageLoader implements BaseImageLoaderStrategy
15 | * Created by Alex on 2020/12/4 0004
16 | *
17 | * 页面内容介绍:
18 | * 这里是图片加载配置信息的基类,定义一些所有图片加载框架都可以用的通用参数
19 | * 每个 {@link BaseImageLoaderStrategy} 应该对应一个 {@link BaseImageConfig} 实现类
20 | *
21 | * ================================================
22 | */
23 | public class BaseImageConfig {
24 |
25 | public BaseImageConfig() {
26 |
27 | }
28 |
29 | /**
30 | * 网络图片url
31 | */
32 | protected Object url;
33 |
34 | /**
35 | * 需要显示图片的View
36 | */
37 | protected View imageView;
38 |
39 | /**
40 | * 加载网络图片之前占位图片
41 | */
42 | protected int placeholder;
43 |
44 | /**
45 | * 错误占位图片
46 | */
47 | protected int errorPic;
48 |
49 | /**
50 | * 是否使用淡入淡出过渡动画
51 | */
52 | protected boolean isCrossFade;
53 |
54 | /**
55 | * 是否将图片剪切为 CenterCrop
56 | */
57 | protected boolean isCenterCrop;
58 |
59 | /**
60 | * 是否将图片剪切为圆形
61 | */
62 | protected boolean isCircle;
63 |
64 | /**
65 | * 左上 圆角
66 | */
67 | protected float topRightRadius;
68 |
69 | /**
70 | * 右上 圆角
71 | */
72 | protected float topLeftRadius;
73 |
74 | /**
75 | * 左下 圆角
76 | */
77 | protected float bottomRightRadius;
78 |
79 | /**
80 | * 右下 圆角
81 | */
82 | protected float bottomLeftRadius;
83 |
84 | /**
85 | * 通用圆角
86 | */
87 | protected float radius;
88 |
89 | /**
90 | * 是否以bitmap加载
91 | */
92 | protected boolean asBitmap;
93 |
94 | /**
95 | * 缓存类型 默认为通用配置中的模式
96 | */
97 | private @CacheStrategy.Strategy
98 | int cacheStrategy = BaseImageSetting.getInstance().getCacheStrategy();
99 |
100 | /**
101 | * 加载监听
102 | */
103 | protected OnLoadListener listener;
104 |
105 | /**
106 | * 是否清除内存中缓存
107 | */
108 | protected boolean clearMemory;
109 |
110 | /**
111 | * 是否清除储存中缓存
112 | */
113 | protected boolean clearDiskCache;
114 |
115 | public BaseImageConfig(Builder builder) {
116 | this.url = builder.url;
117 | this.imageView = builder.imageView;
118 | this.placeholder = builder.placeholder;
119 | this.errorPic = builder.errorPic;
120 | this.isCrossFade = builder.isCrossFade;
121 | this.isCenterCrop = builder.isCenterCrop;
122 | this.isCircle = builder.isCircle;
123 | this.topRightRadius = builder.topRightRadius;
124 | this.topLeftRadius = builder.topLeftRadius;
125 | this.bottomRightRadius = builder.bottomRightRadius;
126 | this.bottomLeftRadius = builder.bottomLeftRadius;
127 | this.radius = builder.radius;
128 | this.asBitmap = builder.asBitmap;
129 | this.listener = builder.listener;
130 | this.clearMemory = builder.clearMemory;
131 | this.clearDiskCache = builder.clearDiskCache;
132 | }
133 |
134 |
135 | public Object getUrl() {
136 | return url;
137 | }
138 |
139 | public View getImageView() {
140 | return imageView;
141 | }
142 |
143 | public int getPlaceholder() {
144 | return placeholder;
145 | }
146 |
147 | public int getErrorPic() {
148 | return errorPic;
149 | }
150 |
151 | public boolean isCrossFade() {
152 | return isCrossFade;
153 | }
154 |
155 | public boolean isCenterCrop() {
156 | return isCenterCrop;
157 | }
158 |
159 | public boolean isCircle() {
160 | return isCircle;
161 | }
162 |
163 | public float getTopRightRadius() {
164 | return topRightRadius;
165 | }
166 |
167 | public float getTopLeftRadius() {
168 | return topLeftRadius;
169 | }
170 |
171 | public float getBottomRightRadius() {
172 | return bottomRightRadius;
173 | }
174 |
175 | public float getBottomLeftRadius() {
176 | return bottomLeftRadius;
177 | }
178 |
179 | public float getRadius() {
180 | return radius;
181 | }
182 |
183 | public void setUrl(Object url) {
184 | this.url = url;
185 | }
186 |
187 | public void setImageView(ImageView imageView) {
188 | this.imageView = imageView;
189 | }
190 |
191 | public void setPlaceholder(int placeholder) {
192 | this.placeholder = placeholder;
193 | }
194 |
195 | public void setErrorPic(int errorPic) {
196 | this.errorPic = errorPic;
197 | }
198 |
199 | public void setCrossFade(boolean crossFade) {
200 | isCrossFade = crossFade;
201 | }
202 |
203 | public void setCenterCrop() {
204 | isCenterCrop = true;
205 | }
206 |
207 | public void setCircle() {
208 | isCircle = true;
209 | }
210 |
211 | public void setTopRightRadius(float topRightRadius) {
212 | this.topRightRadius = topRightRadius;
213 | }
214 |
215 | public void setTopLeftRadius(float topLeftRadius) {
216 | this.topLeftRadius = topLeftRadius;
217 | }
218 |
219 | public void setBottomRightRadius(float bottomRightRadius) {
220 | this.bottomRightRadius = bottomRightRadius;
221 | }
222 |
223 | public void setBottomLeftRadius(float bottomLeftRadius) {
224 | this.bottomLeftRadius = bottomLeftRadius;
225 | }
226 |
227 | public void setRadius(float radius) {
228 | this.radius = radius;
229 | }
230 |
231 | public OnLoadListener getListener() {
232 | return listener;
233 | }
234 |
235 | public void setListener(OnLoadListener listener) {
236 | this.listener = listener;
237 | }
238 |
239 | public boolean getAsBitmap() {
240 | return asBitmap;
241 | }
242 |
243 | public void asBitmap(boolean asBitmap) {
244 | this.asBitmap = asBitmap;
245 | }
246 |
247 | public @CacheStrategy.Strategy
248 | int getCacheStrategy() {
249 | return cacheStrategy;
250 | }
251 |
252 | public void setCacheStrategy(int cacheStrategy) {
253 | this.cacheStrategy = cacheStrategy;
254 | }
255 |
256 | public boolean isClearMemory() {
257 | return clearMemory;
258 | }
259 |
260 | public boolean isClearDiskCache() {
261 | return clearDiskCache;
262 | }
263 |
264 | public static Builder builder() {
265 | return new Builder();
266 | }
267 |
268 | }
269 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/config/BaseImageSetting.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.config;
2 |
3 |
4 | import android.util.Log;
5 |
6 | import me.alex.baseimageloader.srtategy.CacheStrategy;
7 |
8 | /**
9 | * ================================================
10 | * Description:
11 | *
12 | * Created by Alex on 2020/12/5
13 | *
14 | * 页面内容介绍: BaseImage通用设置
15 | *
16 | * ================================================
17 | */
18 | public class BaseImageSetting {
19 | private static BaseImageSetting baseImageSetting = new BaseImageSetting();
20 |
21 | /**
22 | * 加载中图片
23 | */
24 | private int placeholder;
25 | /**
26 | * 加载错误图片
27 | */
28 | protected int errorPic;
29 | /**
30 | * 默认内存缓存大小 20mb
31 | */
32 | private int memoryCacheSize = 20;
33 | /**
34 | * 默认bitmap池缓存大小 30mb
35 | */
36 | private int bitmapPoolSize = 30;
37 | /**
38 | * 默认硬盘缓存大小250mb
39 | */
40 | private int diskCacheSize = 250;
41 | /**
42 | * 默认的缓存文件夹名称
43 | */
44 | private String cacheFileName = "BaseImageLoaderCache";
45 | /**
46 | * 通用缓存类型设置
47 | */
48 | private @CacheStrategy.Strategy
49 | int cacheStrategy = CacheStrategy.AUTOMATIC;
50 |
51 | /**
52 | * log级别设置,默认为ERROR级别
53 | */
54 | private int logLevel = Log.ERROR;
55 |
56 | /**
57 | * 自动加载图片缓存数量最大值
58 | */
59 | private int cacheSize = 50;
60 |
61 |
62 | public static BaseImageSetting getInstance() {
63 | return baseImageSetting;
64 | }
65 |
66 | public int getMemoryCacheSize() {
67 | return memoryCacheSize;
68 | }
69 |
70 | /**
71 | * 设置内存缓存大小 默认20mb
72 | *
73 | * @param memoryCacheSize 单位mb
74 | * @return 返回SImageConfig类可以连续设置
75 | */
76 | public BaseImageSetting setMemoryCacheSize(int memoryCacheSize) {
77 | this.memoryCacheSize = memoryCacheSize;
78 | return this;
79 | }
80 |
81 | public int getBitmapPoolSize() {
82 | return bitmapPoolSize;
83 | }
84 |
85 | /**
86 | * 设置bitMap池缓存大小 默认30mb
87 | *
88 | * @param bitmapPoolSize 单位mb
89 | * @return 返回SImageConfig类可以连续设置
90 | */
91 | public BaseImageSetting setBitmapPoolSize(int bitmapPoolSize) {
92 | this.bitmapPoolSize = bitmapPoolSize;
93 | return this;
94 | }
95 |
96 | public int getDiskCacheSize() {
97 | return diskCacheSize;
98 | }
99 |
100 | /**
101 | * 设置硬盘缓存大小 默认250mb
102 | *
103 | * @param diskCacheSize 单位mb
104 | * @return 返回SImageConfig类可以连续设置
105 | */
106 | public BaseImageSetting setDiskCacheSize(int diskCacheSize) {
107 | this.diskCacheSize = diskCacheSize;
108 | return this;
109 | }
110 |
111 | public String getCacheFileName() {
112 | return cacheFileName;
113 | }
114 |
115 | public BaseImageSetting setCacheFileName(String cacheFileName) {
116 | this.cacheFileName = cacheFileName;
117 | return this;
118 | }
119 |
120 | public BaseImageSetting setCacheStrategy(@CacheStrategy.Strategy int cacheStrategy) {
121 | this.cacheStrategy = cacheStrategy;
122 | return this;
123 | }
124 |
125 | public @CacheStrategy.Strategy
126 | int getCacheStrategy() {
127 | return cacheStrategy;
128 | }
129 |
130 | public int getLogLevel() {
131 | return logLevel;
132 | }
133 |
134 | /**
135 | * 如果你拥有设备的访问权限,你可以使用 adb logcat 或你的 IDE 查看一些日志。
136 | * 你可以使用 adb shell setprop log.tag.
13 | * Created by Alex on 2020/12/15
14 | *
15 | * 页面内容介绍:
16 | *
17 | * ================================================
18 | */
19 | public class Builder {
20 |
21 | public Builder() {
22 |
23 | }
24 |
25 | protected Object url;
26 | protected View imageView;
27 | protected int placeholder = BaseImageSetting.getInstance().getPlaceholder();
28 | protected int errorPic = BaseImageSetting.getInstance().getErrorPic();
29 | protected boolean isCrossFade = false;
30 | protected boolean isCenterCrop = false;
31 | protected boolean isCircle = false;
32 | protected int topRightRadius = 0;
33 | protected int topLeftRadius = 0;
34 | protected int bottomRightRadius = 0;
35 | protected int bottomLeftRadius = 0;
36 | protected int radius = 0;
37 | protected boolean asBitmap;
38 | protected @CacheStrategy.Strategy
39 | int cacheStrategy;//0对应DiskCacheStrategy.all,1对应DiskCacheStrategy.NONE,2对应DiskCacheStrategy.SOURCE,3对应DiskCacheStrategy.RESULT
40 | protected OnLoadListener listener;
41 | protected boolean clearMemory;
42 | protected boolean clearDiskCache;
43 |
44 | public Builder url(Object url) {
45 | this.url = url;
46 | return this;
47 | }
48 |
49 | public Builder imageView(View imageView) {
50 | this.imageView = imageView;
51 | return this;
52 | }
53 |
54 | public Builder placeholder(int placeholder) {
55 | this.placeholder = placeholder;
56 | return this;
57 | }
58 |
59 | public Builder errorPic(int errorPic) {
60 | this.errorPic = errorPic;
61 | return this;
62 | }
63 |
64 | public Builder crossFade(boolean crossFade) {
65 | isCrossFade = crossFade;
66 | return this;
67 | }
68 |
69 | public Builder centerCrop() {
70 | isCenterCrop = true;
71 | return this;
72 | }
73 |
74 | public Builder isCircle() {
75 | isCircle = true;
76 | return this;
77 | }
78 |
79 | public Builder setTopRightRadius(int topRightRadius) {
80 | this.topRightRadius = topRightRadius;
81 | return this;
82 | }
83 |
84 | public Builder setTopLeftRadius(int topLeftRadius) {
85 | this.topLeftRadius = topLeftRadius;
86 | return this;
87 | }
88 |
89 | public Builder setBottomRightRadius(int bottomRightRadius) {
90 | this.bottomRightRadius = bottomRightRadius;
91 | return this;
92 | }
93 |
94 | public Builder setBottomLeftRadius(int bottomLeftRadius) {
95 | this.bottomLeftRadius = bottomLeftRadius;
96 | return this;
97 | }
98 |
99 | public Builder setRadius(int radius) {
100 | this.radius = radius;
101 | return this;
102 | }
103 |
104 | public OnLoadListener getListener() {
105 | return listener;
106 | }
107 |
108 | public Builder setListener(OnLoadListener listener) {
109 | this.listener = listener;
110 | return this;
111 | }
112 |
113 | public boolean isAsBitmap() {
114 | return asBitmap;
115 | }
116 |
117 | public Builder setAsBitmap(boolean asBitmap) {
118 | this.asBitmap = asBitmap;
119 | return this;
120 | }
121 |
122 |
123 | public Builder cacheStrategy(@CacheStrategy.Strategy int cacheStrategy) {
124 | this.cacheStrategy = cacheStrategy;
125 | return this;
126 | }
127 |
128 | public Builder clearMemory() {
129 | this.clearMemory = true;
130 | return this;
131 | }
132 |
133 | public Builder clearDiskCache() {
134 | this.clearDiskCache = true;
135 | return this;
136 | }
137 |
138 | public BaseImageConfig show() {
139 | return new BaseImageConfig(this);
140 | }
141 | }
142 |
143 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/entity/ImageResult.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.entity;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.drawable.Drawable;
5 |
6 | import com.bumptech.glide.load.resource.gif.GifDrawable;
7 |
8 | import java.io.File;
9 |
10 | /**
11 | * ================================================
12 | * Description:
13 | *
14 | * Created by Alex on 2020/12/8 0008
15 | *
16 | * 页面内容介绍:
17 | *
18 | * ================================================
19 | */
20 | public class ImageResult {
21 | private Bitmap bitmap;
22 | private File file;
23 | private Drawable drawable;
24 | private GifDrawable gif;
25 |
26 | public ImageResult(Bitmap bitmap) {
27 | this.bitmap = bitmap;
28 | }
29 |
30 | public ImageResult(File file) {
31 | this.file = file;
32 | }
33 |
34 | public ImageResult(Drawable drawable) {
35 | this.drawable = drawable;
36 | }
37 |
38 | public ImageResult(GifDrawable gif) {
39 | this.gif = gif;
40 | }
41 |
42 | public Bitmap getBitmap() {
43 | return bitmap;
44 | }
45 |
46 |
47 | public File getFile() {
48 | return file;
49 | }
50 |
51 |
52 | public Drawable getDrawable() {
53 | return drawable;
54 | }
55 |
56 |
57 | public GifDrawable getGif() {
58 | return gif;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/listener/OnAsListener.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.listener;
2 |
3 |
4 | import me.alex.baseimageloader.entity.ImageResult;
5 |
6 | public interface OnAsListener {
7 | void OnResult(ImageResult result);
8 | }
9 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/listener/OnBitmapResult.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.listener;
2 |
3 | public interface OnBitmapResult extends OnAsListener {
4 | }
5 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/listener/OnDrawableResult.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.listener;
2 |
3 |
4 | public interface OnDrawableResult extends OnAsListener {
5 | }
6 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/listener/OnFileResult.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.listener;
2 |
3 |
4 | public interface OnFileResult extends OnAsListener {
5 | }
6 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/listener/OnGifResult.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.listener;
2 |
3 |
4 | public interface OnGifResult extends OnAsListener {
5 | }
6 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/listener/OnLoadListener.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.listener;
2 |
3 | import android.graphics.drawable.Drawable;
4 |
5 | import androidx.annotation.Nullable;
6 |
7 | import com.bumptech.glide.load.DataSource;
8 | import com.bumptech.glide.load.engine.GlideException;
9 | import com.bumptech.glide.request.target.Target;
10 |
11 | /**
12 | * ================================================
13 | * Description:
14 | *
15 | * Created by Alex on 2020/12/8 0008
16 | *
17 | * 页面内容介绍: 默认加载结果监听,参数文档参考GlideV4
18 | *
19 | * ================================================
20 | */
21 | public interface OnLoadListener {
22 | /**
23 | * 加载成功结果回调监听
24 | *
25 | * @param resource
26 | * @param model
27 | * @param target
28 | * @param dataSource
29 | * @param isFirstResource
30 | */
31 | void onResourceReady(Drawable resource, Object model, Target
22 | * Created by Alex on 2020/12/2 0002
23 | *
24 | * 页面内容介绍: 生成GlideAlex的自定义Module
25 | *
26 | * ================================================
27 | */
28 | @GlideModule(glideName = "GlideAlex")
29 | public class MyAppGlideModule extends AppGlideModule {
30 |
31 | @Override
32 | public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
33 | builder.setLogLevel(Log.ERROR);
34 |
35 | //内存缓存 20mb
36 | int memoryCacheSizeBytes = 1024 * 1024 * BaseImageSetting.getInstance().getMemoryCacheSize(); // 默认20mb
37 | builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));
38 |
39 | //bitmap池缓存 30mb
40 | int bitmapPoolSizeBytes = 1024 * 1024 * BaseImageSetting.getInstance().getBitmapPoolSize(); // 默认30mb
41 | builder.setBitmapPool(new LruBitmapPool(bitmapPoolSizeBytes));
42 |
43 | //磁盘缓存250mb
44 | int diskCacheSizeBytes = 1024 * 1024 * BaseImageSetting.getInstance().getDiskCacheSize(); //默认缓存文件夹名 GlideAlexCache 缓存大小默认150mb
45 | builder.setDiskCache(new InternalCacheDiskCacheFactory(context, BaseImageSetting.getInstance().getCacheFileName(), diskCacheSizeBytes));
46 |
47 | // builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565));
48 | // builder.setDefaultRequestOptions(
49 | // new RequestOptions()
50 | // .format(DecodeFormat.PREFER_RGB_565)
51 | // .disallowHardwareConfig()
52 | // .disallowHardwareBitmaps());
53 | }
54 | }
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/observer/BaseLifeObserver.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.observer;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 |
6 | import androidx.lifecycle.Lifecycle;
7 | import androidx.lifecycle.LifecycleObserver;
8 | import androidx.lifecycle.OnLifecycleEvent;
9 |
10 | /**
11 | * ================================================
12 | * Description:
13 | *
14 | * Created by Alex on 2020/12/10
15 | *
16 | * 页面内容介绍:
17 | *
18 | * ================================================
19 | */
20 | public class BaseLifeObserver implements LifecycleObserver {
21 |
22 | private Context context;
23 |
24 | public BaseLifeObserver(Context context) {
25 | this.context = context;
26 | }
27 |
28 | @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
29 | void onCreate() {
30 | showLog("onCreate");
31 | }
32 |
33 | @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
34 | void onDestroy() {
35 | showLog("onDestroy");
36 | }
37 |
38 | @OnLifecycleEvent(Lifecycle.Event.ON_START)
39 | void onStart() {
40 | showLog("onStart");
41 | }
42 |
43 | @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
44 | void onStop() {
45 | showLog("onStop");
46 | }
47 |
48 | @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
49 | void onResume() {
50 | showLog("onResume");
51 | }
52 |
53 | @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
54 | void onPause() {
55 | showLog("onPause");
56 | }
57 |
58 | private void showLog(String msg) {
59 | Log.e("===BaseLifeObserver", msg);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/srtategy/BaseImageLoaderStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017 JessYan
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 | package me.alex.baseimageloader.srtategy;
17 |
18 | import android.content.Context;
19 | import android.view.ViewGroup;
20 | import android.widget.ImageView;
21 |
22 | import androidx.annotation.LayoutRes;
23 | import androidx.annotation.NonNull;
24 | import androidx.annotation.Nullable;
25 |
26 | import me.alex.baseimageloader.config.BaseImageConfig;
27 | import me.alex.baseimageloader.listener.OnAsListener;
28 |
29 |
30 | /**
31 | * ================================================
32 | * Description:
33 | *
34 | * Created by Alex on 2020/12/2 0002
35 | *
36 | * 页面内容介绍:
37 | * 图片加载策略,实现 {@link BaseImageLoaderStrategy}
38 | * 并通过 {@link BaseImageLoaderStrategy} 配置后,才可进行图片请求
39 | *
40 | * ================================================
41 | */
42 |
43 | public interface BaseImageLoaderStrategy
15 | * Created by Alex on 2020/12/2 0002
16 | *
17 | * 页面内容介绍:
18 | * Glide缓存方式 {@link com.bumptech.glide.load.engine.DiskCacheStrategy}
19 | * 0对应DiskCacheStrategy.ALL,
20 | * 1对应DiskCacheStrategy.NONE,
21 | * 2对应DiskCacheStrategy.SOURCE,
22 | * 3对应DiskCacheStrategy.RESULT,
23 | * 4对应DiskCacheStrategy.AUTOMATIC
24 | * 不配置{@link BaseImageConfig}中的缓存策略则默认为 ALL
25 | * 如配置了项目统一配置,则单独设置{@link BaseImageConfig}有更高的优先级
26 | *
27 | * ================================================
28 | */
29 |
30 | public interface CacheStrategy {
31 |
32 | int ALL = 0;
33 |
34 | int NONE = 1;
35 |
36 | int RESOURCE = 2;
37 |
38 | int DATA = 3;
39 |
40 | int AUTOMATIC = 4;
41 |
42 | @IntDef({ALL, NONE, RESOURCE, DATA, AUTOMATIC})
43 | @Retention(RetentionPolicy.SOURCE)
44 | @interface Strategy {
45 | }
46 |
47 | }
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/tools/AutoLoadTools.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.tools;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.drawable.BitmapDrawable;
5 | import android.util.Log;
6 |
7 | import java.io.BufferedInputStream;
8 | import java.io.File;
9 | import java.io.FileInputStream;
10 | import java.io.FileNotFoundException;
11 | import java.io.IOException;
12 | import java.io.InputStream;
13 | import java.util.LinkedHashMap;
14 | import java.util.Map;
15 | import java.util.zip.ZipEntry;
16 | import java.util.zip.ZipException;
17 | import java.util.zip.ZipFile;
18 | import java.util.zip.ZipInputStream;
19 |
20 | import me.alex.baseimageloader.BaseImageLoader;
21 | import me.alex.baseimageloader.config.BaseImageSetting;
22 |
23 | /**
24 | * ================================================
25 | * Description:
26 | *
27 | * Created by Alex on 2020/12/16
28 | *
29 | * 页面内容介绍:
30 | *
31 | * ================================================
32 | */
33 | public class AutoLoadTools {
34 | private int cacheSize = BaseImageSetting.getInstance().getCacheSize();
35 |
36 | private static AutoLoadTools instance = new AutoLoadTools();
37 |
38 | public static AutoLoadTools getInstance() {
39 | if (instance == null) {
40 | instance = new AutoLoadTools();
41 | }
42 | return instance;
43 | }
44 |
45 | public AutoLoadTools() {
46 |
47 | }
48 |
49 | private Map
22 | * Created by Alex on 2020/12/10
23 | *
24 | * 页面内容介绍:
25 | *
26 | * ================================================
27 | */
28 | public class Tools {
29 | /**
30 | * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
31 | */
32 | public static int dp2px(Context context, float dpValue) {
33 | final float scale = context.getResources().getDisplayMetrics().density;
34 | return (int) (dpValue * scale + 0.5f);
35 | }
36 |
37 | /**
38 | * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
39 | */
40 | public static int px2dp(Context context, int pxValue) {
41 | final float scale = context.getResources().getDisplayMetrics().density;
42 | return (int) (pxValue / scale + 0.5f);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/transform/RoundAndCenterCropTransform.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.transform;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapShader;
5 | import android.graphics.Canvas;
6 | import android.graphics.Paint;
7 | import android.graphics.RectF;
8 |
9 | import androidx.annotation.NonNull;
10 |
11 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
12 | import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
13 | import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
14 |
15 | import java.nio.ByteBuffer;
16 | import java.security.MessageDigest;
17 | import java.util.Arrays;
18 |
19 | /**
20 | * ================================================
21 | * Description:
22 | *
23 | * Created by Alex on 2020/12/4
24 | *
25 | * 页面内容介绍: 同时实现CenterCrop和自定义4个圆角效果
26 | *
27 | * ================================================
28 | */
29 | public class RoundAndCenterCropTransform extends BitmapTransformation {
30 | private static final String ID = RoundAndCenterCropTransform.class.getName();
31 | private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
32 | private boolean isCenterCrop = false;
33 | private Float[] radiusList = new Float[4];
34 |
35 | public RoundAndCenterCropTransform(boolean isCenterCrop, float topRight, float topLeft, float bottomRight, float bottomLeft) {
36 | this.isCenterCrop = isCenterCrop;
37 | radiusList[0] = topRight;
38 | radiusList[1] = topLeft;
39 | radiusList[2] = bottomRight;
40 | radiusList[3] = bottomLeft;
41 | }
42 |
43 | @Override
44 | protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
45 | if (isCenterCrop) {
46 | Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
47 | return roundCrop(pool, bitmap);
48 | } else {
49 | return roundCrop(pool, toTransform);
50 | }
51 | }
52 |
53 | private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
54 | if (source == null) return null;
55 |
56 | Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
57 |
58 | Canvas canvas = new Canvas(result);
59 | Paint paint = new Paint();
60 | paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
61 | paint.setAntiAlias(true);
62 | RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
63 | // 左上
64 | canvas.save();
65 | canvas.clipRect(0, 0, canvas.getWidth() / 2, canvas.getHeight() / 2);
66 | canvas.drawRoundRect(rectF, radiusList[0], radiusList[0], paint);
67 | canvas.restore();
68 | // 右上
69 | canvas.save();
70 | canvas.clipRect(canvas.getWidth() / 2, 0, canvas.getWidth(), canvas.getHeight() / 2);
71 | canvas.drawRoundRect(rectF, radiusList[1], radiusList[1], paint);
72 | canvas.restore();
73 | // 右下
74 | canvas.save();
75 | canvas.clipRect(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getWidth(), canvas.getHeight());
76 | canvas.drawRoundRect(rectF, radiusList[2], radiusList[2], paint);
77 | canvas.restore();
78 | // 左下
79 | canvas.save();
80 | canvas.clipRect(0, canvas.getHeight() / 2, canvas.getWidth() / 2, canvas.getHeight());
81 | canvas.drawRoundRect(rectF, radiusList[3], radiusList[3], paint);
82 | canvas.restore();
83 | return result;
84 | }
85 |
86 | @Override
87 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
88 | messageDigest.update(ID.getBytes(CHARSET));
89 | byte[] radiusData = ByteBuffer.allocate(4).putInt(Arrays.hashCode(radiusList)).array();
90 | messageDigest.update(radiusData);
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/transform/RoundedCornersTransform.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.transform;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapShader;
5 | import android.graphics.Canvas;
6 | import android.graphics.Paint;
7 | import android.graphics.RectF;
8 | import android.graphics.Shader;
9 |
10 | import androidx.annotation.NonNull;
11 |
12 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
13 | import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
14 |
15 | import java.nio.ByteBuffer;
16 | import java.security.MessageDigest;
17 | import java.util.Arrays;
18 |
19 | /**
20 | * ================================================
21 | * Description:
22 | *
23 | * Created by Alex on 2020/12/4
24 | *
25 | * 页面内容介绍: 单独控制4个圆角效果 与 centerCrop不兼容
26 | *
27 | * ================================================
28 | */
29 | public class RoundedCornersTransform extends BitmapTransformation {
30 |
31 | private static final String ID = RoundedCornersTransform.class.getName();
32 | private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
33 |
34 | private Float[] radiusList = new Float[4];
35 | private boolean isCenterCrop = false;
36 |
37 | /**
38 | * 默认构造方法
39 | *
40 | * @param topRight 左上圆角
41 | * @param topLeft 右上圆角
42 | * @param bottomRight 左下圆角
43 | * @param bottomLeft 右下圆角
44 | */
45 | public RoundedCornersTransform(float topRight, float topLeft, float bottomRight, float bottomLeft) {
46 | radiusList[0] = topRight;
47 | radiusList[1] = topLeft;
48 | radiusList[2] = bottomRight;
49 | radiusList[3] = bottomLeft;
50 | }
51 |
52 | @Override
53 | protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
54 | //获取加载的图片
55 | Bitmap bitmap = pool.get(toTransform.getWidth(), toTransform.getHeight(), Bitmap.Config.ARGB_8888);
56 | Canvas canvas = new Canvas(bitmap);
57 | Paint paint = new Paint();
58 | //关联画笔绘制的原图bitmap
59 | BitmapShader shader = new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
60 | paint.setShader(shader);
61 | paint.setAntiAlias(true);
62 | RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
63 | // 左上
64 | canvas.save();
65 | canvas.clipRect(0, 0, canvas.getWidth() / 2, canvas.getHeight() / 2);
66 | canvas.drawRoundRect(rectF, radiusList[0], radiusList[0], paint);
67 | canvas.restore();
68 | // 右上
69 | canvas.save();
70 | canvas.clipRect(canvas.getWidth() / 2, 0, canvas.getWidth(), canvas.getHeight() / 2);
71 | canvas.drawRoundRect(rectF, radiusList[1], radiusList[1], paint);
72 | canvas.restore();
73 | // 右下
74 | canvas.save();
75 | canvas.clipRect(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getWidth(), canvas.getHeight());
76 | canvas.drawRoundRect(rectF, radiusList[2], radiusList[2], paint);
77 | canvas.restore();
78 | // 左下
79 | canvas.save();
80 | canvas.clipRect(0, canvas.getHeight() / 2, canvas.getWidth() / 2, canvas.getHeight());
81 | canvas.drawRoundRect(rectF, radiusList[3], radiusList[3], paint);
82 | canvas.restore();
83 | return bitmap;
84 | }
85 |
86 |
87 | @Override
88 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
89 | messageDigest.update(ID.getBytes(CHARSET));
90 | byte[] radiusData = ByteBuffer.allocate(4).putInt(Arrays.hashCode(radiusList)).array();
91 | messageDigest.update(radiusData);
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/java/me/alex/baseimageloader/view/BaseImageView.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloader.view;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.Context;
5 | import android.content.res.TypedArray;
6 | import android.util.AttributeSet;
7 | import android.widget.ImageView;
8 |
9 | import androidx.annotation.NonNull;
10 | import androidx.annotation.Nullable;
11 |
12 | import me.alex.baseimageloader.BaseImageLoader;
13 | import me.alex.baseimageloader.R;
14 | import me.alex.baseimageloader.config.BaseImageConfig;
15 | import me.alex.baseimageloader.config.BaseImageSetting;
16 | import me.alex.baseimageloader.srtategy.CacheStrategy;
17 | import me.alex.baseimageloader.tools.Tools;
18 |
19 |
20 | /**
21 | * ================================================
22 | * Description:
23 | *
24 | * Created by Alex on 2020/12/10
25 | *
26 | * 页面内容介绍:
27 | *
28 | * ================================================
29 | */
30 | @SuppressLint("AppCompatCustomView")
31 | public class BaseImageView extends ImageView {
32 | private Context context;
33 | private BaseImageLoader loader;
34 | private BaseImageConfig config = new BaseImageConfig();
35 | /**
36 | * 图片资源指向
37 | */
38 | protected Object url;
39 | /**
40 | * 加载网络图片之前占位图片
41 | */
42 | protected int placeholder;
43 |
44 | /**
45 | * 错误占位图片
46 | */
47 | protected int errorPic;
48 |
49 | /**
50 | * 是否使用淡入淡出过渡动画
51 | */
52 | protected boolean isCrossFade;
53 |
54 | /**
55 | * 是否将图片剪切为 CenterCrop
56 | */
57 | protected boolean isCenterCrop;
58 |
59 | /**
60 | * 是否将图片剪切为圆形
61 | */
62 | protected boolean isCircle;
63 |
64 | /**
65 | * 左上 圆角
66 | */
67 | protected int topRightRadius;
68 |
69 | /**
70 | * 右上 圆角
71 | */
72 | protected int topLeftRadius;
73 |
74 | /**
75 | * 左下 圆角
76 | */
77 | protected int bottomRightRadius;
78 |
79 | /**
80 | * 右下 圆角
81 | */
82 | protected int bottomLeftRadius;
83 |
84 | /**
85 | * 通用圆角
86 | */
87 | protected int radius;
88 |
89 | /**
90 | * 是否以bitmap加载
91 | */
92 | protected boolean asBitmap;
93 |
94 | /**
95 | * 缓存类型 默认为通用配置中的模式
96 | */
97 | private @CacheStrategy.Strategy
98 | int cacheStrategy;
99 |
100 | public BaseImageView(@NonNull Context context) {
101 | super(context);
102 | }
103 |
104 | public BaseImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
105 | super(context, attrs);
106 | init(context, attrs);
107 | }
108 |
109 | public BaseImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
110 | super(context, attrs, defStyleAttr);
111 | }
112 |
113 | private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
114 | this.context = context;
115 | loader = new BaseImageLoader();
116 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseImageView, 0, 0);
117 | //资源指向
118 | url = typedArray.getString(R.styleable.BaseImageView_url);
119 | //占位图
120 | placeholder = typedArray.getResourceId(R.styleable.BaseImageView_placeholder, 0);
121 | //错误图
122 | errorPic = typedArray.getResourceId(R.styleable.BaseImageView_errorPic, 0);
123 | //是否淡出淡入
124 | isCrossFade = typedArray.getBoolean(R.styleable.BaseImageView_isCrossFade, false);
125 | //centerCrop
126 | isCenterCrop = typedArray.getBoolean(R.styleable.BaseImageView_isCenterCrop, false);
127 | //圆形裁剪
128 | isCircle = typedArray.getBoolean(R.styleable.BaseImageView_isCircle, false);
129 |
130 | //左上 圆角
131 | topRightRadius = Tools.px2dp(context, typedArray.getDimensionPixelSize(R.styleable.BaseImageView_topRightRadius, 0));
132 |
133 | //右上 圆角
134 | topLeftRadius = Tools.px2dp(context, typedArray.getDimensionPixelSize(R.styleable.BaseImageView_topLeftRadius, 0));
135 |
136 | //左下 圆角
137 | bottomRightRadius = Tools.px2dp(context, typedArray.getDimensionPixelSize(R.styleable.BaseImageView_bottomRightRadius, 0));
138 |
139 | //右下 圆角
140 | bottomLeftRadius = Tools.px2dp(context, typedArray.getDimensionPixelSize(R.styleable.BaseImageView_bottomLeftRadius, 0));
141 |
142 | //通用圆角
143 | radius = Tools.px2dp(context, typedArray.getDimensionPixelSize(R.styleable.BaseImageView_radius, 0));
144 |
145 | //bitmap格式加载
146 | asBitmap = typedArray.getBoolean(R.styleable.BaseImageView_asBitmap, false);
147 |
148 | //缓存策略
149 | cacheStrategy = typedArray.getInt(R.styleable.BaseImageView_cacheStrategy, BaseImageSetting.getInstance().getCacheStrategy());
150 |
151 | initConfig();
152 | if (url != null && !url.equals("")) {
153 | loader.loadImage(context, config);
154 | }
155 |
156 | typedArray.recycle();
157 | }
158 |
159 | private void initConfig() {
160 | config.setUrl(url);
161 | config.setImageView(BaseImageView.this);
162 | // getLifecycle().addObserver(new BaseLifeObserver(context));
163 | //缓存类型
164 | //如果BaseImageConfig缓存策略为默认的ALL 则使用默认缓存策略
165 | config.setCacheStrategy(cacheStrategy);
166 |
167 | //占位图
168 | if (placeholder != 0) {
169 | config.setPlaceholder(placeholder);
170 | }
171 |
172 | //错误图
173 | if (errorPic != 0) {
174 | config.setErrorPic(errorPic);
175 | }
176 |
177 | //是否淡出淡入
178 | config.setCrossFade(isCrossFade);
179 |
180 | //centerCrop
181 | if (isCenterCrop) {
182 | config.setCenterCrop();
183 | }
184 | //是否将图片剪切为圆形
185 | if (isCircle) {
186 | config.setCircle();
187 | }
188 | config.setTopRightRadius(topRightRadius);
189 | config.setTopLeftRadius(topLeftRadius);
190 | config.setBottomRightRadius(bottomRightRadius);
191 | config.setBottomLeftRadius(bottomLeftRadius);
192 | config.setRadius(radius);
193 | }
194 |
195 | @SuppressLint("CheckResult")
196 | public void load(Object url) {
197 | this.url = url;
198 | config.setUrl(url);
199 | loader.loadImage(context, config);
200 | }
201 |
202 | public int getPlaceholder() {
203 | return placeholder;
204 | }
205 |
206 | public void setPlaceholder(int placeholder) {
207 | this.placeholder = placeholder;
208 | }
209 |
210 | public int getErrorPic() {
211 | return errorPic;
212 | }
213 |
214 | public void setErrorPic(int errorPic) {
215 | this.errorPic = errorPic;
216 | }
217 |
218 | public boolean isCrossFade() {
219 | return isCrossFade;
220 | }
221 |
222 | public void setCrossFade(boolean crossFade) {
223 | isCrossFade = crossFade;
224 | }
225 |
226 | public boolean isCenterCrop() {
227 | return isCenterCrop;
228 | }
229 |
230 | public void setCenterCrop(boolean centerCrop) {
231 | isCenterCrop = centerCrop;
232 | }
233 |
234 | public boolean isCircle() {
235 | return isCircle;
236 | }
237 |
238 | public void setCircle(boolean circle) {
239 | isCircle = circle;
240 | }
241 |
242 | public int getTopRightRadius() {
243 | return topRightRadius;
244 | }
245 |
246 | public void setTopRightRadius(int topRightRadius) {
247 | this.topRightRadius = topRightRadius;
248 | }
249 |
250 | public int getTopLeftRadius() {
251 | return topLeftRadius;
252 | }
253 |
254 | public void setTopLeftRadius(int topLeftRadius) {
255 | this.topLeftRadius = topLeftRadius;
256 | }
257 |
258 | public int getBottomRightRadius() {
259 | return bottomRightRadius;
260 | }
261 |
262 | public void setBottomRightRadius(int bottomRightRadius) {
263 | this.bottomRightRadius = bottomRightRadius;
264 | }
265 |
266 | public int getBottomLeftRadius() {
267 | return bottomLeftRadius;
268 | }
269 |
270 | public void setBottomLeftRadius(int bottomLeftRadius) {
271 | this.bottomLeftRadius = bottomLeftRadius;
272 | }
273 |
274 | public int getRadius() {
275 | return radius;
276 | }
277 |
278 | public void setRadius(int radius) {
279 | this.radius = radius;
280 | }
281 |
282 | public int getCacheStrategy() {
283 | return cacheStrategy;
284 | }
285 |
286 | public void setCacheStrategy(int cacheStrategy) {
287 | this.cacheStrategy = cacheStrategy;
288 | }
289 |
290 | public BaseImageConfig getConfig() {
291 | return config;
292 | }
293 |
294 | public void setUrl(String url) {
295 | load(url);
296 | }
297 | }
298 |
--------------------------------------------------------------------------------
/BaseImageLoader/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 | * Created by Alex on 2020/12/2 0002
17 | *
18 | * 页面内容介绍:
19 | * 设置缓存大小,单位为MB 不设置默认为MemoryCache是20mb BitmapPool是30mb DiskCache是250mb
20 | * setCacheFileName();设置缓存文件夹使用Glide原生api 文档说明: Creates an {@link com.bumptech.glide.disklrucache.DiskLruCache} based disk cache in the internal disk cache directory.
21 | * setCacheStrategy(); 缓存类型详看{@link com.bumptech.glide.load.engine.DiskCacheStrategy}
22 | *
23 | *
24 | * ================================================
25 | */
26 | public class App extends BaseApp
10 | * Created by Alex on 2020/12/4 0004
11 | *
12 | * 页面内容介绍:
13 | * 继承自 {@link BaseImageConfig} 实现了基本功能需求字段
14 | * 如需添加一个自定义功能
15 | * 自行实现构造方法并且在自定义的{@link BaseImageLoaderStrategy}中重写loadImage接口实现方法(暂时不建议重写BaseImageLoaderStrategy,自定义类继承BaseImageConfig是建议的方式)
16 | * 参照{@link BaseImageLoader}
17 | *
18 | * ================================================
19 | */
20 | public class ImageConfig extends BaseImageConfig {
21 |
22 | private boolean isVisibility;
23 |
24 | public void setVisibility(boolean visibility) {
25 | isVisibility = visibility;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/java/me/alex/baseimageloaderdemo/ImageData.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloaderdemo;
2 |
3 |
4 | import androidx.databinding.BaseObservable;
5 | import androidx.databinding.Bindable;
6 |
7 | /**
8 | * ================================================
9 | * Description:
10 | *
11 | * Created by Alex on 2020/12/11 0011
12 | *
13 | * 页面内容介绍: DataBinding JavaBean
14 | *
15 | * ================================================
16 | */
17 | public class ImageData extends BaseObservable {
18 | private String content;
19 | private String imageUrl;
20 |
21 | @Bindable
22 | public String getContent() {
23 | return content;
24 | }
25 |
26 | public void setContent(String content) {
27 | this.content = content;
28 | notifyPropertyChanged(BR.content);
29 | }
30 |
31 | @Bindable
32 | public String getImageUrl() {
33 | return imageUrl;
34 | }
35 |
36 | public void setImageUrl(String imageUrl) {
37 | this.imageUrl = imageUrl;
38 | notifyPropertyChanged(BR.imageUrl);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/java/me/alex/baseimageloaderdemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package me.alex.baseimageloaderdemo;
2 |
3 | import android.view.View;
4 |
5 | import com.kongzue.baseframework.BaseActivity;
6 | import com.kongzue.baseframework.interfaces.DarkStatusBarTheme;
7 | import com.kongzue.baseframework.interfaces.FragmentLayout;
8 | import com.kongzue.baseframework.interfaces.Layout;
9 | import com.kongzue.baseframework.util.FragmentChangeUtil;
10 | import com.kongzue.baseframework.util.JumpParameter;
11 | import com.kongzue.tabbar.Tab;
12 | import com.kongzue.tabbar.TabBarView;
13 | import com.kongzue.tabbar.interfaces.OnTabChangeListener;
14 |
15 | import java.util.ArrayList;
16 | import java.util.List;
17 |
18 | import me.alex.baseimageloader.tools.AutoLoadTools;
19 | import me.alex.baseimageloaderdemo.fragment.AutoLoadFragment;
20 | import me.alex.baseimageloaderdemo.fragment.BaseImageViewFragment;
21 | import me.alex.baseimageloaderdemo.fragment.CustomFragment;
22 | import me.alex.baseimageloaderdemo.fragment.ImageFragment;
23 |
24 | @DarkStatusBarTheme(true)
25 | @Layout(R.layout.activity_main)
26 | @FragmentLayout(R.id.frame)
27 | public class MainActivity extends BaseActivity {
28 |
29 | @Override
30 | public void initViews() {
31 | List
24 | * Created by Alex on 2020/12/16
25 | *
26 | * 页面内容介绍: 自动加载
27 | *
28 | * ================================================
29 | */
30 | @Layout(R.layout.fragment_autoload)
31 | public class AutoLoadFragment extends BaseFragment
14 | * Created by Alex on 2020/12/15
15 | *
16 | * 页面内容介绍: 自定义View
17 | *
18 | * ================================================
19 | */
20 | @Layout(R.layout.fragment_baseimageview)
21 | public class BaseImageViewFragment extends BaseFragment
17 | * Created by Alex on 2020/12/16
18 | *
19 | * 页面内容介绍: 加载至任意View
20 | *
21 | * ================================================
22 | */
23 | @Layout(R.layout.fragment_custom)
24 | public class CustomFragment extends BaseFragment
33 | * Created by Alex on 2020/12/15
34 | *
35 | * 页面内容介绍: ImageView
36 | *
37 | * ================================================
38 | */
39 | @Layout(R.layout.fragment_image)
40 | public class ImageFragment extends BaseFragment