LoginSignup
2
2

More than 3 years have passed since last update.

[Kotlin]glide 備忘録

Posted at

Gradleに

Gradle
dependencies {
  implementation 'com.github.bumptech.glide:glide:4.12.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}

を追加。

MainActivity.kt
class MainActivity : AppCompatActivity() {

    private val image = "https://cdn.pixabay.com/photo/2018/05/03/21/49/android-3372580_960_720.png"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val imageOne = findViewById<ImageView>(R.id.image_one)
        val imageTwo = findViewById<ImageView>(R.id.image_two)
        val imageThree = findViewById<ImageView>(R.id.image_three)

        Glide.with(this)
                .load(image)
                .into(imageOne)

        Glide.with(this)
                .load(image)
                .fitCenter()
                .circleCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageTwo)

        Glide.with(this)
                .load(image)
                .override(300,400)
                .centerCrop()
                .into(imageThree)
    }
}

こんな感じで書ける。

Glide.with(this)
    .load(image)
    .into(imageOne)

これは単純にimageのURLを読み込んで、その中身をactivity_main.xmlのid:imageOneに入れる処理。

Glide.with(this)
    .load(image)
    .fitCenter()
    .circleCrop()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .into(imageTwo)

これはimageOneの処理に加えて、

画像を丸くする処理
画像を真ん中に合わせる処理
全ての解像度のリソースをキャッシュさせる処理
を追加したもの。

Glide.with(this)
    .load(image)
    .override((width)300,(height)400)
    .centerCrop()
    .into(imageThree)

これはimageOneの処理に加えて、画像の処理を追加したもの。
丸みを帯びず、縦長になる感じ。

Glide.with(this)
    .load(image)
    .circleCrop()
    .into(imageThree)

だと画像は丸くなるが、
ここに

.override(width, height)

を書くとcenterCropが無視される?っぽい。

2
2
0

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
2
2