if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
when {
ContextCompat.checkSelfPermission( //이미 앱에 특정권한을 부여했는지 확인하기 위해
this, android.Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED -> { // 권한이 있을시, 갤러리에서 사진을 선택하는 함수
navigatePhotos()
} // 권한이 없을시, 교육용 팝업 확인 후 권한 팝업을 띄움
shouldShowRequestPermissionRationale(android.Manifest.permission.READ_EXTERNAL_STORAGE) -> {
showPermissionContextPopup()
}
else -> { // 이외의 경우, 권한을 요청하는 팝업을 띄움
requestPermissions( //여러 권한들을 요청
arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE), // 그 중, READ_EXTERNAL_STORAGE이라는 하나만 요청
1000// 그 요청의 키값을 1000으로 지정
)
}
}
}
< uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
// 이 앱이 READ_EXTERNAL_STORAGE 권한이 필요하다는것을 정의함
app:layout_constraintDimensionRatio="H, 3:1">//세로를 가로 축 기준으로 3:1비율로
이미지의 크기 조정
android:scaleType="centerCrop" // 이미지 뷰를 넘어가는부분은 잘라냄
private val imageViewList:List<ImageView> by lazy {
mutableListOf<ImageView>().apply {
add(findViewById(R.id.imageView1_1))
add(findViewById(R.id.imageView1_2))
add(findViewById(R.id.imageView1_3))
add(findViewById(R.id.imageView2_1))
add(findViewById(R.id.imageView2_2))
add(findViewById(R.id.imageView2_3))
}
}
// 권한이 있을시, 갤러리에서 사진을 선택하는 함수
private fun navigatePhotos() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*" //모든 이미지 파일들을 가져옴
startActivityForResult(intent, 2000)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// intent가 null?인것은, 혹시라도 데이터가 없을때 오류가 발생할수있음
private val imageUriList: MutableList<Uri> = mutableListOf()
imageUriList.forEachIndexed {index, uri ->
intent.putExtra("photo$index", uri.toString())//각각의 키를 photo번호로 가져오기위해서
}
intent.putExtra("photoListSize", imageUriList.size)//몇번째까지 가져와야하는지
startActivity(intent)
for ( i in 0..size) {
intent.getStringExtra("photo$i")?.let {
photoList.add(Uri.parse(it)) //Uri 객체로 다시 변환
}
private fun startTimer() {
timer = timer(period = 5 * 1000) {
runOnUiThread {
val current = currentPosition
val next =
if (photoList.size <= currentPosition + 1) 0 else currentPosition + 1//마지막일때는 다시 첫번쨰 순서로 돌아옴
backgroundPhotoImageView.setImageURI(photoList[current])
photoImageView.alpha = 0f //alpha는 투명도, 0f는 완전투명해서 아예 보이지않음
photoImageView.setImageURI(photoList[next])
//이미지가 서서히 나타남
photoImageView.animate()
.alpha(1.0f)
.setDuration(1000) //1초 동안 발생
.start()
currentPosition = next
}
}
}
override fun onStop() {//activity가 백그라운드로 들어가서 더이상 사용되지않는 상태
super.onStop()
timer?.cancel()
}
override fun onStart() { // onStop에서는 onCreate로 넘어가지 않고, onStart로 오기때문에 여기서 타이머 시작
super.onStart()
startTimer()
}
override fun onDestroy() { //앱이 완전 종료된 상태
super.onDestroy()
timer?.cancel()
}
android:screenOrientation="landscape"