Kotlin 이미지(Image) : 미니 포토샵 앱 만들기
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/iconLayout"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/ibZoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/zoom_in" />
<ImageButton
android:id="@+id/ibZoomout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/zoom_out" />
<ImageButton
android:id="@+id/ibRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rotate" />
<ImageButton
android:id="@+id/ibBright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bright" />
<ImageButton
android:id="@+id/ibDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dark" />
<ImageButton
android:id="@+id/ibGray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gray" />
</LinearLayout>
<LinearLayout
android:id="@+id/pictureLayout"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="9"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
lateinit var ibZoomin : ImageButton
lateinit var ibZoomout : ImageButton
lateinit var ibRotate : ImageButton
lateinit var ibBright : ImageButton
lateinit var ibDark : ImageButton
lateinit var ibGray : ImageButton
private lateinit var graphicView : MyGraphicView
companion object {
var sX = 1f
var sY = 1f
var angle = 0f
var color = 1f
var satur = 1f
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//title = "미니 포토샵"
var pictureLayout = findViewById<LinearLayout>(R.id.pictureLayout)
graphicView = MyGraphicView(this)
pictureLayout.addView(graphicView)
clickIcons()
}
fun clickIcons() {
ibZoomin = findViewById<ImageButton>(R.id.ibZoomin)
ibZoomin.setOnClickListener {
sX = sX + 0.2f
sY = sY + 0.2f
graphicView.invalidate()
}
ibZoomout = findViewById<ImageButton>(R.id.ibZoomout)
ibZoomout.setOnClickListener {
sX = sX - 0.2f
sY = sY - 0.2f
graphicView.invalidate()
}
ibRotate = findViewById<ImageButton>(R.id.ibRotate)
ibRotate.setOnClickListener {
angle = angle + 20
graphicView.invalidate()
}
ibBright = findViewById<ImageButton>(R.id.ibBright)
ibBright.setOnClickListener {
color = color + 0.2f
graphicView.invalidate()
}
ibDark = findViewById<ImageButton>(R.id.ibDark)
ibDark.setOnClickListener {
color = color - 0.2f
graphicView.invalidate()
}
ibGray = findViewById<ImageButton>(R.id.ibGray)
ibGray.setOnClickListener {
if (satur == 0f)
satur = 1f
else
satur = 0f
graphicView.invalidate()
}
}
class MyGraphicView(context: Context) : View(context) {
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
var cenX = this.width / 2f
var cenY = this.height / 2f
canvas.scale(sX, sY, cenX, cenY)
canvas.rotate(angle, cenX, cenY)
val paint = Paint()
val array = floatArrayOf( color, 0f, 0f, 0f, 0f,
0f, color, 0f, 0f, 0f,
0f, 0f, color, 0f, 0f,
0f, 0f, 0f, 1f, 0f)
val cm = ColorMatrix(array)
if (satur == 0f) cm.setSaturation(satur)
paint.colorFilter = ColorMatrixColorFilter(cm)
var picture = BitmapFactory.decodeResource(resources,R.drawable.lena256)
var picX = (this.width - picture.width) / 2f
var picY = (this.height - picture.height) / 2f
canvas.drawBitmap(picture, picX, picY, paint)
picture.recycle()
}
}
}