在前幾篇博客中,你已經知道如何從不同的資源中加載圖片,以及哪些不同的方式的占位符。這周的博客是重要的,如果你不能支配圖片的大小去加載:調整大小和縮放。
**用 resize(x,y) 調整圖片大小?**
通常情況下,如果你的服務器或者 API 提供的圖像是你需要的精確馳卻,這時是完美的情況下,在內存小號和圖像質量之間的權衡。
在和 Picasso 比較后,Glide 有[更加高效](https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en)的內存管理。Glide 自動限制了圖片的尺寸在緩存和內存中,并給到 `ImageView` 需要的尺寸。Picasso 也有這樣的能力,但需要調用 `fit()` 方法。對于 Glide,如果圖片不會自動適配到 `ImageView`,調用 `override(horizontalSize, verticalSize) `。這將在圖片顯示到 `ImageView`之前重新改變圖片大小。
~~~
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
~~~
當你還沒有目標 view 去知道尺寸的時候,這個選項也可能是有用的。比如,如果 App 想要在閃屏界面預熱緩存,它還不能測量 `ImageView` 的尺寸。然而,如果你知道這個圖片多少大,用 override 去提供明確的尺寸。
**縮放圖像**?
現在,對于任何圖像操作,調整大小真的能讓長寬比失真并且丑化圖像顯示。在你大多數的使用場景中,你想要避免發生這種情況。Glide 提供了一般變化去處理圖像顯示。提供了兩個標準選項:`centerCrop` 和 `fitCenter`。
**CenterCrop**?
`CenterCrop()`是一個裁剪技術,即縮放圖像讓它填充到 `ImageView` 界限內并且側鍵額外的部分。`ImageView` 可能會完全填充,但圖像可能不會完整顯示。
~~~
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel)
.centerCrop() // this cropping technique scales the image so that it fills the requested bounds and then crops the extra.
.into(imageViewResizeCenterCrop);
~~~
**FitCenter**?
`fitCenter()` 是裁剪技術,即縮放圖像讓圖像都測量出來等于或小于 `ImageView` 的邊界范圍。該圖像將會完全顯示,但可能不會填滿整個 `ImageView`。
~~~
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200)
.fitCenter()
.into(imageViewResizeFitCenter);
~~~
我們會在以后的博客去做自定義的轉換,以及 centerCrop() 和 fitCenter() 。
**Outlook**?
在這篇博客中,你學會如何去對圖像的大小和顯示進行調整。這對創建一個很棒的應用非常具有幫助。在我們進入 Glide 進階課程之前,我們來看一個 Glide 很獨特的功能:顯示 Gif 和 video。
- 前言
- 一開始
- 二加載進階
- 三ListAdapter(ListView, GridView)
- 四占位符 和 漸現動畫
- 五圖片重設大小 和 縮放
- 六顯示 Gif 和 Video
- 七緩存基礎
- 八請求優先級
- 九縮略圖
- 十回調:SimpleTarget 和 ViewTarget 用于自定義視圖類
- 十一加載圖片到通知欄和應用小部件中
- 十二異常:調試和錯誤處理
- 十三自定義轉換
- 十四用 animate() 自定義動畫
- 十五集成網絡棧
- 十六用 Module 自定義
- 十七Module 實例:接受自簽名證書的 HTTPS
- 十八Module 實例:自定義緩存
- 十九Module 實例:用自定義尺寸優化加載的圖片
- 二十動態使用 Model Loader
- 二十一如何旋轉圖像
- 二十二系列綜述