반응형

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

<MainActivity>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.cookandroid.project9_2
 
import android.content.Context
import android.graphics.*
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    lateinit var graphicView : MyGraphicView
    companion object{
        var sX = 1f;
        var sY = 1f;
        var angle = 0f;
        var color = 1f;
        var satur = 1f
        var onBlur = false
        var onEmbos = false
 
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        graphicView = MyGraphicView(this)
        pictureLayout.addView(graphicView)
        clickIcons()
    }
 
    inner class MyGraphicView(context: Context) :View(context){
        override fun onDraw(canvas: Canvas) {
            super.onDraw(canvas)
            val cenX = this.width / 2f
            val cenY = this.height / 2f
            canvas.scale(sX, sX, 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)
            cm.setSaturation(satur)
            paint.colorFilter = ColorMatrixColorFilter(cm)
            if(onBlur == true) paint.maskFilter = BlurMaskFilter(30f, BlurMaskFilter.Blur.NORMAL)
            if(onEmbos == true) paint.maskFilter = EmbossMaskFilter(floatArrayOf(3f, 3f, 3f), 0.5f, 5f, 10f)
 
            val picture = BitmapFactory.decodeResource(resources, R.drawable.lena256)
            val picX = (this.width - picture.width) / 2f
            val picY = (this.height - picture.height) /2f
            canvas.drawBitmap(picture, picX, picY, paint)
            picture.recycle()
        }
    }
    private fun clickIcons() {
        ibZoomin.setOnClickListener {
            sX += 0.2f
            sY += 0.2f
            graphicView.invalidate()
 
        }
        ibZoomout.setOnClickListener {
            sX -= 0.2f
            sY -= 0.2f
            graphicView.invalidate()
        }
        ibRotate.setOnClickListener {
            angle += 20f
            graphicView.invalidate()
        }
        ibBright.setOnClickListener {
            color += 0.2f
            satur += 0.2f
            graphicView.invalidate()
        }
        ibDark.setOnClickListener {
            color -= 0.2f
            satur -= 0.2f
            if(satur <= 0f) satur = 0f
            graphicView.invalidate()
        }
        ibBlur.setOnClickListener {
            onBlur = !onBlur
            graphicView.invalidate()
        }
        ibEmbos.setOnClickListener {
            onEmbos = !onEmbos
            graphicView.invalidate()
        }
    }
 
}
cs

<activity.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:id="@+id/iconLayout"
        android:orientation="horizontal"
        android:gravity="center">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibZoomin"
            android:src="@drawable/zoom_in"/>
 
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibZoomout"
            android:src="@drawable/zoom_out"/>
 
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibRotate"
            android:src="@drawable/rotate"/>
 
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibBright"
            android:src="@drawable/bright"/>
 
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibDark"
            android:src="@drawable/dark"/>
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibBlur"
            android:src="@drawable/blur"/>
 
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibEmbos"
            android:src="@drawable/embos"/>
 
 
    </LinearLayout>
 
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:id="@+id/pictureLayout"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_weight="9">
 
    </LinearLayout>
</LinearLayout>
cs
반응형

+ Recent posts