Kotlin Урок 4: Приложение “Светофор”

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="drt.nece_desvan.semafor">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>

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:background="@android:color/background_dark"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imSemafor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/semafor_grey" />

<ImageButton
android:id="@+id/imageButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="16dp"
android:background="@android:color/transparent"
android:onClick="onClickStartStop"
android:scaleType="fitCenter"
android:src="@drawable/button_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imSemafor" />
</androidx.constraintlayout.widget.ConstraintLayout>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowFullscreen">true</item>
</style>

</resources>

MainActivity.java

package drt.nece_desvan.semafor

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import android.widget.ImageView
import java.util.*

class MainActivity : Activity() {
var imSemafor: ImageView? = null
var counter:Int = 0
var timer: Timer? = null
var is_run = false
var imageArray:IntArray = intArrayOf(R.drawable.semafor_red,R.drawable.semafor_yellow,R.drawable.semafor_green)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imSemafor = findViewById(R.id.imSemafor)

}
fun onClickStartStop(view: View)
{
view as ImageButton
if(!is_run){

startStop()
view.setImageResource(R.drawable.button_stop)
is_run = true
}
else
{
imSemafor?.setImageResource(R.drawable.semafor_grey)
view.setImageResource(R.drawable.button_start)
timer?.cancel()
is_run = false
counter = 0
}
}
fun startStop() {
timer = Timer()
timer?.schedule(object :TimerTask(){
override fun run() {
runOnUiThread{
imSemafor?.setImageResource(imageArray[counter])
counter++
if(counter == 3)counter = 0
}
}

},0,1000)
}
}

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *