반응형

[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
package com.cookandroid.project13_2
 
import android.content.Context
import android.media.MediaActionSound
import android.media.MediaPlayer
import android.os.Build.VERSION_CODES.M
import android.os.Build.VERSION_CODES.O
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.os.SystemClock
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.SeekBar
import android.widget.Toast
import androidx.appcompat.widget.AppCompatCheckedTextView
import androidx.core.app.ActivityCompat
import com.cookandroid.project13_2.R
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.text.SimpleDateFormat
 
class MainActivity : AppCompatActivity() {
 
    lateinit var mPlayer : MediaPlayer
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "직접 풀어보기 13-2"
 
        mPlayer = MediaPlayer.create(this, R.raw.song1)
        seekBar1.max = mPlayer.duration
 
 
        switch1.setOnClickListener {
 
            if(switch1.isChecked == true) {
                mPlayer = MediaPlayer.create(this, R.raw.song1)
                mPlayer.seekTo(seekBar1.progress)
                mPlayer.start()
 
                object : Thread(){
                    override fun run(){
                        if(mPlayer == null)
                            return
                        while(mPlayer.isPlaying){
                            seekBar1.progress = mPlayer.currentPosition
                            SystemClock.sleep(200)
                        }
 
                    }
                }.start()
            }else{
                seekBar1.progress = 0
                mPlayer.stop()
                mPlayer.reset()
            }
        }
 
        seekBar1.setOnSeekBarChangeListener(
            object : SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    if(fromUser){
                        if(switch1.isChecked) {
                            mPlayer.pause()
                            mPlayer.seekTo(progress)
                            mPlayer.start()
                        }
                    }
                }
 
                override fun onStartTrackingTouch(seekBar: SeekBar?) {
 
                }
 
                override fun onStopTrackingTouch(seekBar: SeekBar?) {
 
                }
 
            }
        )
    }
}
cs

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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">
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="음악 듣기"
        android:id="@+id/switch1"/>
    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:padding="20dp"
        android:id="@+id/seekBar1"/>
 
 
</LinearLayout>
cs
반응형
반응형

[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
package com.cookandroid.project13_1
 
import android.content.Context
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.widget.AppCompatCheckedTextView
import androidx.core.app.ActivityCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
 
class MainActivity : AppCompatActivity() {
 
    lateinit var mp3List : ArrayList<String>
    lateinit var selectedMP3 : String
 
    var mp3Path = Environment.getExternalStorageDirectory().path + "/"
    lateinit var mPlayer : MediaPlayer
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "간단 MP3 플레이어"
 
        ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 0)
 
        mp3List = ArrayList()
 
        var listFiles = File(mp3Path).listFiles()
        var fileName : String
        var extName : String
        for (file in listFiles!!){
            fileName = file.name
            extName = fileName.substring(fileName.length - 3)
            if(extName == "mp3")
                mp3List.add(fileName)
 
        }
 
        var adapter = ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, mp3List)
        listViewMP3.choiceMode = ListView.CHOICE_MODE_SINGLE
        listViewMP3.adapter = adapter
        listViewMP3.setItemChecked(0, true)
 
        listViewMP3.setOnItemClickListener { parent, view, position, id ->
            selectedMP3 = mp3List[position]
        }
        selectedMP3 = mp3List[0]
 
        btnPlay.setOnClickListener {
            mPlayer = MediaPlayer()
            mPlayer.setDataSource(mp3Path + selectedMP3)
            mPlayer.prepare()
            mPlayer.start()
            btnPlay.isClickable = false
            btnStop.isClickable = true
            btnPause.isClickable =  true
            tvMP3.text = "실행중인 음악 : ${selectedMP3}"
            pbMP3.visibility = View.VISIBLE
 
        }
        btnPause.setOnClickListener {
            if(btnPause.text == "일시정지"){
                btnPause.text = "이어듣기"
                mPlayer.pause()
            }else{
                btnPause.text = "일시정지"
                mPlayer.start()
 
            }
        }
        btnStop.setOnClickListener {
            mPlayer.stop()
            mPlayer.reset()
            btnPlay.isClickable = true
            btnStop.isClickable = false
            btnPause.isClickable = false
            btnPause.text = "일시정지"
            tvMP3.text = "실행중인 음악 : "
            pbMP3.visibility = View.INVISIBLE
        }
        btnStop.isClickable = false
        btnPause.isClickable = false
 
    }
}
cs

<activity_main.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
<?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:background="@color/purple_200"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listViewMP3">
        </ListView>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnPlay"
            android:text="듣기"/>
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnPause"
            android:text="일시정지"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnStop"
            android:text="중지"/>
 
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvMP3"
            android:text="실행중인 음악:"/>
        <ProgressBar
            android:id="@+id/pbMP3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"/>
 
    </LinearLayout>
 
</LinearLayout>
cs
반응형
반응형

[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
package com.cookandroid.project12_2
 
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    lateinit var myHelper : myDBHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        title = "가수 그룹 관리 DB"
        myHelper = myDBHelper(this)
        btnInit.setOnClickListener {
            var sqlDB = myHelper.writableDatabase
            myHelper.onUpgrade(sqlDB, 1, 2)
            sqlDB.close()
        }
 
        btnInsert.setOnClickListener {
            var sqlDB = myHelper.writableDatabase
            sqlDB.execSQL("INSERT INTO groupTBL VALUES('"+ edtName.text.toString() + "' ,"+edtNumber.text.toString() + " ); "  )
            sqlDB.close()
            Toast.makeText(applicationContext, "입력됨", Toast.LENGTH_SHORT).show()
        }
        btnSelect.setOnClickListener {
            var sqlDB = myHelper.readableDatabase
            var cursor = sqlDB.rawQuery("SELECT * FROM groupTBL;", null)
 
            var strNames = "그룹이름" + "\r\n" + "-----" +"\r\n"
            var strNumbers = "인원" + "\r\n" + "-----" +"\r\n"
 
            while(cursor.moveToNext()){
                strNames += cursor.getString(0) + "\r\n"
                strNumbers += cursor.getString(1) + "\r\n"
            }
 
            tvNameResult.text = strNames
            tvNumberResult.text = strNumbers
 
            cursor.close()
            sqlDB.close()
 
        }
 
        btnUpdate.setOnClickListener {
            var sqlDB = myHelper.writableDatabase
            var afterNumber = edtNumber.text.toString().toInt()
            var targetName = edtName.text.toString()
            sqlDB.execSQL("UPDATE groupTBL SET gNumber = '${afterNumber}' WHERE gName = '${targetName}';")
 
            Toast.makeText(applicationContext, "수정됨", Toast.LENGTH_SHORT).show()
            btnSelect.callOnClick()
        }
 
        btnDelete.setOnClickListener {
            var sqlDB = myHelper.writableDatabase
            var targetName = edtName.text.toString()
            sqlDB.execSQL("DELETE FROM groupTBL WHERE gName = '${targetName}';")
            Toast.makeText(applicationContext, "삭제됨", Toast.LENGTH_SHORT).show()
            btnSelect.callOnClick()
        }
 
    }
 
    inner class myDBHelper(context: Context) :SQLiteOpenHelper(context, "groupDB", null, 1){
        override fun onCreate(db: SQLiteDatabase?) {
            db!!.execSQL("CREATE TABLE groupTBL(gName CHAR(20) PRIMARY KEY, gNumber INTEGER)")
        }
 
        override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
            db!!.execSQL("DROP TABLE IF EXISTS groupTBL")
            onCreate(db)
        }
 
    }
}
cs

 

<activity_main.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
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
97
<?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_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="이름 : "/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/edtName"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="인원 : "/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/edtNumber"/>
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
 
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="초기"
            android:id="@+id/btnInit"/>
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="입력"
            android:id="@+id/btnInsert"/>
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="수정"
            android:id="@+id/btnUpdate"/>
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="삭제"
            android:id="@+id/btnDelete"/>
 
        <Button
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="조회"
            android:id="@+id/btnSelect"/>
 
 
    </LinearLayout>
    <LinearLayout
        android:gravity="center"
        android:background="#00FF00"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/tvNameResult"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/tvNumberResult"/>
    </LinearLayout>
 
</LinearLayout>
cs
반응형
반응형

Spinner의 onItemSelectedListener에 AdapterView.OnItemSelectedListener을 대입하는 방법은 

무명 클래스 인스턴스 Object를 사용해서 onItemSelected, onNothingSelected 을 오버라이딩하면 됩니다.

(오버라이딩해야 될 메서드가 2개 이므로 람다식을 사용할 수 없다)


spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
              //todo

 

            }
 
            override fun onNothingSelected(parent: AdapterView<*>?) {

               //todo

            }
반응형
반응형

Spinner의 onItemSelectedListener에 AdapterView.OnitemSelectedListener 람다식을 대입하는 방법은

Object 객체를 만들어서 onItemSelected, onNothingSelected 을 오버라이딩하면 됩니다.

spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                ivPoster.setImageResource(posterID[position])
            }
 
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
 

 

[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
package com.cookandroid.project11_3
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "스피너 테스트"
 
        var movie = arrayOf(
            "영화 제목1", "영화 제목2", "영화 제목3", "영화 제목4",
            "영화 제목5", "영화 제목6", "영화 제목7", "영화 제목8",
            "영화 제목9", "영화 제목10"
        )
 
        var posterID = arrayOf(
            R.drawable.mov01, R.drawable.mov02, R.drawable.mov03, R.drawable.mov04,
            R.drawable.mov05, R.drawable.mov06, R.drawable.mov07, R.drawable.mov08,
            R.drawable.mov09, R.drawable.mov10,
        )
 
        var adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, movie)
        spinner1.adapter = adapter
        spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                ivPoster.setImageResource(posterID[position])
            }
 
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
 
        }
    }
}
cs

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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">
 
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner1"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:id="@+id/ivPoster"/>
 
</LinearLayout>
cs
반응형
반응형

[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
package com.cookandroid.project11_2
 
import android.annotation.SuppressLint
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.Gallery
import android.widget.ImageView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toast1.view.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        var galAdapter = MyGalleryAdapter(this)
        gallery1.adapter = galAdapter
    }
 
    inner class MyGalleryAdapter(var context: Context) : BaseAdapter(){
        var posterID = arrayOf(
            R.drawable.mov11, R.drawable.mov12, R.drawable.mov13, R.drawable.mov14,
            R.drawable.mov15, R.drawable.mov16, R.drawable.mov17, R.drawable.mov18,
            R.drawable.mov19, R.drawable.mov20,
        )
 
        var posterName = arrayOf(
            "영화 제목1""영화 제목2""영화 제목3""영화 제목4",
            "영화 제목5""영화 제목6""영화 제목7""영화 제목8",
            "영화 제목9""영화 제목10",
        )
 
        override fun getCount(): Int {
            return posterID.size
        }
 
        override fun getItem(position: Int): Any {
            return 0
        }
 
        override fun getItemId(position: Int): Long {
            return 0;
        }
 
        @SuppressLint("ClickableViewAccessibility")
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var imageview = ImageView(context)
            imageview.layoutParams = Gallery.LayoutParams(200300)
            imageview.scaleType = ImageView.ScaleType.FIT_CENTER
            imageview.setPadding(5555)
            imageview.setImageResource(posterID[position])
 
 
            imageview.setOnTouchListener { v, event ->
                ivPoster.scaleType = ImageView.ScaleType.FIT_CENTER
                ivPoster.setImageResource(posterID[position])
 
                var toast = Toast(this@MainActivity)
                var toastView = View.inflate(this@MainActivity, R.layout.toast1, null)
 
                toastView.toastText1.text = posterName[position]
                toast.view = toastView
                toast.show()
 
                false
 
            }
            return imageview
        }
 
    }
}
cs

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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">
 
    <Gallery
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gallery1"
        android:spacing="5dp"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ivPoster"
        android:padding="20dp"/>
 
 
</LinearLayout>
cs

<toast1.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
<?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="horizontal"
    android:gravity="center"
    android:background="#0000FF">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/movie_icon"/>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:id="@+id/toastText1"
        android:textColor="@color/white"
        android:textSize="20dp"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/movie_icon"/>
 
</LinearLayout>
cs
반응형
반응형

[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
package com.cookandroid.project11_1
 
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.dialog.view.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "그리드뷰 영화 포스터"
 
        var gAdapter = MyGridAdapter(this)
        gridView1.adapter = gAdapter
 
    }
 
    inner class MyGridAdapter(var context: Context) : BaseAdapter(){
        var posterID = arrayOf(
            R.drawable.mov01, R.drawable.mov02, R.drawable.mov03, R.drawable.mov04,
            R.drawable.mov05, R.drawable.mov06, R.drawable.mov07, R.drawable.mov08,
            R.drawable.mov09, R.drawable.mov10,
            R.drawable.mov01, R.drawable.mov02, R.drawable.mov03, R.drawable.mov04,
            R.drawable.mov05, R.drawable.mov06, R.drawable.mov07, R.drawable.mov08,
            R.drawable.mov09, R.drawable.mov10,
            R.drawable.mov01, R.drawable.mov02, R.drawable.mov03, R.drawable.mov04,
            R.drawable.mov05, R.drawable.mov06, R.drawable.mov07, R.drawable.mov08,
            R.drawable.mov09, R.drawable.mov10,
            R.drawable.mov01, R.drawable.mov02, R.drawable.mov03, R.drawable.mov04,
            R.drawable.mov05, R.drawable.mov06, R.drawable.mov07, R.drawable.mov08,
            R.drawable.mov09, R.drawable.mov10,
        )
        var posterName = arrayOf(
            "영화 제목1", "영화 제목2", "영화 제목3", "영화 제목4",
            "영화 제목 5", "영화 제목6", "영화 제목7", "영화 제목8",
            "영화 제목9", "영화 제목10"
        )
 
        override fun getCount(): Int {
            return posterID.size
        }
 
        override fun getItem(position: Int): Any {
            return 0
        }
 
        override fun getItemId(position: Int): Long {
            return 0
        }
 
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var imageview = ImageView(context)
            imageview.layoutParams = ViewGroup.LayoutParams(200, 300)
            imageview.scaleType = ImageView.ScaleType.FIT_CENTER
            imageview.setPadding(5, 5, 5, 5)
            imageview.setImageResource(posterID[position])
 
 
            imageview.setOnClickListener{
                var dialogView = View.inflate(this@MainActivity, R.layout.dialog, null)
                var dlg = AlertDialog.Builder(this@MainActivity)
                dialogView.ivPoster.setImageResource(posterID[position])
                dlg.setTitle(posterName[position])
                dlg.setIcon(R.drawable.movie_icon)
                dlg.setView(dialogView)
                dlg.setNegativeButton("닫기", null)
                dlg.show()
            }
            return imageview
        }
 
    }
 
 
}
cs

<activity_main.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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">
 
    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gridView1"
        android:gravity="center"
        android:numColumns="4">
    </GridView>
 
 
</LinearLayout>
cs

<dialog.xml>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivPoster"/>
 
 
 
</LinearLayout>
cs
반응형
반응형

[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