Не обновляется собственное View при изменении полей класса
Написал собственно View. HexaView наследует класс View. Туда добавлены поля:
var angle_cren: Float = 15f
var angle_tang: Float = 10f
var angle_risk: Float = 225f
Также есть две кнопки. Также в MainActivity написаны обработчики нажатий на кнопки(onClickTakeOff, onClickLanding), которые меняют поля angle_cren, angle_tang, angle_risk. Поиск HexaView происходит по id. Проблема заключается в том, что при нажатии на кнопку, значения этих полей меняются, но при этом HexaView не перерисовывается на самом экране. В чем может быть проблема? Как обновить HexaView на экране с новыми параметрами? Код: MainActivity.kt:
package com.example.hexacopterapp
import android.app.Activity
import android.os.Bundle
import android.os.SystemClock
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.hexacopterapp.view.HexaView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onClickTakeOff(view : View) {
val HView = findViewById<HexaView>(R.id.hexaView)
HView.angle_cren = 155f
HView.angle_tang = 15f
HView.angle_risk = 30f
Log.d("MyLog", txt.text.toString())
Log.d("MyLog", HView.angle_cren.toString())
}
fun onClickLanding(view : View) {
val HView = findViewById<HexaView>(R.id.hexaView)
HView.angle_cren = -25f
HView.angle_tang = -15f
HView.angle_risk = -125f
Log.d("MyLog", HView.angle_cren.toString())
}
override fun onResume() {
super.onResume()
}
HexaView.kt:
package com.example.hexacopterapp.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.annotation.Nullable
class HexaView : View {
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val paint_text = Paint(Paint.ANTI_ALIAS_FLAG)
var angle_cren: Float = 15f
var angle_tang: Float = 10f
var angle_risk: Float = 225f
val a = 1
constructor(context: Context?) : super(context) {}
constructor(context: Context?, @Nullable attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context?, @Nullable attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.save()
var width = width.toFloat()
var height = height.toFloat()
canvas.scale(.5f * width, -1f * height)
canvas.translate(1f, -1f)
paint.color = Color.parseColor("#17C3E1")
paint.style = Paint.Style.FILL
canvas.drawRect(-1f, 0.5f, 1f, 1f, paint)
paint.color = Color.parseColor("#FF7CFF46")
canvas.drawRect(-1f, 0f, 1f, 0.5f, paint)
paint.color = Color.WHITE
paint.style = Paint.Style.STROKE
paint.strokeWidth = 0.005f
//Шкала для угла крена
val maxValue = 150
val value = 25
val scale = 0.97f
val step = Math.PI / 180
for (i in 30..maxValue) {
val x1 = Math.cos(step * i).toFloat() * 0.5f
val y1 = Math.sin(step * i).toFloat() * 0.5f
var x2: Float
var y2: Float
if (i % 10 == 0) {
x2 = x1 * scale * 0.98f * 0.99f
y2 = y1 * scale * 0.98f * 0.99f
} else {
x2 = x1 * scale * 0.99f
y2 = y1 * scale * 0.99f
}
canvas.drawLine(x1, (y1 + 0.5f), x2, (y2 + 0.5f), paint)
}
//Шкала для угла рысканья
for (i in 0..360) {
val x1 = Math.cos(step * i).toFloat() * 0.15f
val y1 = Math.sin(step * i).toFloat() * 0.15f
var x2: Float
var y2: Float
if (i % 45 == 0){
x2 = x1 * scale * 0.95f * 0.99f
y2 = y1 * scale * 0.95f * 0.99f
} else if (i % 10 == 0) {
x2 = x1 * scale * 0.98f * 0.99f
y2 = y1 * scale * 0.98f * 0.99f
} else {
x2 = x1 * scale * 0.99f
y2 = y1 * scale * 0.99f
}
canvas.drawLine((x1 + 0.75f), (y1 + 0.75f), (x2 + 0.75f), (y2 + 0.75f), paint)
}
paint.strokeWidth = 0.0025f
canvas.drawLine(0.75f, 0.6f, 0.75f, 0.9f, paint)
canvas.drawLine(0.6f, 0.75f, 0.9f, 0.75f, paint)
//Шкала для угла тангажа
paint.color = Color.WHITE
paint.strokeWidth = 0.005f
canvas.drawLine(-0.1f, 0.1f, 0.1f, 0.1f, paint)
canvas.drawLine(-0.05f, 0.2f, 0.05f, 0.2f, paint)
canvas.drawLine(-0.1f, 0.3f, 0.1f, 0.3f, paint)
canvas.drawLine(-0.05f, 0.4f, 0.05f, 0.4f, paint)
canvas.drawLine(-0.1f, 0.5f, 0.1f, 0.5f, paint)
canvas.drawLine(-0.05f, 0.6f, 0.05f, 0.6f, paint)
canvas.drawLine(-0.1f, 0.7f, 0.1f, 0.7f, paint)
canvas.drawLine(-0.05f, 0.8f, 0.05f, 0.8f, paint)
canvas.drawLine(-0.1f, 0.9f, 0.1f, 0.9f, paint)
//Указатель курса и угла тангажа
paint.color = Color.RED
paint.strokeWidth = 0.005f
var h: Float = 0.2f / 10
if (angle_tang > 20f) {
angle_tang = 20f
}
canvas.drawLine(Math.cos(step * angle_cren).toFloat() * 0.45f, Math.sin(step * angle_cren).toFloat() * 0.45f + 0.5f + (h * angle_tang), Math.cos(step * angle_cren).toFloat() * 0.2f, Math.sin(step * angle_cren).toFloat() * 0.2f + 0.5f + (h * angle_tang), paint)
canvas.drawLine(Math.cos(step * angle_cren).toFloat() * -0.45f, Math.sin(step * angle_cren).toFloat() * -0.45f + 0.5f + (h * angle_tang), Math.cos(step * angle_cren).toFloat() * -0.2f, Math.sin(step * angle_cren).toFloat() * -0.2f + 0.5f + (h * angle_tang), paint)
canvas.drawLine(Math.cos(step * (angle_cren - 30)).toFloat() * 0.11f, Math.sin(step * (angle_cren - 30)).toFloat() * 0.11f + 0.5f + (h * angle_tang), 0f, 0.5f + (h * angle_tang), paint)
canvas.drawLine(Math.cos(step * (angle_cren + 30)).toFloat() * -0.11f, Math.sin(step * (angle_cren + 30)).toFloat() * -0.11f + 0.5f + (h * angle_tang), 0f, 0.5f + (h * angle_tang), paint)
paint.strokeWidth = 0.005f
//Указатель угла крена
canvas.drawLine(Math.cos(step * (angle_cren + 90f)).toFloat() * 0.4851f, Math.sin(step * (angle_cren + 90f)).toFloat() * 0.4851f + 0.5f, Math.cos(step * (angle_cren + 90f + 3f)).toFloat() * 0.4851f * 0.93f, Math.sin(step * (angle_cren + 90f + 3f)).toFloat() * 0.4851f * 0.93f + 0.5f, paint)
canvas.drawLine(Math.cos(step * (angle_cren + 90f)).toFloat() * 0.4851f, Math.sin(step * (angle_cren + 90f)).toFloat() * 0.4851f + 0.5f, Math.cos(step * (angle_cren + 90f - 3f)).toFloat() * 0.4851f * 0.93f, Math.sin(step * (angle_cren + 90f - 3f)).toFloat() * 0.4851f * 0.93f + 0.5f, paint)
canvas.drawLine(Math.cos(step * (angle_cren + 90f + 3f)).toFloat() * 0.4851f * 0.93f, Math.sin(step * (angle_cren + 90f + 3f)).toFloat() * 0.4851f * 0.93f + 0.5f, Math.cos(step * (angle_cren + 90f - 3f)).toFloat() * 0.4851f * 0.93f, Math.sin(step * (angle_cren + 90f - 3f)).toFloat() * 0.4851f * 0.93f + 0.5f, paint)
//Указатель угла рысканья
paint.strokeWidth = 0.0025f
canvas.drawLine(Math.cos(step * angle_risk).toFloat() * -0.01f + 0.75f, Math.sin(step * angle_risk).toFloat() * 0.01f + 0.75f, Math.cos(step * angle_risk).toFloat() * 0.01f + 0.75f,Math.sin(step * angle_risk).toFloat() * -0.01f + 0.75f, paint)
canvas.drawLine(Math.cos(step * angle_risk).toFloat() * -0.01f + 0.75f, Math.sin(step * angle_risk).toFloat() * 0.01f + 0.75f, Math.cos(step * (angle_risk - 90)).toFloat() * 0.15f + 0.75f,Math.sin(step * (angle_risk + 90)).toFloat() * 0.15f + 0.75f, paint)
canvas.drawLine(Math.cos(step * angle_risk).toFloat() * 0.01f + 0.75f,Math.sin(step * angle_risk).toFloat() * -0.01f + 0.75f, Math.cos(step * (angle_risk - 90)).toFloat() * 0.15f + 0.75f,Math.sin(step * (angle_risk + 90)).toFloat() * 0.15f + 0.75f, paint)
canvas.save()
canvas.rotate(90 - 180.toFloat() * (value / maxValue.toFloat()))
canvas.restore()
canvas.restore()
}
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:screenOrientation="landscape"
tools:context=".MainActivity">
<com.example.hexacopterapp.view.HexaView
android:id="@+id/hexaView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.434"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.105" />
<TextView
android:id="@+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-20"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.896" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.467"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04" />
<TextView
android:id="@+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="@+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-10"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.45"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.684" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.037" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.427"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.066" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.573"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.066" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.393"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.096" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.608"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.096" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.327"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.211" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.673"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.211" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="40"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.359"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.145" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="40"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.641"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.145" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="45"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.955"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.114" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="135"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.966"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.362" />
<TextView
android:id="@+id/textView24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="В"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.968"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.234" />
<TextView
android:id="@+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="315"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.807"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.114" />
<TextView
android:id="@+id/textView23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="С"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.879"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048" />
<TextView
android:id="@+id/textView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ю"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.884"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.428" />
<TextView
android:id="@+id/textView25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="З"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.789"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.234" />
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="225"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.807"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.362" />
<Button
android:id="@+id/button2"
android:layout_width="144dp"
android:layout_height="81dp"
android:onClick="onClickLanding"
android:text="Landing"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.972"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.951" />
<Button
android:id="@+id/button"
android:layout_width="144dp"
android:layout_height="81dp"
android:onClick="onClickTakeOff"
android:text="Take Off"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.027"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.951" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="60"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.302"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.283" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="60"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.698"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.283" />
</androidx.constraintlayout.widget.ConstraintLayout>