Upgrade to Pro — share decks privately, control downloads, hide ads and more …

謎とき会(Android画像ライブラリ調査会)@2018/02/21

Shion
February 11, 2018

 謎とき会(Android画像ライブラリ調査会)@2018/02/21

コピペ、リンククリック用の補完資料
http://tshion.webcrow.jp/itlog/index.html?id=20180222125216

Shion

February 11, 2018
Tweet

More Decks by Shion

Other Decks in Programming

Transcript

  1. Picasso とは Square 社製の画像ライブラリ。 手軽に画像を扱える ようになるので、 とても便利!! 特にサー バー にある画像の表示がすごく楽になる

    導入前 導入後 画像を通信で取得して、 Bitmap 等で加工して、UI に 反映…… URL を指定するだけ で、 あとは勝手にUI に反映 ※ 使わない時の実装は下記ブログのようになります。 【Android プログラミング入門 #007】 ネット上の画 像を取得しImageView に表示する 10
  2. 2. レイアウトファイルをいじる activity_main.xml を修正します。 下記をコピペして、 書き換えてください。 <?xml version="1.0" encoding="utf-8"?> <FrameLayout

    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout> 15
  3. 3. gradle をいじる app/build.gradle にPicasso を導入するための記述を 追加します。 dependencies { implementation

    fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-la testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1 androidTestImplementation 'com.android.support.test.espresso // Picasso compile 'com.squareup.picasso:picasso:2.5.2' } 16
  4. 5. コー ドをいじる( サー バー 画像編) MainActivity に画像を表示するためのコー ドを書い ていきます。

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 画像の表示場所の取得 ImageView imageView = findViewById(R.id.image); // 画像の読み込み Picasso.with(this) .load("http://i.imgur.com/DvpvklR.png") .into(imageView); } 18
  5. 5. コー ドをいじる( ロー カル画像編) mipmap-xxxhdpi/ic_launcher.png をdrawable フォル ダー にコピー

    します。 ※load() にmipmap を指定してうまくいかなかったの で……。 21
  6. 5. コー ドをいじる( ロー カル画像編) MainActivity に画像を表示するためのコー ドを書い ていきます。 @Override

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 画像の表示場所の取得 ImageView imageView = findViewById(R.id.image); // 画像の読み込み Picasso.with(this) .load(R.drawable.ic_launcher) .into(imageView); } 22
  7. Tips 2 画像パスの指定方法で落ちる場合があるので注意! OK! String path = null; Picasso.with(context) .load(path)

    .into(imageView); NG! (IllegalArgumentException) String path = ""; Picasso.with(context) .load(path) .into(imageView); 25
  8. Tips 3 t() は画像表示領域の大きさが決まっている必要があ るので気をつけて! // 画像の表示場所の取得 ImageView imageView =

    findViewById(R.id.image); // 画像の読み込み Picasso.with(this) .load(R.drawable.ic_launcher) .fit() .into(imageView); 26
  9. 参考文献 DroidKaigi2017 に参加して感じた今後のAndroid 開発 Picasso の公式ペー ジ Picasso のGitHub Glide

    のGitHub Fresco のGitHub 【Android プログラミング入門 #007】 ネット上の 画像を取得しImageView に表示する 31