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
以下、サンプルコードです。
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | |
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/my_image_view" /> | |
</RelativeLayout> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="test.page.facebook.glidetest" > | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
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
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 22 | |
buildToolsVersion "22.0.1" | |
defaultConfig { | |
applicationId "test.page.facebook.glidetest" | |
minSdkVersion 15 | |
targetSdkVersion 22 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile 'com.android.support:appcompat-v7:22.2.0' | |
compile 'com.github.bumptech.glide:glide:3.6.0' | |
} |
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
package test.page.facebook.glidetest; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.ImageView; | |
import com.bumptech.glide.Glide; | |
import com.bumptech.glide.load.resource.drawable.GlideDrawable; | |
import com.bumptech.glide.request.RequestListener; | |
import com.bumptech.glide.request.target.Target; | |
public class MainActivity extends ActionBarActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// ビューを取得 | |
ImageView imageView = (ImageView) findViewById(R.id.my_image_view); | |
// Glideを設定 | |
Glide.with(this) | |
// 画像URL | |
.load("https://40.media.tumblr.com/f49e56a443aecd533fb53d55a1cf1408/tumblr_nsc4fht5ol1u3hv5ko1_1280.jpg") | |
// リスナー(エラーハンドリングをする) | |
.listener(new RequestListener<String, GlideDrawable>() { | |
@Override | |
public boolean onException(Exception e, String s, Target<GlideDrawable> glideDrawableTarget, boolean b) { | |
Log.d("Glide", "Error in Glide listener"); | |
if (e != null) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
@Override | |
public boolean onResourceReady(GlideDrawable glideDrawable, String s, Target<GlideDrawable> glideDrawableTarget, boolean b, boolean b2) { | |
return false; | |
} | |
}) | |
// リサイズ(縦横の最大サイズを指定して、収める) | |
.override(600, 600) | |
// ローディング画像 | |
.placeholder(android.R.drawable.ic_menu_call) | |
// エラー画像 | |
.error(android.R.drawable.ic_delete) | |
// placeholderを設定した場合に必要 これを書かないとplaceholder画像と同じ大きさにリサイズされる | |
.dontAnimate() | |
// imageViewに投入 | |
.into(imageView); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |