2015年8月29日土曜日

Android開発 Glideを使ってURLから取得した画像をリサイズ表示

https://github.com/bumptech/glide
Androidの画像変換ライブラリのGlideを使って、URLから画像を取得・リサイズして表示します。

画像の非同期ダウンロード、メモリ管理、キャッシュ管理などを裏側でやってくれるので、利用者側がアプリに画像処理を実装するときには、ほとんど手間取らないで済む。という便利なライブラリです。
同じような画像変換ライブラリとしてPicassoというものが有りました(ライブラリ名はこちらの方が格好いい)が、GlideのほうがGoogle寄り、ということでこちらを使ってみました。
(GitHubのREADMEのいちばん下にThis is not an official Google product.という但し書きがありますが。なぜか、プルリクエストをするときにはGoogleのライセンスを承諾する感じです。

こんな感じで使うことができます。

元画像

アプリでURLを読み込んだ画像


実装するにあたって以下の資料が参考になりました。
Android - PicassoとGlideのどちらを使うべきか? - Qiita
http://qiita.com/rejasupotaro/items/ead90beaeaa2a6eace35
Introduction to Glide, Image Loader Library for Android, recommended by Google :: The Cheese Factory
http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
When placeholder is replaced by image it keeps with the size of placeholder when using .override(size, size) · Issue #542 · bumptech/glide
https://github.com/bumptech/glide/issues/542

以下、サンプルコードです。