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
Flyge
committed
Sep 3, 2017
1 parent
3740d17
commit 6db5422
Showing
8 changed files
with
141 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,60 @@ | ||
resize用来修剪图片以及调整宽高比例,使用maxSize可以加载合适尺寸的图片到内存中,那么有的时候我们可能还需要固定尺寸的图片或固定宽高比例的图片,resize就是 用来解决这个问题的。 | ||
### Resize 用来干嘛? | ||
|
||
规则如下: | ||
>* 如果原图的尺寸大于resize则按resize裁剪,如果小于resize则会按照resize的比例修剪原图,让原图的宽高比同resize一样。 | ||
>* 如果forceUseResize为true,则即使原图尺寸小于resize,则也会按照resize的尺寸创建一张新的图片。 | ||
Resize 用来调整图片的尺寸和宽高比例 | ||
|
||
有的时候我们需要固定尺寸或固定宽高比的图片,例如将图片用作页面头部的背景,设计图上有标好的尺寸,但是图片的尺寸我们不能控制,这时候就可以用 Resize 用来解决问题 | ||
|
||
### 使用 | ||
|
||
假如设计图上标明的图片尺寸是 720x385,那么我们如实配置即可 | ||
|
||
```java | ||
DisplayOptions options = ...; | ||
options.setResize(720, 385); | ||
``` | ||
|
||
注意,Resize 有两种模式: | ||
* ASPECT_RATIO_SAME: 新图片的尺寸不会比 resize 大,但宽高比一定会一样 | ||
* EXACTLY_SAME: 即使原图尺寸比 resize 小,也会得到一个跟 resize 尺寸一样的 bitmap | ||
|
||
Resize 默认采用 ASPECT_RATIO_SAME 模式,这样的好处是会比较节省内存,因此通过上面的配置你会得到一张宽高比一定是 720/385 的图片,但尺寸可能会比 720x385 小的图片 | ||
|
||
如果你必须要求返回图片的尺寸跟 Resize 一模一样,那么你可以使用 EXACTLY_SAME 模式 | ||
|
||
```java | ||
DisplayOptions options = ...; | ||
options.setResize(new Resize(720, 385, Resize.EXACTLY_SAME)); | ||
``` | ||
|
||
### 调整尺寸时的裁剪规则 | ||
|
||
当 Resize 的尺寸和原图片的尺寸不一致时就会对原图片进行裁剪,具体的裁剪规则是根据 Resize 的 scaleType 属性决定的。 | ||
|
||
默认从 ImageView 获取,你也可以强制指定,如下: | ||
|
||
```java | ||
DisplayOptions options = ...; | ||
options.setResize(new Resize(720, 385, ScaleType.CENTER_CROP)); | ||
``` | ||
|
||
### 自动使用 ImageView 的固定尺寸作为 Resize | ||
|
||
当 ImageView 已经设置了固定尺寸的话,我们就可以不必再写一遍 resize 的尺寸,而是自动使用 ImageView 的尺寸,如下: | ||
|
||
#### 使用 | ||
```java | ||
DisplayOptions options = ...; | ||
options.setResize(300, 300); | ||
options.seForceUseResize(true); | ||
options.setResize(Resize.byViewFixedSize()); | ||
``` | ||
|
||
Resize.byViewFixedSize() 方法也可以设置 Resize.Mode,如下: | ||
|
||
```java | ||
Sketch.with(context).load(R.drawable.ic_launcher, new LoadListener(){ | ||
... | ||
}) | ||
.resize(300, 300) | ||
.commit(); | ||
DisplayOptions options = ...; | ||
options.setResize(Resize.byViewFixedSize(Resize.EXACTLY_SAME)); | ||
``` | ||
|
||
使用DisplayOptions的时候还可以使用resizeByFixedSize(true)方法自动使用SketchImageView的layout_width和layout_height作为resize | ||
但是当你使用了此功能而 ImageView 却没有设置固定尺寸的话就会抛出异常,如下: | ||
|
||
```java | ||
IllegalStateException: ImageView's width and height are not fixed, can not be applied with the Resize.byViewFixedSize() function | ||
``` |
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
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