AndroidでURLから画像を表示する方法をメモします。
Glide、Picasso、Coilの表示方法を書くが、Glideの方が扱いやすく、安定し、性能も高いためおすすめです。
Glide を使う方法
xml
<ImageView
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="centerCrop" />
kotlin
val imageView = binding.image
val url = "https://example.com/sample.jpg"
Glide.with(this)
.load(url)
.into(imageView)
Picasso を使う方法
val imageView = binding.image
Picasso.get().load("https://example.com/sample.jpg").into(imageView)
Coil を使う方法
val imageView = binding.image
imageView.load("https://example.com/sample.jpg")