LoginSignup
168

More than 5 years have passed since last update.

画像ライブラリGlideの使い方

Last updated at Posted at 2015-01-02

Glide

image

Androidの画像ライブラリといえばSquarePicassoUniversal Image Loaderが有名だが,Googlerの開発しているGlideというライブラリがある。リポジトリのorganizationがbumptechとなっているがこれはGoogleが買収したBumpの企業アカウントだ。

「This is not an official Google product.」と但し書きがついているが,Androidの公式Camera appやGoogle IOのAndroidアプリにも使われている。

Usage

Download

dependencies {
    compile 'com.github.bumptech.glide:glide:3.6.1'
    compile 'com.android.support:support-v4:22.2.1'
}
<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>glide</artifactId>
  <version>3.6.1</version>
  <type>aar</type>
</dependency>
<dependency>
  <groupId>com.google.android</groupId>
  <artifactId>support-v4</artifactId>
  <version>r7</version>
</dependency>

基本的な使い方

Glideは以下の様にして使う事ができる。

@Override
public void onCreate(Bundle savedInstanceState) {
    ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
    Glide.with(this).load("http://goo.gl/h8qOq7").into(imageView);
}

Picassoと似ているので慣れている人であれば使い方はなんとなく分かるだろう。
初めての方は以下のページを参考にしてほしい。

GlideのTransformationsはこちらを,Targetに関してはこちらを参照してほしい。

設定

Picassoと比較して嬉しい所に,Glideは細かな設定をグローバルに設定できるインタフェースがある。

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        Glide.setup(new GlideBuilder(this)
            .setDiskCache(...)
            .setMemoryCache(...)
        );
    }
}

GlideBuilderクラスを利用して以下の項目の設定をする事ができる。詳しくはこちら

  • Disk Cache
  • Memory cache
  • Bitmap pools
  • Bitmap Format

Volley連携

GlideはVolleyと連携する機能を持ちメディアの取得にVolleyを使う事ができる。
使用するには以下の様に記述する。

dependencies {
    compile 'com.github.bumptech.glide:volley-integration:1.1.+'
    compile 'com.mcxiaoke.volley:library:1.0.+'
}
<dependency>
    <groupId>com.github.bumptech.glide</groupId>
    <artifactId>volley-integration</artifactId>
    <version>1.1.0</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>com.mcxiaoke.volley</groupId>
    <artifactId>library</artifactId>
    <version>1.0.5</version>
    <type>aar</type>
</dependency>

ApplicationかActivityで以下のメソッドを呼び出す。

public void onCreate() {
  Glide.get(this).register(GlideUrl.class, InputStream.class,
        new VolleyUrlLoader.Factory(yourRequestQueue));
}

OkHttp連携

Volleyに加え,GlideはOkHttpとの連携もサポートしている。

dependencies {
    compile 'com.github.bumptech.glide:okhttp-integration:1.1.+'
    compile 'com.squareup.okhttp:okhttp:2.0.+'
}
<dependency>
    <groupId>com.github.bumptech.glide</groupId>
    <artifactId>okhttp-integration</artifactId>
    <version>1.1.0</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp</groupId>
    <artifactId>okhttp</artifactId>
    <version>2.0.0</version>
    <type>jar</type>
</dependency>

ApplicationかActivityで以下のメソッドを呼び出す。

public void onCreate() {
  Glide.get(this).register(GlideUrl.class, InputStream.class,
        new OkHttpUrlLoader.Factory(yourOkHttpClient));
}

Picassoとの比較

インタフェースはほぼPicassoと同様(というかパクリ)なのだが,GlideはPicassoよりも高機能で以下の機能をサポートしている。

  • Animated GIF support
    • Picassoではサポートしてないので地味に嬉しい。従来は自前でdecode処理を書いたりWebViewで実現していたものをGlideに置き換えられる。使い方はサンプルにもある。
  • Local video support
    • ローカルにあるvideoもdecodeしてくれるらしい。
  • Thumbnail support
    • サムネイルの表示機能を実装している。
Glide.with(yourFragment).load(yourUrl).thumbnail(0.1f).into(yourView)
  • Animation Support

    • Picassoでアニメーションを実装するのに困った事がある人には嬉しい機能だ。
Animation anim = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
Glide.with(this).load(URL).animate(anim).into(imageView);

尚,英語だがこちらのブログの表が非常に分かりやすい。

まとめ

  • Animated GIFやAnimationを差し込みたい場合はGlideを使ったほうが良い(GifサポートがOOM起こさないかは未検証)。
  • Disk CacheやMemory Cacheの細かな設定をしたい場合はGlideの方が都合が良い。逆に言うとこだわらない場合はPicassoの方が都合が良い。
  • Debug機能はPicassoの方が充実している。
  • ソースコード量はGlideの方が多いので,全量を読まないと使いたくないという人はPicassoの方が良い。
  • サポート体制に関してはPicassoの方が手厚く感じる。Glideは現状@samajuddという人がメインのメンテナである。

Link

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
168