[코틀린을 활용한 안드로이드 프로그래밍] 8장 직접 풀어보기 8-2

2022. 5. 25. 19:14·안드로이드 프로그래밍/코틀린
반응형

[Gradle 설정]

1
id 'kotlin-android-extensions'
cs

 

[AndroidManifest.xml]

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 추가

 

<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
package com.cookandroid.project8_2
 
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.cookandroid.project8_2.myPictureView
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
 
class MainActivity : AppCompatActivity() {
    var imageFiles : Array<File>? = null
    lateinit var imageFname : String
    var curNum  = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "간단한 이미지 뷰어"
        ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), Context.MODE_PRIVATE)
 
        imageFiles = File(Environment.getExternalStorageDirectory().absolutePath + "/Pictures").listFiles()
        imageFname = imageFiles!![0].toString()
        myPictureView.imagePath = imageFname
        tv.setText("1/${imageFiles!!.size - 1}")
 
        btnPrev.setOnClickListener {
            if(curNum <= 0){
                curNum = imageFiles!!.size - 2
            }else{
                curNum--
            }
            tv.setText("${curNum + 1}/${imageFiles!!.size -1}")
            imageFname = imageFiles!![curNum].toString()
            myPictureView.imagePath = imageFname
            myPictureView.invalidate()
        }
        btnNext.setOnClickListener {
            if(curNum == imageFiles!!.size -2){
                curNum = 0
            }else{
                curNum++
            }
            tv.setText("${curNum + 1}/${imageFiles!!.size -1}")
            imageFname = imageFiles!![curNum].toString()
            myPictureView.imagePath = imageFname
            myPictureView.invalidate()
        }
 
 
    }
}
Colored by Color Scripter
cs

<myPictureView>

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
package com.cookandroid.project8_2
 
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.View
 
class myPictureView(context: Context, attrs: AttributeSet?): View(context, attrs) {
 
    var imagePath : String? = null
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        try{
            if(imagePath != null){
                var bitmap = BitmapFactory.decodeFile(imagePath)
                canvas!!.scale(2f, 2f, 0f, 0f)
                canvas!!.drawBitmap(bitmap!!, 0f, 0f, null)
                bitmap!!.recycle()
 
            }
        }catch(e :Exception){
 
        }
    }
}
Colored by Color Scripter
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
<?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="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnPrev"
            android:text="이전 그림"
            android:layout_weight="1"/>
        <TextView
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0/0"
            android:id="@+id/tv"/>
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnNext"
            android:text="다음 그림"
            android:layout_weight="1"/>
 
    </LinearLayout>
    <com.cookandroid.project8_2.myPictureView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myPictureView"/>
 
 
</LinearLayout>
Colored by Color Scripter
cs
반응형
저작자표시 (새창열림)

'안드로이드 프로그래밍 > 코틀린' 카테고리의 다른 글

[코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-3  (0) 2022.05.26
[코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-2  (0) 2022.05.26
[코틀린을 활용한 안드로이드 프로그래밍] 8장 직접 풀어보기 8-1  (0) 2022.05.25
[코틀린을 활용한 안드로이드 프로그래밍] 6장 직접 풀어보기 6-3  (0) 2022.05.25
[코틀린을 활용한 안드로이드 프로그래밍] 6장 직접 풀어보기 6-2  (0) 2022.05.22
'안드로이드 프로그래밍/코틀린' 카테고리의 다른 글
  • [코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-3
  • [코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-2
  • [코틀린을 활용한 안드로이드 프로그래밍] 8장 직접 풀어보기 8-1
  • [코틀린을 활용한 안드로이드 프로그래밍] 6장 직접 풀어보기 6-3
슥지니
슥지니
개발 블로그
  • 슥지니
    슥지니의 코딩노트
    슥지니
  • 전체
    오늘
    어제
    • 분류 전체보기 (199)
      • 알고리즘 문제풀이 (158)
        • 백준 (158)
      • 알고리즘 (6)
      • Node.js (2)
        • MongoDB (1)
        • 기타 (1)
      • spring (0)
      • 가상화폐 (1)
        • 바이낸스(Binance) (1)
      • C++ 테트리스 게임 (1)
      • C++ (10)
      • 안드로이드 프로그래밍 (21)
        • 코틀린 (21)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    코틀린
    시뮬레이션
    콘솔
    그래프
    dfs
    알고리즘
    구현
    Kotlin
    그리디
    백준
    C++
    우선순위 큐
    BFS
    자료구조
    C
    코틀린을 활용한 안드로이드 프로그래밍
    콘솔 테트리스 게임
    백트랙킹
    다이나믹 프로그래밍
    dp
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
슥지니
[코틀린을 활용한 안드로이드 프로그래밍] 8장 직접 풀어보기 8-2
상단으로

티스토리툴바