이미지뷰(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/
'Android' 카테고리의 다른 글
Android App Refactoring (0) | 2023.04.02 |
---|---|
Android Room 데이터베이스 예제 (0) | 2019.04.30 |
루팅된 앱에서의 접속 차단하기(RootBeer) (0) | 2019.04.25 |
WifiManager로 접속변경 시 이전 와이파이에 다시 붙는 문제 (0) | 2019.03.12 |
Thread (0) | 2019.03.11 |