diff --git a/docs/logs/log_2.6.0_p1.md b/docs/logs/log_2.6.0_p1.md index 6c319d65d4..0efb2c5e8c 100644 --- a/docs/logs/log_2.6.0_p1.md +++ b/docs/logs/log_2.6.0_p1.md @@ -16,6 +16,8 @@ * :hammer: HttpStack.ImageHttpResponse.getResponseHeader() 重命名为 HttpStack.Response.getHeader() * :hammer: ImageAttrs 从 drawable 中移动到 decode 包中 * :bug: 修复开启解码 gif 后内存缓存失效的 bug +* :bug: 修复在生成 WrappedImageProcessor 的 key 时崩溃的错误,并且优化 key 的格式 +* :hammer: 重构 WrappedImageProcessor 的 getKey() 和 toString() 方法,所有子类都需要重新适配 sample app: * :bug: 修复 UNSPLASH 页面没有加载完数据就切换到别的页面时崩溃的 BUG diff --git a/docs/wiki/image_processor.md b/docs/wiki/image_processor.md index 51c569e9f6..992a0e5c38 100644 --- a/docs/wiki/image_processor.md +++ b/docs/wiki/image_processor.md @@ -42,6 +42,7 @@ Sketch.with(context).display(uri, sketchImageView) 2. 通过 [ImageProcessor] 的 process() 方法传进去的 Bitmap 在处理完之后无需回收它,Sketch 会去回收 3. 创建新的 bitmap 之前,先从 BitmapPool 中查找可复用 bitmap,实在没有再创建新的 bitmap 4. 在处理的过程中产生的过渡 Bitmap 在用完之后一定要调用 BitmapPoolUtils.freeBitmapToPool(Bitmap, BitmapPool) 回收掉 +4. 实现 onToString() 和 onGetKey() 方法 自定义的 [ImageProcessor] 写好后通过 [LoadOptions]/[DisplayOptions] 的 setProcessor(ImageProcessor) 方法或 [LoadHelper]/[DisplayHelper] 的 processor(ImageProcessor) 方法使用即可 diff --git a/sample/src/main/java/me/xiaopan/sketchsample/SampleErrorTracker.kt b/sample/src/main/java/me/xiaopan/sketchsample/SampleErrorTracker.kt index 93df409b9b..9efc99b221 100644 --- a/sample/src/main/java/me/xiaopan/sketchsample/SampleErrorTracker.kt +++ b/sample/src/main/java/me/xiaopan/sketchsample/SampleErrorTracker.kt @@ -34,7 +34,7 @@ internal class SampleErrorTracker(context: Context) : ErrorTracker(context) { this.context = context.applicationContext } - override fun getKey(): String { + override fun toString(): String { return "SampleErrorTracker" } @@ -197,7 +197,7 @@ internal class SampleErrorTracker(context: Context) : ErrorTracker(context) { "Sketch - %s - " + "%s" + "\n%s", - processor.key, + processor.toString(), decodeUri(context, imageUri), outOfMemoryInfo ), throwable)) diff --git a/sketch/src/main/java/me/xiaopan/sketch/Configuration.java b/sketch/src/main/java/me/xiaopan/sketch/Configuration.java index 347f783b5f..73bd2893e1 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/Configuration.java +++ b/sketch/src/main/java/me/xiaopan/sketch/Configuration.java @@ -174,7 +174,7 @@ public Configuration setDiskCache(@NonNull DiskCache newDiskCache) { if (oldDiskCache != null) { oldDiskCache.close(); } - SLog.w(NAME, "diskCache=%s", diskCache.getKey()); + SLog.w(NAME, "diskCache=%s", diskCache.toString()); } return this; } @@ -206,7 +206,7 @@ public Configuration setBitmapPool(@NonNull BitmapPool newBitmapPool) { if (oldBitmapPool != null) { oldBitmapPool.close(); } - SLog.w(NAME, "bitmapPool = %s", bitmapPool.getKey()); + SLog.w(NAME, "bitmapPool=%s", bitmapPool.toString()); } return this; } @@ -237,7 +237,7 @@ public Configuration setMemoryCache(@NonNull MemoryCache memoryCache) { if (oldMemoryCache != null) { oldMemoryCache.close(); } - SLog.w(NAME, "memoryCache=", memoryCache.getKey()); + SLog.w(NAME, "memoryCache=", memoryCache.toString()); } return this; } @@ -265,7 +265,7 @@ public Configuration setProcessedImageCache(@NonNull ProcessedImageCache process //noinspection ConstantConditions if (processedImageCache != null) { this.processedImageCache = processedImageCache; - SLog.w(NAME, "processedCache=", processedImageCache.getKey()); + SLog.w(NAME, "processedCache=", processedImageCache.toString()); } return this; } @@ -294,7 +294,7 @@ public Configuration setHttpStack(@NonNull HttpStack httpStack) { //noinspection ConstantConditions if (httpStack != null) { this.httpStack = httpStack; - SLog.w(NAME, "httpStack=", httpStack.getKey()); + SLog.w(NAME, "httpStack=", httpStack.toString()); } return this; } @@ -321,7 +321,7 @@ public Configuration setDecoder(@NonNull ImageDecoder decoder) { //noinspection ConstantConditions if (decoder != null) { this.decoder = decoder; - SLog.w(NAME, "decoder=%s", decoder.getKey()); + SLog.w(NAME, "decoder=%s", decoder.toString()); } return this; } @@ -348,7 +348,7 @@ public Configuration setDownloader(@NonNull ImageDownloader downloader) { //noinspection ConstantConditions if (downloader != null) { this.downloader = downloader; - SLog.w(NAME, "downloader=%s", downloader.getKey()); + SLog.w(NAME, "downloader=%s", downloader.toString()); } return this; } @@ -375,7 +375,7 @@ public Configuration setOrientationCorrector(@NonNull ImageOrientationCorrector //noinspection ConstantConditions if (orientationCorrector != null) { this.orientationCorrector = orientationCorrector; - SLog.w(NAME, "orientationCorrector=%s", orientationCorrector.getKey()); + SLog.w(NAME, "orientationCorrector=%s", orientationCorrector.toString()); } return this; } @@ -403,7 +403,7 @@ public Configuration setDefaultDisplayer(@NonNull ImageDisplayer defaultDisplaye //noinspection ConstantConditions if (defaultDisplayer != null) { this.defaultDisplayer = defaultDisplayer; - SLog.w(NAME, "defaultDisplayer=%s", defaultDisplayer.getKey()); + SLog.w(NAME, "defaultDisplayer=%s", defaultDisplayer.toString()); } return this; } @@ -430,7 +430,7 @@ public Configuration setResizeProcessor(@NonNull ImageProcessor resizeProcessor) //noinspection ConstantConditions if (resizeProcessor != null) { this.resizeProcessor = resizeProcessor; - SLog.w(NAME, "resizeProcessor=%s", resizeProcessor.getKey()); + SLog.w(NAME, "resizeProcessor=%s", resizeProcessor.toString()); } return this; } @@ -457,7 +457,7 @@ public Configuration setResizeCalculator(@NonNull ResizeCalculator resizeCalcula //noinspection ConstantConditions if (resizeCalculator != null) { this.resizeCalculator = resizeCalculator; - SLog.w(NAME, "resizeCalculator=%s", resizeCalculator.getKey()); + SLog.w(NAME, "resizeCalculator=%s", resizeCalculator.toString()); } return this; } @@ -484,7 +484,7 @@ public Configuration setSizeCalculator(@NonNull ImageSizeCalculator sizeCalculat //noinspection ConstantConditions if (sizeCalculator != null) { this.sizeCalculator = sizeCalculator; - SLog.w(NAME, "sizeCalculator=%s", sizeCalculator.getKey()); + SLog.w(NAME, "sizeCalculator=%s", sizeCalculator.toString()); } return this; } @@ -512,7 +512,7 @@ public Configuration setFreeRideManager(@NonNull FreeRideManager freeRideManager //noinspection ConstantConditions if (freeRideManager != null) { this.freeRideManager = freeRideManager; - SLog.w(NAME, "freeRideManager=%s", freeRideManager.getKey()); + SLog.w(NAME, "freeRideManager=%s", freeRideManager.toString()); } return this; } @@ -543,7 +543,7 @@ public Configuration setExecutor(@NonNull RequestExecutor newRequestExecutor) { if (oldRequestExecutor != null) { oldRequestExecutor.shutdown(); } - SLog.w(NAME, "executor=%s", executor.getKey()); + SLog.w(NAME, "executor=%s", executor.toString()); } return this; } @@ -570,7 +570,7 @@ public Configuration setHelperFactory(@NonNull HelperFactory helperFactory) { //noinspection ConstantConditions if (helperFactory != null) { this.helperFactory = helperFactory; - SLog.w(NAME, "helperFactory=%s", helperFactory.getKey()); + SLog.w(NAME, "helperFactory=%s", helperFactory.toString()); } return this; } @@ -597,7 +597,7 @@ public Configuration setRequestFactory(@NonNull RequestFactory requestFactory) { //noinspection ConstantConditions if (requestFactory != null) { this.requestFactory = requestFactory; - SLog.w(NAME, "requestFactory=%s", requestFactory.getKey()); + SLog.w(NAME, "requestFactory=%s", requestFactory.toString()); } return this; } @@ -625,7 +625,7 @@ public Configuration setErrorTracker(@NonNull ErrorTracker errorTracker) { //noinspection ConstantConditions if (errorTracker != null) { this.errorTracker = errorTracker; - SLog.w(NAME, "errorTracker=%s", errorTracker.getKey()); + SLog.w(NAME, "errorTracker=%s", errorTracker.toString()); } return this; } @@ -749,31 +749,31 @@ public Configuration setMobileDataPauseDownloadEnabled(boolean mobileDataPauseDo } @NonNull - public String getInfo() { + public String toString() { return NAME + ": " + - "\n" + "uriModelManager:" + uriModelManager.getKey() + - "\n" + "optionsFilterManager:" + optionsFilterManager.getKey() + - - "\n" + "diskCache:" + diskCache.getKey() + - "\n" + "bitmapPool:" + bitmapPool.getKey() + - "\n" + "memoryCache:" + memoryCache.getKey() + - "\n" + "processedImageCache:" + processedImageCache.getKey() + - - "\n" + "httpStack:" + httpStack.getKey() + - "\n" + "decoder:" + decoder.getKey() + - "\n" + "downloader:" + downloader.getKey() + - "\n" + "orientationCorrector:" + orientationCorrector.getKey() + - - "\n" + "defaultDisplayer:" + defaultDisplayer.getKey() + - "\n" + "resizeProcessor:" + resizeProcessor.getKey() + - "\n" + "resizeCalculator:" + resizeCalculator.getKey() + - "\n" + "sizeCalculator:" + sizeCalculator.getKey() + - - "\n" + "freeRideManager:" + freeRideManager.getKey() + - "\n" + "executor:" + executor.getKey() + - "\n" + "helperFactory:" + helperFactory.getKey() + - "\n" + "requestFactory:" + requestFactory.getKey() + - "\n" + "errorTracker:" + errorTracker.getKey() + + "\n" + "uriModelManager:" + uriModelManager.toString() + + "\n" + "optionsFilterManager:" + optionsFilterManager.toString() + + + "\n" + "diskCache:" + diskCache.toString() + + "\n" + "bitmapPool:" + bitmapPool.toString() + + "\n" + "memoryCache:" + memoryCache.toString() + + "\n" + "processedImageCache:" + processedImageCache.toString() + + + "\n" + "httpStack:" + httpStack.toString() + + "\n" + "decoder:" + decoder.toString() + + "\n" + "downloader:" + downloader.toString() + + "\n" + "orientationCorrector:" + orientationCorrector.toString() + + + "\n" + "defaultDisplayer:" + defaultDisplayer.toString() + + "\n" + "resizeProcessor:" + resizeProcessor.toString() + + "\n" + "resizeCalculator:" + resizeCalculator.toString() + + "\n" + "sizeCalculator:" + sizeCalculator.toString() + + + "\n" + "freeRideManager:" + freeRideManager.toString() + + "\n" + "executor:" + executor.toString() + + "\n" + "helperFactory:" + helperFactory.toString() + + "\n" + "requestFactory:" + requestFactory.toString() + + "\n" + "errorTracker:" + errorTracker.toString() + "\n" + "pauseDownload:" + optionsFilterManager.isPauseDownloadEnabled() + "\n" + "pauseLoad:" + optionsFilterManager.isPauseLoadEnabled() + diff --git a/sketch/src/main/java/me/xiaopan/sketch/ErrorTracker.java b/sketch/src/main/java/me/xiaopan/sketch/ErrorTracker.java index d21676224f..a508ff7e4b 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/ErrorTracker.java +++ b/sketch/src/main/java/me/xiaopan/sketch/ErrorTracker.java @@ -23,7 +23,7 @@ /** * 负责输出错误信息,你可借此记录错误日志 */ -public class ErrorTracker implements Identifier { +public class ErrorTracker { private static final String NAME = "ErrorTracker"; private Context context; @@ -125,7 +125,7 @@ public void onProcessImageError(@NonNull Throwable e, @NonNull String imageUri, } SLog.e(NAME, "onProcessImageError. imageUri: %s. processor: %s", - imageUri, processor.getKey()); + imageUri, processor.toString()); } /** @@ -196,7 +196,7 @@ public void onDecodeRegionError(@NonNull String imageUri, int imageWidth, int im @NonNull @Override - public String getKey() { + public String toString() { return NAME; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/Identifier.java b/sketch/src/main/java/me/xiaopan/sketch/Identifier.java deleted file mode 100644 index 213f42f46a..0000000000 --- a/sketch/src/main/java/me/xiaopan/sketch/Identifier.java +++ /dev/null @@ -1,13 +0,0 @@ -package me.xiaopan.sketch; - -import android.support.annotation.NonNull; - -public interface Identifier { - /** - * 用来生成缓存 key,或者在 log 中标识一个组件 - *
- * 通常 key 是由组件名称和组件属性组成的,例如 String.format("%s(radius=%d,maskColor=%d)", "GaussianBlurImageProcessor", radius, maskColor) - */ - @NonNull - String getKey(); -} diff --git a/sketch/src/main/java/me/xiaopan/sketch/Key.java b/sketch/src/main/java/me/xiaopan/sketch/Key.java new file mode 100644 index 0000000000..48061e0b1b --- /dev/null +++ b/sketch/src/main/java/me/xiaopan/sketch/Key.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2017 Peng fei Pan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package me.xiaopan.sketch; + +import android.support.annotation.Nullable; + +public interface Key { + + @Nullable + String getKey(); +} diff --git a/sketch/src/main/java/me/xiaopan/sketch/Sketch.java b/sketch/src/main/java/me/xiaopan/sketch/Sketch.java index 767fc35b3c..0d73d8d24d 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/Sketch.java +++ b/sketch/src/main/java/me/xiaopan/sketch/Sketch.java @@ -73,7 +73,7 @@ public static Sketch with(@NonNull Context context) { if (instance == null) { Sketch newInstance = new Sketch(context); SLog.i(null, "Version %s %s(%d) -> %s", - BuildConfig.BUILD_TYPE, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, newInstance.configuration.getInfo()); + BuildConfig.BUILD_TYPE, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, newInstance.configuration.toString()); Initializer initializer = SketchUtils.findInitializer(context); if (initializer != null) { diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/BitmapPool.java b/sketch/src/main/java/me/xiaopan/sketch/cache/BitmapPool.java index 063c247e59..9c7be99c4c 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/BitmapPool.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/BitmapPool.java @@ -4,12 +4,10 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import me.xiaopan.sketch.Identifier; - /** * {@link Bitmap} 复用缓存池,用于缓存并复用 {@link Bitmap},便于解码时直接使用,减少内存分配 */ -public interface BitmapPool extends Identifier { +public interface BitmapPool { /** * 获取最大容量 diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/DiskCache.java b/sketch/src/main/java/me/xiaopan/sketch/cache/DiskCache.java index b14bebf066..9967ab7cc1 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/DiskCache.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/DiskCache.java @@ -25,13 +25,12 @@ import java.io.OutputStream; import java.util.concurrent.locks.ReentrantLock; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.util.DiskLruCache; /** * 磁盘缓存管理器 */ -public interface DiskCache extends Identifier { +public interface DiskCache { String DISK_CACHE_DIR_NAME = "sketch"; int DISK_CACHE_MAX_SIZE = 100 * 1024 * 1024; int DISK_CACHE_RESERVED_SPACE_SIZE = 200 * 1024 * 1024; diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/LruBitmapPool.java b/sketch/src/main/java/me/xiaopan/sketch/cache/LruBitmapPool.java index 42f8d6756b..f452f96c5d 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/LruBitmapPool.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/LruBitmapPool.java @@ -27,7 +27,9 @@ public class LruBitmapPool implements BitmapPool { private static final Bitmap.Config DEFAULT_CONFIG = Bitmap.Config.ARGB_8888; private static final String NAME = "LruBitmapPool"; + @NonNull private final LruPoolStrategy strategy; + @NonNull private final Set allowedConfigs; private final int initialMaxSize; private final BitmapTracker tracker; @@ -43,9 +45,8 @@ public class LruBitmapPool implements BitmapPool { private boolean closed; private boolean disabled; - LruBitmapPool(Context context, int maxSize, LruPoolStrategy strategy, Set allowedConfigs) { - context = context.getApplicationContext(); - this.context = context; + LruBitmapPool(Context context, int maxSize, @NonNull LruPoolStrategy strategy, @NonNull Set allowedConfigs) { + this.context = context.getApplicationContext(); this.initialMaxSize = maxSize; this.maxSize = maxSize; this.strategy = strategy; @@ -69,7 +70,7 @@ public LruBitmapPool(Context context, int maxSize) { * @param allowedConfigs {@link Bitmap.Config} 白名单 */ @SuppressWarnings("unused") - public LruBitmapPool(Context context, int maxSize, Set allowedConfigs) { + public LruBitmapPool(Context context, int maxSize, @NonNull Set allowedConfigs) { this(context, maxSize, getDefaultStrategy(), allowedConfigs); } @@ -319,8 +320,9 @@ private void dumpUnchecked() { @NonNull @Override - public String getKey() { - return String.format("%s(maxSize=%s)", NAME, Formatter.formatFileSize(context, getMaxSize())); + public String toString() { + return String.format("%s(maxSize=%s,strategy=%s,allowedConfigs=%s)", + NAME, Formatter.formatFileSize(context, getMaxSize()), strategy.getKey(), allowedConfigs.toString()); } private interface BitmapTracker { diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/LruDiskCache.java b/sketch/src/main/java/me/xiaopan/sketch/cache/LruDiskCache.java index abb4859eb6..ec6779f5d4 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/LruDiskCache.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/LruDiskCache.java @@ -353,7 +353,7 @@ public synchronized ReentrantLock getEditLock(@NonNull String key) { @NonNull @Override - public String getKey() { + public String toString() { return String.format("%s(maxSize=%s,appVersionCode=%d,cacheDir=%s)", NAME, Formatter.formatFileSize(context, maxSize), appVersionCode, cacheDir.getPath()); } diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/LruMemoryCache.java b/sketch/src/main/java/me/xiaopan/sketch/cache/LruMemoryCache.java index 23ddac15c3..af5e25d1d0 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/LruMemoryCache.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/LruMemoryCache.java @@ -194,7 +194,7 @@ public synchronized void close() { @NonNull @Override - public String getKey() { + public String toString() { return String.format("%s(maxSize=%s)", NAME, Formatter.formatFileSize(context, getMaxSize())); } diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/MemoryCache.java b/sketch/src/main/java/me/xiaopan/sketch/cache/MemoryCache.java index dc50109be3..3185971082 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/MemoryCache.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/MemoryCache.java @@ -19,13 +19,12 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.drawable.SketchRefBitmap; /** * 内存缓存管理器 */ -public interface MemoryCache extends Identifier { +public interface MemoryCache { /** * 缓存一张图片 * diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/AttributeStrategy.java b/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/AttributeStrategy.java index c76dc8dd6d..a3ce383ef2 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/AttributeStrategy.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/AttributeStrategy.java @@ -1,6 +1,7 @@ package me.xiaopan.sketch.cache.recycle; import android.graphics.Bitmap; +import android.support.annotation.Nullable; import me.xiaopan.sketch.util.SketchUtils; @@ -11,6 +12,14 @@ public class AttributeStrategy implements LruPoolStrategy { private final KeyPool keyPool = new KeyPool(); private final GroupedLinkedMap groupedMap = new GroupedLinkedMap(); + private static String getBitmapString(Bitmap bitmap) { + return getBitmapString(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); + } + + private static String getBitmapString(int width, int height, Bitmap.Config config) { + return "[" + width + "x" + height + "], " + config; + } + public void put(Bitmap bitmap) { final Key key = keyPool.get(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); @@ -46,15 +55,13 @@ public int getSize(Bitmap bitmap) { @Override public String toString() { - return "AttributeStrategy:\n " + groupedMap; + return "AttributeStrategy(" + groupedMap + ")"; } - private static String getBitmapString(Bitmap bitmap) { - return getBitmapString(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); - } - - private static String getBitmapString(int width, int height, Bitmap.Config config) { - return "[" + width + "x" + height + "], " + config; + @Nullable + @Override + public String getKey() { + return "AttributeStrategy"; } // Visible for testing. diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/LruPoolStrategy.java b/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/LruPoolStrategy.java index 0f7df870b0..33f90a88f2 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/LruPoolStrategy.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/LruPoolStrategy.java @@ -2,7 +2,9 @@ import android.graphics.Bitmap; -public interface LruPoolStrategy { +import me.xiaopan.sketch.Key; + +public interface LruPoolStrategy extends Key { void put(Bitmap bitmap); Bitmap get(int width, int height, Bitmap.Config config); diff --git a/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/SizeConfigStrategy.java b/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/SizeConfigStrategy.java index 697258f8ec..bfb564f3c9 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/SizeConfigStrategy.java +++ b/sketch/src/main/java/me/xiaopan/sketch/cache/recycle/SizeConfigStrategy.java @@ -3,6 +3,7 @@ import android.annotation.TargetApi; import android.graphics.Bitmap; import android.os.Build; +import android.support.annotation.Nullable; import java.util.HashMap; import java.util.Map; @@ -44,8 +45,7 @@ public class SizeConfigStrategy implements LruPoolStrategy { private final KeyPool keyPool = new KeyPool(); private final GroupedLinkedMap groupedMap = new GroupedLinkedMap(); - private final Map> sortedSizes = - new HashMap>(); + private final Map> sortedSizes = new HashMap>(); @Override public void put(Bitmap bitmap) { @@ -159,6 +159,12 @@ public String toString() { return sb.append(")}").toString(); } + @Nullable + @Override + public String getKey() { + return "SizeConfigStrategy"; + } + // Visible for testing. static class KeyPool extends BaseKeyPool { diff --git a/sketch/src/main/java/me/xiaopan/sketch/decode/ImageDecoder.java b/sketch/src/main/java/me/xiaopan/sketch/decode/ImageDecoder.java index fa7623fd3e..af4a447cc4 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/decode/ImageDecoder.java +++ b/sketch/src/main/java/me/xiaopan/sketch/decode/ImageDecoder.java @@ -24,7 +24,6 @@ import java.util.LinkedList; import java.util.List; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.SLog; import me.xiaopan.sketch.datasource.DataSource; import me.xiaopan.sketch.process.ImageProcessor; @@ -46,7 +45,7 @@ *
  • 缓存经过处理的图片
  • * */ -public class ImageDecoder implements Identifier { +public class ImageDecoder { private static final String NAME = "ImageDecoder"; private DecodeTimeAnalyze timeAnalyze = new DecodeTimeAnalyze(); @@ -202,7 +201,7 @@ private void doProcess(LoadRequest request, DecodeResult result) throws ProcessE @NonNull @Override - public String getKey() { + public String toString() { return NAME; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/decode/ImageOrientationCorrector.java b/sketch/src/main/java/me/xiaopan/sketch/decode/ImageOrientationCorrector.java index fc170c42cf..27f54f7948 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/decode/ImageOrientationCorrector.java +++ b/sketch/src/main/java/me/xiaopan/sketch/decode/ImageOrientationCorrector.java @@ -29,7 +29,6 @@ import java.io.IOException; import java.io.InputStream; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.cache.BitmapPool; import me.xiaopan.sketch.datasource.DataSource; import me.xiaopan.sketch.util.ExifInterface; @@ -38,7 +37,7 @@ /** * 图片方向纠正器,可让原本被旋转了的图片以正常方向显示 */ -public class ImageOrientationCorrector implements Identifier { +public class ImageOrientationCorrector { public static final int PAINT_FLAGS = Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG; @@ -334,7 +333,7 @@ public void reverseRotate(Rect srcRect, int imageWidth, int imageHeight, int exi @NonNull @Override - public String getKey() { + public String toString() { return "ImageOrientationCorrector"; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/decode/ImageSizeCalculator.java b/sketch/src/main/java/me/xiaopan/sketch/decode/ImageSizeCalculator.java index e9bee44da3..692eda1d6d 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/decode/ImageSizeCalculator.java +++ b/sketch/src/main/java/me/xiaopan/sketch/decode/ImageSizeCalculator.java @@ -25,7 +25,6 @@ import java.lang.reflect.Field; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.SketchView; import me.xiaopan.sketch.request.DisplayRequest; import me.xiaopan.sketch.request.FixedSize; @@ -36,7 +35,7 @@ /** * 和图片尺寸相关的需求的计算器 */ -public class ImageSizeCalculator implements Identifier { +public class ImageSizeCalculator { private static final String KEY = "ImageSizeCalculator"; private int openGLMaxTextureSize = -1; @@ -308,7 +307,7 @@ public void setTargetSizeScale(float targetSizeScale) { @NonNull @Override - public String getKey() { + public String toString() { return KEY; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/decode/ProcessedImageCache.java b/sketch/src/main/java/me/xiaopan/sketch/decode/ProcessedImageCache.java index c3fd1b2b3f..e5db453279 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/decode/ProcessedImageCache.java +++ b/sketch/src/main/java/me/xiaopan/sketch/decode/ProcessedImageCache.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.util.concurrent.locks.ReentrantLock; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.cache.DiskCache; import me.xiaopan.sketch.datasource.DiskCacheDataSource; import me.xiaopan.sketch.request.ImageFrom; @@ -36,7 +35,7 @@ /** * 对读到内存后又再次处理过的图片进行缓存,下次就不用再处理了,可加快加载速度 */ -public class ProcessedImageCache implements Identifier { +public class ProcessedImageCache { /** * 判断是否可以使用此功能 @@ -160,7 +159,7 @@ public void saveToDiskCache(LoadRequest request, Bitmap bitmap) { @NonNull @Override - public String getKey() { + public String toString() { return "ProcessedImageCache"; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/decode/ResizeCalculator.java b/sketch/src/main/java/me/xiaopan/sketch/decode/ResizeCalculator.java index 9a6dbc3216..fdb75216f6 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/decode/ResizeCalculator.java +++ b/sketch/src/main/java/me/xiaopan/sketch/decode/ResizeCalculator.java @@ -20,13 +20,12 @@ import android.support.annotation.NonNull; import android.widget.ImageView; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.request.Resize; /** * 用来计算 {@link Resize} */ -public class ResizeCalculator implements Identifier { +public class ResizeCalculator { private static final String KEY = "ResizeCalculator"; public static Rect srcMappingStartRect(int originalImageWidth, int originalImageHeight, int targetImageWidth, int targetImageHeight) { @@ -96,7 +95,7 @@ public static int[] scaleTargetSize(int originalImageWidth, int originalImageHei @NonNull @Override - public String getKey() { + public String toString() { return KEY; } diff --git a/sketch/src/main/java/me/xiaopan/sketch/display/ColorTransitionImageDisplayer.java b/sketch/src/main/java/me/xiaopan/sketch/display/ColorTransitionImageDisplayer.java index cbc66c81bb..5b410fb19d 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/display/ColorTransitionImageDisplayer.java +++ b/sketch/src/main/java/me/xiaopan/sketch/display/ColorTransitionImageDisplayer.java @@ -66,8 +66,8 @@ public void display(@NonNull SketchView sketchView, @NonNull Drawable newDrawabl @NonNull @Override - public String getKey() { - return String.format("%s(duration=%d, color=%d, alwaysUse=%s)", KEY, duration, color, alwaysUse); + public String toString() { + return String.format("%s(duration=%d,color=%d,alwaysUse=%s)", KEY, duration, color, alwaysUse); } @Override diff --git a/sketch/src/main/java/me/xiaopan/sketch/display/DefaultImageDisplayer.java b/sketch/src/main/java/me/xiaopan/sketch/display/DefaultImageDisplayer.java index f3b932f744..64d3e7a395 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/display/DefaultImageDisplayer.java +++ b/sketch/src/main/java/me/xiaopan/sketch/display/DefaultImageDisplayer.java @@ -51,7 +51,7 @@ public boolean isAlwaysUse() { @NonNull @Override - public String getKey() { + public String toString() { return KEY; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/display/FadeInImageDisplayer.java b/sketch/src/main/java/me/xiaopan/sketch/display/FadeInImageDisplayer.java index 1109adeaa2..cdc0ca2136 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/display/FadeInImageDisplayer.java +++ b/sketch/src/main/java/me/xiaopan/sketch/display/FadeInImageDisplayer.java @@ -59,7 +59,7 @@ public boolean isAlwaysUse() { @NonNull @Override - public String getKey() { - return String.format("%s(duration=%d, alwaysUse=%s)", KEY, duration, alwaysUse); + public String toString() { + return String.format("%s(duration=%d,alwaysUse=%s)", KEY, duration, alwaysUse); } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/display/ImageDisplayer.java b/sketch/src/main/java/me/xiaopan/sketch/display/ImageDisplayer.java index 1046d80cc2..01d25e1284 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/display/ImageDisplayer.java +++ b/sketch/src/main/java/me/xiaopan/sketch/display/ImageDisplayer.java @@ -19,13 +19,12 @@ import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.SketchView; /** * 图片显示器,用来在加载完成后显示图片 */ -public interface ImageDisplayer extends Identifier { +public interface ImageDisplayer { int DEFAULT_ANIMATION_DURATION = 400; /** diff --git a/sketch/src/main/java/me/xiaopan/sketch/display/TransitionImageDisplayer.java b/sketch/src/main/java/me/xiaopan/sketch/display/TransitionImageDisplayer.java index 7cb8e12be8..1a263cae17 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/display/TransitionImageDisplayer.java +++ b/sketch/src/main/java/me/xiaopan/sketch/display/TransitionImageDisplayer.java @@ -98,7 +98,7 @@ public int getDuration() { @NonNull @Override - public String getKey() { - return String.format("%s(duration=%d, alwaysUse=%s)", KEY, duration, alwaysUse); + public String toString() { + return String.format("%s(duration=%d,alwaysUse=%s)", KEY, duration, alwaysUse); } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/display/ZoomInImageDisplayer.java b/sketch/src/main/java/me/xiaopan/sketch/display/ZoomInImageDisplayer.java index 6c2a748fa2..a96568c553 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/display/ZoomInImageDisplayer.java +++ b/sketch/src/main/java/me/xiaopan/sketch/display/ZoomInImageDisplayer.java @@ -106,8 +106,8 @@ public void display(@NonNull SketchView sketchView, @NonNull Drawable newDrawabl @NonNull @Override - public String getKey() { - return String.format("%s(duration=%d, fromX=%s, fromY=%s, interpolator=%s, alwaysUse=%s)", + public String toString() { + return String.format("%s(duration=%d,fromX=%s,fromY=%s,interpolator=%s,alwaysUse=%s)", KEY, duration, fromX, fromY, interpolator != null ? interpolator.getClass().getSimpleName() : null, alwaysUse); } diff --git a/sketch/src/main/java/me/xiaopan/sketch/display/ZoomOutImageDisplayer.java b/sketch/src/main/java/me/xiaopan/sketch/display/ZoomOutImageDisplayer.java index 0ebb6ba2e3..c7eafb39c4 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/display/ZoomOutImageDisplayer.java +++ b/sketch/src/main/java/me/xiaopan/sketch/display/ZoomOutImageDisplayer.java @@ -105,8 +105,8 @@ public void display(@NonNull SketchView sketchView, @NonNull Drawable newDrawabl @NonNull @Override - public String getKey() { - return String.format("%s(duration=%d, fromX=%s, fromY=%s, interpolator=%s, alwaysUse=%s)", + public String toString() { + return String.format("%s(duration=%d,fromX=%s,fromY=%s,interpolator=%s,alwaysUse=%s)", KEY, duration, fromX, fromY, interpolator != null ? interpolator.getClass().getSimpleName() : null, alwaysUse); } diff --git a/sketch/src/main/java/me/xiaopan/sketch/http/HttpStack.java b/sketch/src/main/java/me/xiaopan/sketch/http/HttpStack.java index d597cbd803..eee5b4bb80 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/http/HttpStack.java +++ b/sketch/src/main/java/me/xiaopan/sketch/http/HttpStack.java @@ -23,12 +23,10 @@ import java.io.InputStream; import java.util.Map; -import me.xiaopan.sketch.Identifier; - /** * 负责发送 HTTP 请求,并返回响应 */ -public interface HttpStack extends Identifier { +public interface HttpStack { int DEFAULT_READ_TIMEOUT = 7 * 1000; // 默认读取超时时间 int DEFAULT_CONNECT_TIMEOUT = 7 * 1000; // 默认连接超时时间 int DEFAULT_MAX_RETRY_COUNT = 0; // 默认最大重试次数 diff --git a/sketch/src/main/java/me/xiaopan/sketch/http/HurlStack.java b/sketch/src/main/java/me/xiaopan/sketch/http/HurlStack.java index d72b9054a0..b357891613 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/http/HurlStack.java +++ b/sketch/src/main/java/me/xiaopan/sketch/http/HurlStack.java @@ -119,7 +119,7 @@ public boolean canRetry(Throwable throwable) { @NonNull @Override - public String getKey() { + public String toString() { return String.format("%s(maxRetryCount=%d,connectTimeout=%d,readTimeout=%d,userAgent=%s)", KEY, maxRetryCount, connectTimeout, readTimeout, userAgent); } diff --git a/sketch/src/main/java/me/xiaopan/sketch/http/ImageDownloader.java b/sketch/src/main/java/me/xiaopan/sketch/http/ImageDownloader.java index e28a836841..9b62a0d712 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/http/ImageDownloader.java +++ b/sketch/src/main/java/me/xiaopan/sketch/http/ImageDownloader.java @@ -26,7 +26,6 @@ import java.io.OutputStream; import java.util.concurrent.locks.ReentrantLock; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.SLog; import me.xiaopan.sketch.cache.DiskCache; import me.xiaopan.sketch.request.BaseRequest; @@ -41,7 +40,7 @@ /** * 负责下载并缓存图片 */ -public class ImageDownloader implements Identifier { +public class ImageDownloader { private static final String NAME = "ImageDownloader"; /** @@ -379,7 +378,7 @@ private int readData(@NonNull DownloadRequest request, @NonNull InputStream inpu @NonNull @Override - public String getKey() { + public String toString() { return NAME; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/optionsfilter/OptionsFilterManager.java b/sketch/src/main/java/me/xiaopan/sketch/optionsfilter/OptionsFilterManager.java index 49d3d65df6..2ffdc2eebb 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/optionsfilter/OptionsFilterManager.java +++ b/sketch/src/main/java/me/xiaopan/sketch/optionsfilter/OptionsFilterManager.java @@ -22,7 +22,6 @@ import java.util.List; import me.xiaopan.sketch.Configuration; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.Sketch; import me.xiaopan.sketch.SketchView; import me.xiaopan.sketch.request.DownloadOptions; @@ -38,7 +37,7 @@ *
  • 全局移动数据下暂停下载功能 -> {@link MobileDataPauseDownloadController}
  • * */ -public class OptionsFilterManager implements Identifier { +public class OptionsFilterManager { private PauseDownloadOptionsFilter pauseDownloadOptionsFilter; private PauseLoadOptionsFilter pauseLoadOptionsFilter; @@ -231,7 +230,7 @@ public void setMobileDataPauseDownloadEnabled(Configuration configuration, boole @NonNull @Override - public String getKey() { + public String toString() { return "OptionsFilterManager"; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/CircleImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/CircleImageProcessor.java index df33626408..f6862dabe2 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/CircleImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/CircleImageProcessor.java @@ -35,7 +35,6 @@ */ @SuppressWarnings("unused") public class CircleImageProcessor extends WrappedImageProcessor { - private static final String KEY = "CircleImageProcessor"; private static CircleImageProcessor instance; @@ -58,11 +57,6 @@ public static CircleImageProcessor getInstance() { return instance; } - @Override - public String onGetKey() { - return KEY; - } - @Override protected boolean isInterceptResize() { return true; @@ -108,4 +102,15 @@ public Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, @Nullabl return circleBitmap; } + + @NonNull + @Override + public String onToString() { + return "CircleImageProcessor"; + } + + @Override + public String onGetKey() { + return "Circle"; + } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/GaussianBlurImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/GaussianBlurImageProcessor.java index d597e44927..2a272530b6 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/GaussianBlurImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/GaussianBlurImageProcessor.java @@ -12,18 +12,17 @@ * 高斯模糊图片处理器 */ public class GaussianBlurImageProcessor extends WrappedImageProcessor { - private static final String KEY = "GaussianBlurImageProcessor"; private static final int NO_LAYER_COLOR = -1; private static final int DEFAULT_RADIUS = 15; private int radius; // 模糊半径,取值为0到100 - private int layerColor; // 图层颜色,在模糊后的图片上加一层颜色 + private int maskColor; // 图层颜色,在模糊后的图片上加一层颜色 - private GaussianBlurImageProcessor(int radius, int layerColor, WrappedImageProcessor wrappedImageProcessor) { + private GaussianBlurImageProcessor(int radius, int maskColor, WrappedImageProcessor wrappedImageProcessor) { super(wrappedImageProcessor); this.radius = radius; - this.layerColor = layerColor; + this.maskColor = maskColor; } /** @@ -76,7 +75,7 @@ public static GaussianBlurImageProcessor makeLayerColor(int layerColor) { /** * 创建一个指定半径的高斯模糊图片处理器 * - * @param radius 模糊半径,取值为0到100 + * @param radius 模糊半径,取值为 0 到 100 * @param wrappedImageProcessor 嵌套一个图片处理器 * @return GaussianBlurImageProcessor */ @@ -88,7 +87,7 @@ public static GaussianBlurImageProcessor makeRadius(int radius, WrappedImageProc /** * 创建一个指定半径的高斯模糊图片处理器 * - * @param radius 模糊半径,取值为0到100 + * @param radius 模糊半径,取值为 0 到 100 * @return GaussianBlurImageProcessor */ @SuppressWarnings("unused") @@ -108,7 +107,7 @@ public static GaussianBlurImageProcessor make(WrappedImageProcessor wrappedImage } /** - * 创建一个半径为15的高斯模糊图片处理器 + * 创建一个半径为 15 的高斯模糊图片处理器 * * @return GaussianBlurImageProcessor */ @@ -334,27 +333,6 @@ public static Bitmap fastGaussianBlur(Bitmap sentBitmap, int radius, boolean can } } - /** - * 获取模糊半径 - */ - @SuppressWarnings("unused") - public int getRadius() { - return radius; - } - - /** - * 获取图层颜色 - */ - @SuppressWarnings("unused") - public int getLayerColor() { - return layerColor; - } - - @Override - public String onGetKey() { - return String.format("%s(radius=%d,maskColor=%d)", KEY, radius, layerColor); - } - @NonNull @Override public Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, @Nullable Resize resize, boolean lowQualityImage) { @@ -370,11 +348,38 @@ public Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, @Nullabl } // layer color handle - if (layerColor != NO_LAYER_COLOR) { + if (maskColor != NO_LAYER_COLOR) { Canvas canvas = new Canvas(blurBitmap); - canvas.drawColor(layerColor); + canvas.drawColor(maskColor); } return blurBitmap; } + + /** + * 获取模糊半径 + */ + @SuppressWarnings("unused") + public int getRadius() { + return radius; + } + + /** + * 获取图层颜色 + */ + @SuppressWarnings("unused") + public int getMaskColor() { + return maskColor; + } + + @NonNull + @Override + public String onToString() { + return String.format("%s(radius=%d,maskColor=%d)", "GaussianBlurImageProcessor", radius, maskColor); + } + + @Override + public String onGetKey() { + return String.format("%s(radius=%d,maskColor=%d)", "GaussianBlur", radius, maskColor); + } } \ No newline at end of file diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/ImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/ImageProcessor.java index b1ffe90045..391f0fd773 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/ImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/ImageProcessor.java @@ -20,14 +20,14 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import me.xiaopan.sketch.Identifier; +import me.xiaopan.sketch.Key; import me.xiaopan.sketch.Sketch; import me.xiaopan.sketch.request.Resize; /** * 用于在图片读取到内存后对图片进行修改 */ -public interface ImageProcessor extends Identifier { +public interface ImageProcessor extends Key { /** * 修改图片并返回修改后的图片,请不要回收原图片 * diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/MaskImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/MaskImageProcessor.java index 360ee9a630..d79669eaf3 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/MaskImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/MaskImageProcessor.java @@ -1,5 +1,6 @@ package me.xiaopan.sketch.process; +import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; @@ -16,31 +17,27 @@ */ public class MaskImageProcessor extends WrappedImageProcessor { - private static final String KEY = "MaskImageProcessor"; - private Paint paint; private int maskColor; + /** + * 创建一个遮罩图片处理器 + * + * @param maskColor 遮罩颜色 + * @param wrappedProcessor 嵌套一个图片处理器 + */ public MaskImageProcessor(int maskColor, WrappedImageProcessor wrappedProcessor) { super(wrappedProcessor); this.maskColor = maskColor; } - public MaskImageProcessor(int maskColor) { - this(maskColor, null); - } - /** - * 获取遮罩颜色 + * 创建一个遮罩图片处理器 + * + * @param maskColor 遮罩颜色 */ - @SuppressWarnings("unused") - public int getMaskColor() { - return maskColor; - } - - @Override - public String onGetKey() { - return String.format("%s(maskColor=%d)", KEY, maskColor); + public MaskImageProcessor(int maskColor) { + this(maskColor, null); } @NonNull @@ -80,6 +77,7 @@ public Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, Resize r int saveFlags = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG; + @SuppressLint("WrongConstant") int src = canvas.saveLayer(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint, saveFlags); canvas.drawBitmap(bitmap, 0, 0, null); @@ -91,4 +89,23 @@ public Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, Resize r return maskBitmap; } + + /** + * 获取遮罩颜色 + */ + @SuppressWarnings("unused") + public int getMaskColor() { + return maskColor; + } + + @NonNull + @Override + public String onToString() { + return String.format("%s(%d)", "MaskImageProcessor", maskColor); + } + + @Override + public String onGetKey() { + return String.format("%s(%d)", "Mask", maskColor); + } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/ReflectionImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/ReflectionImageProcessor.java index b1f107f11b..458ff8e2b2 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/ReflectionImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/ReflectionImageProcessor.java @@ -35,7 +35,6 @@ */ @SuppressWarnings("unused") public class ReflectionImageProcessor extends WrappedImageProcessor { - private static final String KEY = "ReflectionImageProcessor"; private static final int DEFAULT_REFLECTION_SPACING = 2; private static final float DEFAULT_REFLECTION_SCALE = 0.3f; @@ -47,7 +46,7 @@ public class ReflectionImageProcessor extends WrappedImageProcessor { * 创建一个倒影图片处理器 * * @param reflectionSpacing 倒影和图片之间的距离 - * @param reflectionScale 倒影的高度所占原图高度比例,取值为0.0到1 + * @param reflectionScale 倒影的高度所占原图高度比例,取值为 0 到 1 * @param wrappedImageProcessor 嵌套一个图片处理器 */ public ReflectionImageProcessor(int reflectionSpacing, float reflectionScale, WrappedImageProcessor wrappedImageProcessor) { @@ -60,25 +59,28 @@ public ReflectionImageProcessor(int reflectionSpacing, float reflectionScale, Wr * 创建一个倒影图片处理器 * * @param reflectionSpacing 倒影和图片之间的距离 - * @param reflectionScale 倒影的高度所占原图高度比例,取值为0.0到1 + * @param reflectionScale 倒影的高度所占原图高度比例,取值为 0 到 1 */ public ReflectionImageProcessor(int reflectionSpacing, float reflectionScale) { this(reflectionSpacing, reflectionScale, null); } + /** + * 创建一个倒影图片处理器,默认倒影和图片之间的距离是 2 个像素,倒影的高度所占原图高度比例是 0.3 + * + * @param wrappedImageProcessor 嵌套一个图片处理器 + */ public ReflectionImageProcessor(WrappedImageProcessor wrappedImageProcessor) { this(DEFAULT_REFLECTION_SPACING, DEFAULT_REFLECTION_SCALE, wrappedImageProcessor); } + /** + * 创建一个倒影图片处理器,默认倒影和图片之间的距离是 2 个像素,倒影的高度所占原图高度比例是 0.3 + */ public ReflectionImageProcessor() { this(DEFAULT_REFLECTION_SPACING, DEFAULT_REFLECTION_SCALE, null); } - @Override - public String onGetKey() { - return String.format("%s(scale=%s,spacing=%d)", KEY, reflectionScale, reflectionSpacing); - } - @NonNull @Override public Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, Resize resize, boolean lowQualityImage) { @@ -121,4 +123,15 @@ public float getReflectionScale() { public int getReflectionSpacing() { return reflectionSpacing; } + + @NonNull + @Override + public String onToString() { + return String.format("%s(scale=%s,spacing=%d)", "ReflectionImageProcessor", reflectionScale, reflectionSpacing); + } + + @Override + public String onGetKey() { + return String.format("%s(scale=%s,spacing=%d)", "Reflection", reflectionScale, reflectionSpacing); + } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/ResizeImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/ResizeImageProcessor.java index 83b3155868..3ca45e108c 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/ResizeImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/ResizeImageProcessor.java @@ -27,7 +27,6 @@ import me.xiaopan.sketch.request.Resize; public class ResizeImageProcessor implements ImageProcessor { - private static final String KEY = "ResizeImageProcessor"; @NonNull @Override @@ -64,7 +63,13 @@ public Bitmap process(@NonNull Sketch sketch, @NonNull Bitmap bitmap, @Nullable @NonNull @Override + public String toString() { + return "ResizeImageProcessor"; + } + + @Nullable + @Override public String getKey() { - return KEY; + return "Resize"; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/RotateImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/RotateImageProcessor.java index 76816e8622..84c3696937 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/RotateImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/RotateImageProcessor.java @@ -22,6 +22,7 @@ import android.graphics.Paint; import android.graphics.RectF; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import me.xiaopan.sketch.Sketch; import me.xiaopan.sketch.cache.BitmapPool; @@ -32,15 +33,25 @@ */ @SuppressWarnings("unused") public class RotateImageProcessor extends WrappedImageProcessor { - private static final String KEY = "RotateImageProcessor"; private int degrees; + /** + * 创建一个图片旋转处理器 + * + * @param degrees 旋转角度 + * @param wrappedImageProcessor 嵌套一个图片处理器 + */ public RotateImageProcessor(int degrees, WrappedImageProcessor wrappedImageProcessor) { super(wrappedImageProcessor); this.degrees = degrees; } + /** + * 创建一个图片旋转处理器 + * + * @param degrees 旋转角度 + */ public RotateImageProcessor(int degrees) { this(degrees, null); } @@ -82,13 +93,20 @@ public Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, Resize r return rotate(bitmap, degrees, sketch.getConfiguration().getBitmapPool()); } + @NonNull + @Override + public String onToString() { + return String.format("%s(%d)", "RotateImageProcessor", degrees); + } + + @Nullable @Override public String onGetKey() { - // 0度或360度时不加标识,这样做是为了避免浪费合适的内存缓存 + // 0 度或 360 度时不加标识,这样做是为了避免浪费合适的内存缓存 if (degrees % 360 == 0) { return null; - } else { - return String.format("%s(degrees=%d)", KEY, degrees); } + + return String.format("%s(%d)", "Rotate", degrees); } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/RoundRectImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/RoundRectImageProcessor.java index 4cf28f4bb1..0830bdaa1e 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/RoundRectImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/RoundRectImageProcessor.java @@ -25,6 +25,8 @@ import android.graphics.RectF; import android.support.annotation.NonNull; +import java.util.Arrays; + import me.xiaopan.sketch.Sketch; import me.xiaopan.sketch.cache.BitmapPool; import me.xiaopan.sketch.decode.ResizeCalculator; @@ -35,10 +37,19 @@ */ @SuppressWarnings("unused") public class RoundRectImageProcessor extends WrappedImageProcessor { - private static final String KEY = "RoundRectImageProcessor"; + @NonNull private float[] cornerRadius; + /** + * 创建一个圆角矩形图片处理器 + * + * @param topLeftRadius 左上角圆角角度 + * @param topRightRadius 右上角圆角角度 + * @param bottomLeftRadius 左下角圆角角度 + * @param bottomRightRadius 右下角圆角角度 + * @param wrappedImageProcessor 嵌套一个图片处理器 + */ public RoundRectImageProcessor(float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius, WrappedImageProcessor wrappedImageProcessor) { super(wrappedImageProcessor); cornerRadius = new float[]{topLeftRadius, topLeftRadius, @@ -47,29 +58,37 @@ public RoundRectImageProcessor(float topLeftRadius, float topRightRadius, float bottomRightRadius, bottomRightRadius}; } + /** + * 创建一个圆角矩形图片处理器 + * + * @param topLeftRadius 左上角圆角角度 + * @param topRightRadius 右上角圆角角度 + * @param bottomLeftRadius 左下角圆角角度 + * @param bottomRightRadius 右下角圆角角度 + */ public RoundRectImageProcessor(float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius) { this(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, null); } + /** + * 创建一个圆角矩形图片处理器 + * + * @param cornerRadius 圆角角度 + * @param wrappedImageProcessor 嵌套一个图片处理器 + */ public RoundRectImageProcessor(float cornerRadius, WrappedImageProcessor wrappedImageProcessor) { this(cornerRadius, cornerRadius, cornerRadius, cornerRadius, wrappedImageProcessor); } + /** + * 创建一个圆角矩形图片处理器 + * + * @param cornerRadius 圆角角度 + */ public RoundRectImageProcessor(float cornerRadius) { this(cornerRadius, cornerRadius, cornerRadius, cornerRadius, null); } - @Override - public String onGetKey() { - if (cornerRadius != null) { - return String.format("%s(cornerRadius=[%sx%s,%sx%s,%sx%s,%sx%s])", - KEY, cornerRadius[0], cornerRadius[1], cornerRadius[2], cornerRadius[3], - cornerRadius[4], cornerRadius[5], cornerRadius[6], cornerRadius[7]); - } else { - return KEY; - } - } - @Override protected boolean isInterceptResize() { return true; @@ -115,7 +134,19 @@ public Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, Resize r return roundRectBitmap; } + @NonNull public float[] getCornerRadius() { return cornerRadius; } + + @NonNull + @Override + public String onToString() { + return String.format("%s(%s)", "RoundRectImageProcessor", Arrays.toString(cornerRadius)); + } + + @Override + public String onGetKey() { + return String.format("%s(%s)", "RoundRect", Arrays.toString(cornerRadius)); + } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/process/WrappedImageProcessor.java b/sketch/src/main/java/me/xiaopan/sketch/process/WrappedImageProcessor.java index 9db50a3650..b220a0cf03 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/process/WrappedImageProcessor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/process/WrappedImageProcessor.java @@ -27,46 +27,15 @@ import me.xiaopan.sketch.request.Resize; /** - * 用于组合两个ImageProcessor一起使用 + * 用于组合两个 {@link ImageProcessor} 一起使用,可以无限嵌套 */ public abstract class WrappedImageProcessor extends ResizeImageProcessor { private WrappedImageProcessor wrappedProcessor; - public WrappedImageProcessor(WrappedImageProcessor wrappedProcessor) { + protected WrappedImageProcessor(WrappedImageProcessor wrappedProcessor) { this.wrappedProcessor = wrappedProcessor; } - @NonNull - @Override - public final String getKey() { - String selfKey = onGetKey(); - String wrappedKey = wrappedProcessor != null ? wrappedProcessor.getKey() : null; - if (!TextUtils.isEmpty(selfKey)) { - if (!TextUtils.isEmpty(wrappedKey)) { - return String.format("%s&%s", selfKey, wrappedKey); - } else { - return selfKey; - } - } else { - if (!TextUtils.isEmpty(wrappedKey)) { - return wrappedKey; - } else { - return "WrappedImageProcessor"; - } - } - } - - @SuppressWarnings("unused") - public WrappedImageProcessor getWrappedProcessor() { - return wrappedProcessor; - } - - public abstract String onGetKey(); - - protected boolean isInterceptResize() { - return false; - } - @NonNull @Override public final Bitmap process(@NonNull Sketch sketch, @NonNull Bitmap bitmap, @Nullable Resize resize, boolean lowQualityImage) { @@ -97,4 +66,49 @@ public final Bitmap process(@NonNull Sketch sketch, @NonNull Bitmap bitmap, @Nul @NonNull public abstract Bitmap onProcess(@NonNull Sketch sketch, @NonNull Bitmap bitmap, @Nullable Resize resize, boolean lowQualityImage); + + @SuppressWarnings("unused") + public WrappedImageProcessor getWrappedProcessor() { + return wrappedProcessor; + } + + @Nullable + @Override + public String getKey() { + String selfKey = onGetKey(); + String wrappedKey = wrappedProcessor != null ? wrappedProcessor.getKey() : null; + + if (!TextUtils.isEmpty(selfKey)) { + if (!TextUtils.isEmpty(wrappedKey)) { + return String.format("%s->%s", selfKey, wrappedKey); + } else { + return selfKey; + } + } else if (!TextUtils.isEmpty(wrappedKey)) { + return wrappedKey; + } + return null; + } + + @Nullable + public abstract String onGetKey(); + + @NonNull + @Override + public String toString() { + String selfToString = onToString(); + String wrappedToString = wrappedProcessor != null ? wrappedProcessor.toString() : null; + + if (TextUtils.isEmpty(wrappedToString)) { + return selfToString; + } + return String.format("%s->%s", selfToString, wrappedToString); + } + + @NonNull + public abstract String onToString(); + + protected boolean isInterceptResize() { + return false; + } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/FixedSize.java b/sketch/src/main/java/me/xiaopan/sketch/request/FixedSize.java index eeb33ed6c5..4cb3106dd2 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/FixedSize.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/FixedSize.java @@ -18,12 +18,10 @@ import android.support.annotation.NonNull; -import me.xiaopan.sketch.Identifier; - /** * {@link android.widget.ImageView} 的固定尺寸,只能是通过 layout_width 和 layout_height 设置的固定值才能算是固定尺寸 */ -public class FixedSize implements Identifier { +public class FixedSize { private int width; private int height; @@ -57,7 +55,7 @@ public boolean equals(final Object obj) { @NonNull @Override - public String getKey() { + public String toString() { return String.format("FixedSize(%dx%d)", width, height); } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/FreeRideManager.java b/sketch/src/main/java/me/xiaopan/sketch/request/FreeRideManager.java index dae1890fdc..1f5ca67a87 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/FreeRideManager.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/FreeRideManager.java @@ -22,7 +22,6 @@ import java.util.Set; import java.util.WeakHashMap; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.SLog; /** @@ -31,7 +30,7 @@ * 对于相同的请求(不同类型条件不一样),只要第一个请求执行完毕了,后续请求可以直接使用第一个请求的结果,那么我们可以将所有后续的请求都绑定在第一个请求上, * 等第一个请求执行完毕后直接将结果交给后续请求处理即可,对于这样的情况我们称之为顺风车 */ -public class FreeRideManager implements Identifier { +public class FreeRideManager { private static final String NAME = "FreeRideManager"; private final Object displayFreeRideProviderMapLock = new Object(); @@ -99,7 +98,7 @@ public void unregisterDisplayFreeRideProvider(DisplayFreeRide provider) { boolean success = childFreeRide.processDisplayFreeRide(); if (SLog.isLoggable(SLog.LEVEL_DEBUG | SLog.TYPE_FLOW)) { - SLog.d(NAME, "display. callback free ride. %s. %s <------ %s", + SLog.d(NAME, "display. callback free ride. %s. %s <- %s", success ? "success" : "failed", childFreeRide.getDisplayFreeRideLog(), providerId); } } @@ -197,7 +196,7 @@ public void unregisterDownloadFreeRideProvider(DownloadFreeRide provider) { boolean success = childFreeRide.processDownloadFreeRide(); if (SLog.isLoggable(SLog.LEVEL_DEBUG | SLog.TYPE_FLOW)) { - SLog.d(NAME, "download. callback free ride. %s. %s <------ %s", + SLog.d(NAME, "download. callback free ride. %s. %s <- %s", success ? "success" : "failed", childFreeRide.getDownloadFreeRideLog(), providerId); } } @@ -237,7 +236,7 @@ public boolean byDownloadFreeRide(DownloadFreeRide childFreeRide) { @NonNull @Override - public String getKey() { + public String toString() { return NAME; } diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/HelperFactory.java b/sketch/src/main/java/me/xiaopan/sketch/request/HelperFactory.java index 4ad8bbf2d6..e068897f40 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/HelperFactory.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/HelperFactory.java @@ -18,14 +18,13 @@ import android.support.annotation.NonNull; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.Sketch; import me.xiaopan.sketch.SketchView; /** * 负责创建 {@link DisplayHelper}、{@link LoadHelper}、{@link DownloadHelper} */ -public class HelperFactory implements Identifier { +public class HelperFactory { private static final String KEY = "HelperFactory"; private DisplayHelper cacheDisplayHelper; @@ -61,7 +60,7 @@ public void recycleDisplayHelper(@NonNull DisplayHelper displayHelper) { @NonNull @Override - public String getKey() { + public String toString() { return KEY; } } \ No newline at end of file diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/LoadOptions.java b/sketch/src/main/java/me/xiaopan/sketch/request/LoadOptions.java index 70f1d716a4..96b7060560 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/LoadOptions.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/LoadOptions.java @@ -432,34 +432,43 @@ public void copy(@Nullable LoadOptions options) { public String makeKey() { StringBuilder builder = new StringBuilder(); if (maxSize != null) { - builder.append("_").append(maxSize.getKey()); + if (builder.length() > 0) builder.append('-'); + builder.append(maxSize.getKey()); } if (resize != null) { - builder.append("_").append(resize.getKey()); + if (builder.length() > 0) builder.append('-'); + builder.append(resize.getKey()); if (thumbnailMode) { - builder.append("_").append("thumbnailMode"); + if (builder.length() > 0) builder.append('-'); + builder.append("thumbnailMode"); } } if (correctImageOrientationDisabled) { - builder.append("_").append("correctImageOrientationDisabled"); + if (builder.length() > 0) builder.append('-'); + builder.append("ignoreOrientation"); } if (lowQualityImage) { - builder.append("_").append("lowQualityImage"); + if (builder.length() > 0) builder.append('-'); + builder.append("lowQuality"); } if (inPreferQualityOverSpeed) { - builder.append("_").append("preferQuality"); + if (builder.length() > 0) builder.append('-'); + builder.append("preferQuality"); } if (decodeGifImage) { - builder.append("_").append("decodeGifImage"); + if (builder.length() > 0) builder.append('-'); + builder.append("gif"); } if (bitmapConfig != null) { - builder.append("_").append(bitmapConfig.name()); + if (builder.length() > 0) builder.append('-'); + builder.append(bitmapConfig.name()); } if (processor != null) { // 旋转图片处理器在旋转0度或360度时不用旋转处理,因此也不会返回key,因此这里过滤一下 String processorKey = processor.getKey(); if (!TextUtils.isEmpty(processorKey)) { - builder.append("_").append(processorKey); + if (builder.length() > 0) builder.append('-'); + builder.append(processorKey); } } return builder.toString(); @@ -470,16 +479,19 @@ public String makeKey() { public String makeStateImageKey() { StringBuilder builder = new StringBuilder(); if (resize != null) { - builder.append("_").append(resize.getKey()); + if (builder.length() > 0) builder.append('-'); + builder.append(resize.getKey()); } if (lowQualityImage) { - builder.append("_").append("lowQualityImage"); + if (builder.length() > 0) builder.append('-'); + builder.append("lowQuality"); } if (processor != null) { // 旋转图片处理器在旋转0度或360度时不用旋转处理,因此也不会返回key,因此这里过滤一下 String processorKey = processor.getKey(); if (!TextUtils.isEmpty(processorKey)) { - builder.append("_").append(processorKey); + if (builder.length() > 0) builder.append('-'); + builder.append(processorKey); } } return builder.toString(); diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/MaxSize.java b/sketch/src/main/java/me/xiaopan/sketch/request/MaxSize.java index 46aa59957d..8ae6892996 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/MaxSize.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/MaxSize.java @@ -18,12 +18,12 @@ import android.support.annotation.NonNull; -import me.xiaopan.sketch.Identifier; +import me.xiaopan.sketch.Key; /** * 用于计算 inSimpleSize 缩小图片 */ -public class MaxSize implements Identifier { +public class MaxSize implements Key { private int width; private int height; @@ -57,7 +57,12 @@ public boolean equals(final Object obj) { @NonNull @Override - public String getKey() { + public String toString() { return String.format("MaxSize(%dx%d)", width, height); } + + @Override + public String getKey() { + return toString(); + } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/RequestExecutor.java b/sketch/src/main/java/me/xiaopan/sketch/request/RequestExecutor.java index e28f275f4e..6abb6ba3fb 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/RequestExecutor.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/RequestExecutor.java @@ -29,12 +29,10 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import me.xiaopan.sketch.Identifier; - /** * 请求执行器 */ -public class RequestExecutor implements Identifier { +public class RequestExecutor { public static final int DEFAULT_LOCAL_THREAD_POOL_SIZE = 3; public static final int DEFAULT_NET_THREAD_POOL_SIZE = 3; @@ -139,7 +137,7 @@ public void setNetTaskExecutor(ExecutorService netTaskExecutor) { @NonNull @Override - public String getKey() { + public String toString() { return String.format("%s(%s", KEY, shutdown ? "shutdown" : "running)"); } diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/RequestFactory.java b/sketch/src/main/java/me/xiaopan/sketch/request/RequestFactory.java index 2a3648970a..1630507cdb 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/RequestFactory.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/RequestFactory.java @@ -18,14 +18,13 @@ import android.support.annotation.NonNull; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.Sketch; import me.xiaopan.sketch.uri.UriModel; /** * 负责创建 {@link DisplayRequest}、{@link LoadRequest}、{@link DownloadRequest} */ -public class RequestFactory implements Identifier { +public class RequestFactory { private static final String KEY = "RequestFactory"; public DisplayRequest newDisplayRequest(Sketch sketch, String uri, UriModel uriModel, String key, DisplayOptions displayOptions, @@ -49,7 +48,7 @@ public DownloadRequest newDownloadRequest(Sketch sketch, String uri, UriModel ur @NonNull @Override - public String getKey() { + public String toString() { return KEY; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/Resize.java b/sketch/src/main/java/me/xiaopan/sketch/request/Resize.java index 3a07cb0987..366bd10117 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/Resize.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/Resize.java @@ -17,16 +17,17 @@ package me.xiaopan.sketch.request; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.widget.ImageView; -import me.xiaopan.sketch.Identifier; +import me.xiaopan.sketch.Key; /** * 将图片加载到内存中之后根据 {@link Resize} 进行调整尺寸 *

    * 修正的原则就是最终返回的图片的比例一定是跟 {@link Resize} 一样的,但尺寸小于等于 {@link Resize} ,如果需要必须同 {@link Resize} 一致可以设置 {@link Mode#ASPECT_RATIO_SAME} */ -public class Resize implements Identifier { +public class Resize implements Key { private int width; private int height; @@ -123,7 +124,7 @@ public boolean equals(final Object obj) { @NonNull @Override - public String getKey() { + public String toString() { return String.format("Resize(%dx%d-%s-%s)", width, height, scaleType != null ? scaleType.name() : "null", mode.name()); } @@ -132,6 +133,12 @@ public Mode getMode() { return mode; } + @Nullable + @Override + public String getKey() { + return toString(); + } + public enum Mode { /** * 新图片的尺寸不会比 {@link Resize} 大,但宽高比一定会一样 diff --git a/sketch/src/main/java/me/xiaopan/sketch/request/ShapeSize.java b/sketch/src/main/java/me/xiaopan/sketch/request/ShapeSize.java index 328e55bcc1..bcc359849f 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/request/ShapeSize.java +++ b/sketch/src/main/java/me/xiaopan/sketch/request/ShapeSize.java @@ -19,7 +19,6 @@ import android.support.annotation.NonNull; import android.widget.ImageView; -import me.xiaopan.sketch.Identifier; import me.xiaopan.sketch.drawable.SketchShapeBitmapDrawable; /** @@ -29,7 +28,7 @@ *

    * 当使用 {@link ShapeSize} 时,就可以使用同一个 {@link android.graphics.Bitmap} 在绘制时显示出不同的尺寸,避免了产生多个差别很小的 {@link android.graphics.Bitmap},提高了内存缓存利用率 */ -public class ShapeSize implements Identifier { +public class ShapeSize { private int width; private int height; private ImageView.ScaleType scaleType; @@ -89,7 +88,7 @@ public boolean equals(final Object obj) { @NonNull @Override - public String getKey() { + public String toString() { return String.format("ShapeSize(%dx%d)", width, height); } diff --git a/sketch/src/main/java/me/xiaopan/sketch/uri/UriModelManager.java b/sketch/src/main/java/me/xiaopan/sketch/uri/UriModelManager.java index 86e88f10aa..b35657f850 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/uri/UriModelManager.java +++ b/sketch/src/main/java/me/xiaopan/sketch/uri/UriModelManager.java @@ -23,8 +23,6 @@ import java.util.LinkedList; import java.util.List; -import me.xiaopan.sketch.Identifier; - /** * 负责管理和匹配 {@link UriModel},默认支持以下 uri 和 {@link UriModel} *

    @@ -47,7 +45,7 @@ *

    * 通过 {@link #match(String)} 方法可以为指定 uri 匹配对应的 {@link UriModel} */ -public class UriModelManager implements Identifier { +public class UriModelManager { private List uriModelList = new LinkedList<>(); @@ -129,7 +127,7 @@ public UriModel match(@NonNull String uri) { @NonNull @Override - public String getKey() { + public String toString() { return "UriModelManager"; } } diff --git a/sketch/src/main/java/me/xiaopan/sketch/util/SketchUtils.java b/sketch/src/main/java/me/xiaopan/sketch/util/SketchUtils.java index 05c20be259..09042bfd15 100644 --- a/sketch/src/main/java/me/xiaopan/sketch/util/SketchUtils.java +++ b/sketch/src/main/java/me/xiaopan/sketch/util/SketchUtils.java @@ -990,15 +990,23 @@ public static void rotatePoint(PointF point, int rotateDegrees, Size drawableSiz @SuppressWarnings("unused") @NonNull public static String makeRequestKey(@NonNull String imageUri, @NonNull UriModel uriModel, @NonNull String optionsKey) { - StringBuilder builder = new StringBuilder(); if (uriModel.isConvertShortUriForKey()) { - builder.append(SketchMD5Utils.md5(imageUri)); - } else { - builder.append(imageUri); + imageUri = SketchMD5Utils.md5(imageUri); + } + + if (TextUtils.isEmpty(optionsKey)) { + return imageUri; } - if (!TextUtils.isEmpty(optionsKey)) { - builder.append(optionsKey); + + StringBuilder builder = new StringBuilder(imageUri); + if (imageUri.lastIndexOf("?") == -1) { + builder.append('?'); + } else { + builder.append('&'); } + builder.append("options"); + builder.append("="); + builder.append(optionsKey); return builder.toString(); }