Android

이미지뷰(ImageView)에 구멍뚫기

lipnus 2019. 4. 8. 17:47
반응형

이미지뷰(ImageView)에 구멍뚫기




class MainActivity : AppCompatActivity() {

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

val background = BitmapFactory.decodeResource(resources, R.drawable.cat)
var foreground = BitmapFactory.decodeResource(resources, R.drawable.yellow)


foreground = punchAHoleInABitmap(foreground)
imageView.setImageBitmap(combineTwoBitmaps(background, foreground))
}




private fun punchAHoleInABitmap(foreground: Bitmap): Bitmap {

val bitmap = Bitmap.createBitmap(foreground.width, foreground.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val paint = Paint()

canvas.drawBitmap(foreground, 0f, 0f, paint)
paint.isAntiAlias = true
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)

val radius = 600f
Log.d("SSS", "screensize:${getScreenSize()} width:${imageView.measuredWidth}")
val x = getScreenSize().x-100f;
val y = 100f;
canvas.drawCircle(x, y, radius, paint)
return bitmap
}



private fun combineTwoBitmaps(background: Bitmap, foreground: Bitmap): Bitmap {
val combinedBitmap = Bitmap.createBitmap(background.width, background.height, background.config)
val canvas = Canvas(combinedBitmap)
val paint = Paint(Paint.FILTER_BITMAP_FLAG)
canvas.drawBitmap(background, 0f, 0f, paint)
canvas.drawBitmap(foreground, 0f, 0f, paint)
return combinedBitmap
}



private fun getScreenSize(): Point {
val window = getSystemService(WINDOW_SERVICE) as WindowManager
val display = window.defaultDisplay
val size = Point()

display.getSize(size)
return size
}


}


val background = BitmapFactory.decodeResource(resources, R.drawable.cat)
var foreground = BitmapFactory.decodeResource(resources, R.drawable.yellow)

불러오는 이미지는 bmp이어야 한다.

아니면 drawable을 bmp로 바꾸는 작업을 해줘야 한다.




참고: https://www.techrepublic.com/article/punch-a-hole-in-a-bitmap-by-using-androids-porter-duff-xfer/

반응형