Kotlin 이미지(Image) : 블러링(blurring) 앱바(App Bar)
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var mgv: MyGraphicView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mgv = MyGraphicView(this)
setContentView(mgv)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
super.onCreateOptionsMenu(menu)
menu?.add(0, 1, 0, "NORMAL")
menu?.add(0, 2, 0, "INNER")
menu?.add(0, 3, 0, "OUTER")
menu?.add(0, 4, 0, "SOLID")
menu?.add(0, 5, 0, "원래대로")
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
1 -> {
choice = 1
mgv.invalidate()
return true
}
2 -> {
choice = 2
mgv.invalidate()
return true
}
3 -> {
choice = 3
mgv.invalidate()
return true
}
4 -> {
choice = 4
mgv.invalidate()
return true
}
5 -> {
choice = 5
mgv.invalidate()
return true
}
}
return false
}
}
------------------------------------------------------------------------------------
***** MyGraphicView.kt **
------------------------------------------------------------------------------------
class MyGraphicView(context: Context?) : View(context) {
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val picture = BitmapFactory.decodeResource(resources, R.drawable.durumi)
var picX = (this.width - picture.width) / 2f
var picY = (this.height - picture.height) / 2f
var paint = Paint()
var bMask: BlurMaskFilter
when (choice) {
0 -> canvas?.drawBitmap(picture, picX.toFloat(), picY.toFloat(), null)
//원본대로 읽어와서 표시
1 -> {
bMask = BlurMaskFilter(30f, BlurMaskFilter.Blur.NORMAL)
paint.maskFilter = bMask
canvas.drawBitmap(picture, picX, picY, paint)
//NORMAL
}
2 -> {
bMask = BlurMaskFilter(30f, BlurMaskFilter.Blur.INNER)
paint.maskFilter = bMask
canvas.drawBitmap(picture, picX, picY, paint)
}
3 -> {
bMask = BlurMaskFilter(30f, BlurMaskFilter.Blur.OUTER)
paint.maskFilter = bMask
canvas.drawBitmap(picture, picX, picY, paint)
}
4 -> {
bMask = BlurMaskFilter(30f, BlurMaskFilter.Blur.SOLID)
paint.maskFilter = bMask
canvas.drawBitmap(picture, picX, picY, paint)
}
5 -> {
canvas?.drawBitmap(picture, picX.toFloat(), picY.toFloat(), null)
}
}
picture.recycle() //해당 이미지를 재사용하겠다라는 의미.
}
companion object {
internal var choice = 0
//
}
}
------------------------------------------------------------------------------------
**** strings.xml
------------------------------------------------------------------------------------
<resources>
<string name="app_name">이미지 블러링</string>
</resources>
------------------------------------------------------------------------------------
***** themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.ImageBlurringAppBar" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">#FB8C00</item>
<item name="colorPrimaryDark">#FBF986</item>
</style>
<style name="Theme.ImageBlurringAppBar" parent="Base.Theme.ImageBlurringAppBar" />
</resources>