forked from panpf/sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
panpengfei
committed
Jun 4, 2016
1 parent
a316229
commit dbb85a8
Showing
18 changed files
with
179 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#### 简介 | ||
|
||
Sketch在运行的过程中会有各种各样的异常,你可以通过ErrorCallback收到这些异常,然后将这些异常记录下来帮助解决问题 | ||
|
||
### 使用 | ||
|
||
ErrorCallback默认实现只是将收到的异常打印在了logcat中,而你需要继承ErrorCallback然后去记录异常 | ||
|
||
ErrorCallback包含以下方法: | ||
>* onInstallDiskCacheFailed(Exception, File):安装DiskLruCache失败,但Sketch还是可以正常运行的。 | ||
一旦失败了这个方法就会被频繁回调,这是因为每一次需要用到DiskCache的时候都会尝试恢复,再次失败就会再次回调。 | ||
因此你在记录日志的时候不必每次失败都记,可以每隔一个小时记一次 | ||
>* onDecodeGifImageFailed(Throwable, LoadRequest, BitmapFactory.Options):解码GIF图片失败。你需要过滤一下Throwable, | ||
如果是UnsatisfiedLinkError或ExceptionInInitializerError就是找到libpl_droidsonroids_gif.so, | ||
这个时候你就要记录一下当前设备的abi类型,Sketch中包含的libpl_droidsonroids_gif.so已经覆盖了全平台了,如果还找不到就说明有其他的问题了。 | ||
另外这里的Options是只含有outWidth、outHeight、outMimeType信息的 | ||
>* onDecodeNormalImageFailed(Throwable, LoadRequest, BitmapFactory.Options):解码普通图片失败。这里的Options是只含有outWidth、outHeight、outMimeType、inSampleSize信息的 | ||
最后调用Sketch.with(context).getConfiguration().setErrorCallback(ErrorCallback)使用你的ErrorCallback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#### 简介 | ||
|
||
inSampleSize是用来减小读到内存中的图片的尺寸,非常重要,默认的实现是ImageSizeCalculator.calculateInSampleSize(int, int, int, int)方法 | ||
```java | ||
@Override | ||
public int calculateInSampleSize(int outWidth, int outHeight, int targetWidth, int targetHeight) { | ||
// 如果目标尺寸都小于等于0,那就别计算了没意义 | ||
if(targetWidth <= 0 && targetHeight <= 0){ | ||
return 1; | ||
} | ||
|
||
// 如果目标尺寸都大于等于原始尺寸,也别计算了没意义 | ||
if(targetWidth >= outWidth && targetHeight >= outHeight){ | ||
return 1; | ||
} | ||
|
||
// 首先根据缩放后只要有任何一边小于等于目标即可的规则计算一遍inSampleSize | ||
int inSampleSize = 1; | ||
do{ | ||
inSampleSize *= 2; | ||
}while ((outWidth/inSampleSize) > targetWidth && (outHeight/inSampleSize) > targetHeight); | ||
|
||
// 然后根据比较像素总数的原则过滤掉那些比较极端的一边特别小,一边特别大的图片 | ||
// 比如目标尺寸是400x400,图片的尺寸是6000*600,缩放后是3000*300 | ||
// 这样看来的确是满足了第一个条件了,但是图片的尺寸依然很大 | ||
// 因此这一步我们根据像素总数来过滤,规则是总像素数不得大于目标尺寸像素数的两倍 | ||
long totalPixels = (outWidth/inSampleSize) * (outHeight/inSampleSize); | ||
final long totalReqPixelsCap = targetWidth * targetHeight * 2; | ||
while (totalPixels > totalReqPixelsCap) { | ||
inSampleSize *= 2; | ||
totalPixels /= 2; | ||
} | ||
|
||
return inSampleSize; | ||
} | ||
``` | ||
|
||
其实现规则是: | ||
>* 如果目标尺寸的宽高都小于0,就不计算了直接返回1 | ||
>* 如果目标尺寸的宽高都大于原图的宽高,就不计算了直接返回1 | ||
>* 然后开始每次乘以2计算inSampleSize,直到应用inSampleSize后原图的宽或高小于目标尺寸的宽或高即可 | ||
>* 接下来开始按照总像素数进行就算inSampleSize,直到应用inSampleSize后原图的总像素数小于目标尺寸总像素数的2倍即可 | ||
#### 自定义inSampleSize计算规则 | ||
如果你想自定义inSampleSize的话,你只需继承ImageSizeCalculator,并重写calculateInSampleSize(int, int, int, int)方法, | ||
然后调用Sketch.with(context).getConfiguration().setImageSizeCalculator(ImageSizeCalculator)方法应用即可 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#### 简介 | ||
maxSize用来防止加载过大的图片到内存中,已达到节省内存的目的 | ||
|
||
#### 背景 | ||
我们在移动端显示图片的时候都需要面对加载大尺寸图片的问题 | ||
目前主流的Android旗舰机内存一般在2G左右,而APP最多可用内存为200M左右 | ||
现在手机都是800万像素的,随便拍一张照片都是3264x1840的,那么加载到内存中就需要3264x1840x4/1024/1024=22M,照这么看加载9张3264x1840的图片内存就耗完了,显然我们不能这样搞 | ||
|
||
#### 何时使用 | ||
ImageDecoder会在读取图片的时候根据maxSize计算出合适的inSampleSize,然后再读取图片 | ||
|
||
#### 关于inSampleSize | ||
BitmapFactory提供了一个Options来配置读取图片的相关选项,其中一个参数就是inSampleSize,其作用是设置缩放倍数。 | ||
|
||
例如: | ||
原图尺寸3264x1840 | ||
inSampleSize=2 | ||
那么最终读到内存的图片的尺寸就是1632x920,宽高都缩小了2倍,其所占内存从23M缩小到1632x920x4/1024/1024=5.7M,缩小了4倍。 | ||
|
||
#### 缺省值 | ||
>* 在使用load()方法加载图片的时候maxSize的缺省值是当前设备屏幕的0.75倍 | ||
>* 在使用SketchImageView的时候如果layout_width和layout_height是固定的那么就会用layout_width和layout_height来作为maxSize,否则就使用当前屏幕宽高的0.75倍来作为maxSize | ||
因此在大多数情况下你不需要主动设置maxSize,Sketch会自动帮你搞定 | ||
|
||
#### 自定义maxSize计算规则 | ||
如果你对现有的maxSize计算规则不满意,那么你可以继承ImageSizeCalculator类重写calculateImageMaxSize()方法实现你自己的计算规则 | ||
然后调用Sketch.with(context).getConfiguration().setImageSizeCalculator(ImageSizeCalculator)方法来使用你自定义的ImageSizeCalculator |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.