Skip to content

FlexibleImageView

Sieun Ju edited this page Feb 1, 2022 · 3 revisions

์š”์•ฝ (Summary)

  • ์ œ์Šค์ฒ˜๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ Move, Scale, Rotate ์ฒ˜๋ฆฌ ํ• ์ˆ˜ ์žˆ๋Š” ImageView ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
  • FlexibleImageView๋Š” ์ ˆ๋Œ€ ๋‹จ๋…์ ์œผ๋กœ ์žˆ์œผ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค. FrameLayout ์ด๋‚˜ ConstraintLayout ์„ ๊ฐ์‹ธ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ (Supported Features)

  • ์‚ฌ์šฉ์ž ์ œ์Šค์ฒ˜๋ฅผ ํ†ตํ•œ Move, Scale, Rotate
  • ํ•ด๋‹น ๋ทฐ ๋ฒ”์œ„ ๋ฐ–์œผ๋กœ ๋„˜์–ด๊ฐ”์„์‹œ ๋‹ค์‹œ ๋˜๋Œ์•„ ์˜ค๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜
  • ์ด๋ฏธ์ง€ ์บก์ฒ˜ ๊ธฐ๋Šฅ

Public ํ•จ์ˆ˜ ์„ค๋ช…

  • loadUrl (url : String)
    • Function Parameter
      • url (String) ๋กœ๋“œ ํ•˜๊ณ  ์‹ถ์€ ์™ธ๋ถ€ ์ด๋ฏธ์ง€ ์ฃผ์†Œ๋‚˜ ๊ฐค๋Ÿฌ๋ฆฌ ์ด๋ฏธ์ง€ URL
    • ๋กœ๋“œํ•˜๊ณ ์‹ถ์€ URL ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋ฏธ์ง€๋ทฐ์— ๋ Œ๋”๋ง ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • centerCrop
    • ์ด๋ฏธ์ง€ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ FlexibleImageView ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ๋น„์œจ์— ๋งž๊ฒŒ ๊ฝ‰์ฑ„์šฐ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • fitCenter
    • ์ด๋ฏธ์ง€ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ FlexibleImageView ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ๋‘˜์ค‘ ํ•˜๋‚˜์— ๋งž์ถฐ์„œ ํ™•๋Œ€ ์ฒ˜๋ฆฌ ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • getStateItem: RectF
    • ํ˜„์žฌ ์ด๋ฏธ์ง€ ์ขŒํ‘œ๊ฐ’์„ ๋ฆฌํ„ด ํ•˜๋Š” ํ•จ์ˆ˜

ํ™•์žฅ ํ•จ์ˆ˜ ์„ค๋ช…

  • captureBitmap() : Bitmap

    • FlexibleImageView๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋ทฐ ๊ธฐ์ค€์œผ๋กœ ์บก์ฒ˜ํ•˜์—ฌ Bitmap ์œผ๋กœ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • backgroundCaptureBitmap() : Bitmap

    • FlexibleImageView๋ฅผ ํ†ตํ•ด ์›€์ง์ธ ์ด๋ฏธ์ง€ ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ๋กœ ์บก์ฒ˜ ํ• ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆฉ๋‹ˆ๋‹ค.
    • ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ด๋ฏธ์ง€๋“ค์„ ์›€์ง์ธ ์ขŒํ‘œ, ์Šค์ผ€์ผ ๊ฐ’๋งŒ ๋ฏธ๋ฆฌ ์ €์žฅํ•ด๋‘๊ณ  ๋‚˜์ค‘์— Bitmap ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ• ๋•Œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.
    Glide.with(requireContext())
                    .asDrawable()
                    .load(imageUrl)
                    .into(object : CustomTarget<Drawable?>() {
    
                        override fun onResourceReady(
                            resource: Drawable,
                            transition: Transition<in Drawable?>?
                        ) {
                            val bitmapDrawable = resource as BitmapDrawable
                            GlobalScope.launch {
                                val bitmap = withContext(Dispatchers.IO) {
                                    backgroundCaptureBitmap(
                                        bitmapDrawable.bitmap,
                                        stateItem,
                                        flexibleCaptureView.width,
                                        flexibleCaptureView.height
                                    )
                                }
                                withContext(Dispatchers.Main) {
                                    view.findViewById<AppCompatImageView>(R.id.imgCapture)
                                        .setImageBitmap(bitmap)
                                }
                            }
                        }
    
                        override fun onLoadCleared(placeholder: Drawable?) {
                        }
                    })

์‚ฌ์šฉ ์บก์ฒ˜ ํ™”๋ฉด

FlexibleImageView