와챠의 우당탕탕 개발 기록장

[안드로이드] 직접 풀어보기 6-2(뷰플리퍼로 자동 사진 넘기기) 본문

코딩 일기장/Android(Kotlin)

[안드로이드] 직접 풀어보기 6-2(뷰플리퍼로 자동 사진 넘기기)

minWachya 2021. 3. 27. 11:48
반응형

직접 풀어보기 6-2

뷰 플리퍼를 이용하여 자동 사진 보기 앱을 작성하라

  • 적절한 이미지 여러 장이 자동으로 넘어가는 앱을 만든다.
  • <사진 보기 시작>과 <사진 보기 정지>를 만들고, <사진 보기 시작>을 클릭하면 1초(1000ms) 단위로 화면이 자동으로 넘어가게 한다.
  • 뷰 플리퍼 안에 리니어 레이아웃을 배치할 필요는 없고 직접 이미지 뷰가 나오면 된다.

실행 결과

 

MainActivity.kt

package com.example.mytest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.*
import kotlin.system.exitProcess
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "직접 풀어보기 6-2"
var btnStart : Button
var btnStop : Button
var vFlipper : ViewFlipper
btnStart = findViewById<Button>(R.id.btnStart)
btnStop = findViewById<Button>(R.id.btnStop)
vFlipper = findViewById<ViewFlipper>(R.id.viewFlipper)
// 사진 보기 시작
btnStart.setOnClickListener {
vFlipper.startFlipping()
vFlipper.setFlipInterval(1000)
}
// 사진 보기 정지
btnStop.setOnClickListener {
vFlipper.stopFlipping()
}
}
}
view raw MainActivity.kt hosted with ❤ by GitHub

 

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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#CCCCCC"
android:textColor="#000000"
android:text="사진 보기 시작" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#CCCCCC"
android:textColor="#000000"
android:text="사진 보기 정지" />
</LinearLayout>
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/fire"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/water"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/dark"
/>
</ViewFlipper>
</LinearLayout>
반응형
Comments