개발(Android)/android studio

[Android Studio/Kotlin] Views (2) Video View, Calendar View

shinyelee 2022. 5. 7. 00:51

[안드로이드 스튜디오/코틀린] 비디오 뷰, 캘린더 뷰

VideoView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

package com.shinyelee.android_study2

import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebViewClient
import android.widget.MediaController
import android.widget.Toast
import com.shinyelee.android_study2.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private var vBinding : ActivityMainBinding? = null
    private val binding get() = vBinding!!

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        vBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 방법1
        val videoPath = "android.resource://" + getPackageName() + "/" + R.raw.joinfail
        binding.videoView.setVideoPath(videoPath)

        // 방법2
//        binding.videoView.setVideoURI(Uri.parse("https://www.radiantmediaplayer.com/media/bbb-360p.mp4"))

        val mediaController = MediaController(this)
        binding.videoView.setMediaController(mediaController)
        mediaController.setAnchorView(binding.videoView)
        binding.videoView.keepScreenOn = true

//        binding.videoView.start()
//        binding.videoView.pause()
//        binding.videoView.seekTo(10000)

    }

    override fun onDestroy() {
        vBinding = null
        super.onDestroy()
    }

}

비디오 재생/정지 가능


CalendarView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:minDate="01/01/2022"
        android:maxDate="12/31/2022"/>
    <!-- minData, maxData로 범위 설정(MM/DD/YYYY) -->

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

package com.shinyelee.android_study2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.shinyelee.android_study2.databinding.ActivityMainBinding
import java.util.*

class MainActivity : AppCompatActivity() {

    private var vBinding : ActivityMainBinding? = null
    private val binding get() = vBinding!!

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        vBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.calendarView.setOnDateChangeListener { calendarView, year, month, day ->
        // 월(month)은 1이 아니라 0부터 시작하니 주의!
//            println(year.toString() + "/" + month.toString() + "/" + day.toString())
        }
        val calendar = Calendar.getInstance()
        // 기본값 세팅하기
        calendar.set(2022, 5, 15)
        binding.calendarView.setDate(calendar.timeInMillis)

    }

    override fun onDestroy() {
        vBinding = null
        super.onDestroy()
    }

}

세팅한 값으로 시작하는 달력을 볼 수 있음


참고

반응형