mirror of
https://github.com/Skysamara/airshot_a.git
synced 2025-12-06 06:16:05 +00:00
onClick
This commit is contained in:
parent
b38a9655e7
commit
9c7cca97e5
@ -3,6 +3,11 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
package="ru.panorobot.airshot">
|
package="ru.panorobot.airshot">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.BLUETOOTH" />
|
||||||
|
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|||||||
@ -1,13 +1,96 @@
|
|||||||
package ru.panorobot.airshot
|
package ru.panorobot.airshot
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.app.Activity
|
||||||
|
import android.bluetooth.BluetoothAdapter
|
||||||
|
import android.content.Intent
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.widget.Toast
|
||||||
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
|
|
||||||
|
// Почему не получалось обратиться к элементам формы напрямую
|
||||||
|
// https://stackoverflow.com/questions/52271521/import-kotlinx-android-synthetic-main-activity-main-is-not-working
|
||||||
|
//
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
private val REQUEST_CODE_ENABLE_BT:Int = 1
|
||||||
|
|
||||||
|
// bluetooth adapter
|
||||||
|
lateinit var bAdapter:BluetoothAdapter
|
||||||
|
|
||||||
|
@SuppressLint("MissingPermission")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
// setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
val draw2D = Draw2D(this)
|
|
||||||
setContentView(draw2D)
|
// bluetooth adapter
|
||||||
|
bAdapter = BluetoothAdapter.getDefaultAdapter()
|
||||||
|
// check is on/off
|
||||||
|
if(bAdapter == null){
|
||||||
|
bluetoothStatusTV.text = "Bluetooht is not available"
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
bluetoothStatusTV.text = "Bluetooht is available"
|
||||||
|
}
|
||||||
|
// Статус bluetooth
|
||||||
|
if (bAdapter.isEnabled){
|
||||||
|
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Включаем bluetooth
|
||||||
|
turnOnBtn.setOnClickListener {
|
||||||
|
if (bAdapter.isEnabled){
|
||||||
|
// Уже включен
|
||||||
|
Toast.makeText(this, "Already on", Toast.LENGTH_LONG).show()
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// Включаем
|
||||||
|
var intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
||||||
|
startActivityForResult(intent, REQUEST_CODE_ENABLE_BT)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Выключаем bluetooth
|
||||||
|
turnOffBtn.setOnClickListener {
|
||||||
|
|
||||||
|
}
|
||||||
|
// Ищем bluetooth
|
||||||
|
discoverableBtn.setOnClickListener {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ****************************************** startShooting()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
when(requestCode){
|
||||||
|
REQUEST_CODE_ENABLE_BT ->
|
||||||
|
if (requestCode == Activity.RESULT_OK){
|
||||||
|
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) // 14:14
|
||||||
|
Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_LONG).show()
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
Toast.makeText(this, "Could not on bluetooth", Toast.LENGTH_LONG).show()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startShooting() {
|
||||||
|
val target = Target(this)
|
||||||
|
target.setOnClickListener {
|
||||||
|
target.invalidate()
|
||||||
|
}
|
||||||
|
setContentView(target)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -7,29 +7,35 @@ import android.graphics.Canvas
|
|||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
class Draw2D(context: Context?) : View(context) {
|
class Target(context: Context?) : View(context) {
|
||||||
|
|
||||||
private val paint: Paint = Paint()
|
private val paint: Paint = Paint()
|
||||||
private val shots:Shots = Shots()
|
private val shots:Shots = Shots()
|
||||||
|
|
||||||
override fun setOnClickListener(l: OnClickListener?) {
|
// override fun setOnTouchListener(l: OnTouchListener?) {
|
||||||
super.setOnClickListener(l)
|
//// super.setOnTouchListener(l)
|
||||||
invalidate()
|
//// invalidate()
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
// override fun setOnClickListener(l: OnClickListener?) {
|
||||||
|
//// super.setOnClickListener(l)
|
||||||
|
//// invalidate()
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
override fun onDraw(canvas: Canvas?) {
|
override fun onDraw(canvas: Canvas?) {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
val screenWidth: Int = getWidth()
|
val screenWidth: Int = getWidth()
|
||||||
val screenHeight: Int = getHeight()
|
val screenHeight: Int = getHeight()
|
||||||
|
|
||||||
drawTarget(canvas)
|
drawTarget(canvas, screenWidth, screenHeight)
|
||||||
shots.draw(canvas,screenWidth, screenHeight)
|
shots.draw(canvas,screenWidth, screenHeight)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun drawTarget(canvas: Canvas?) {
|
private fun drawTarget(canvas: Canvas?, screenWidth: Int, screenHeight: Int) {
|
||||||
paint.apply {
|
paint.apply {
|
||||||
style = Paint.Style.FILL // стиль Заливка
|
style = Paint.Style.FILL // стиль Заливка
|
||||||
color = Color.WHITE // закрашиваем холст белым цветом
|
color = Color.WHITE // закрашиваем холст белым цветом
|
||||||
@ -41,28 +47,26 @@ class Draw2D(context: Context?) : View(context) {
|
|||||||
color = Color.BLACK
|
color = Color.BLACK
|
||||||
}
|
}
|
||||||
|
|
||||||
val screenWidth: Int = getWidth()
|
|
||||||
val screenHeight: Int = getHeight()
|
|
||||||
val centerX = width / 2
|
val centerX = width / 2
|
||||||
val centerY = height / 2
|
val centerY = height / 2
|
||||||
|
|
||||||
var radiusSun: Int = (screenWidth + screenHeight) / 30
|
var radiusApple: Int = (screenWidth + screenHeight) / 30
|
||||||
canvas!!.drawCircle(centerX.toFloat(), centerY.toFloat(), radiusSun.toFloat(), paint)
|
canvas!!.drawCircle(centerX.toFloat(), centerY.toFloat(), radiusApple.toFloat(), paint)
|
||||||
|
|
||||||
paint.apply {
|
paint.apply {
|
||||||
style = Paint.Style.STROKE
|
style = Paint.Style.STROKE
|
||||||
}
|
}
|
||||||
var radius = radiusSun * 2
|
var radius = radiusApple * 2
|
||||||
canvas!!.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
|
canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
|
||||||
|
|
||||||
radius = radiusSun * 3
|
radius = radiusApple * 3
|
||||||
canvas!!.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
|
canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
|
||||||
|
|
||||||
radius = radiusSun * 4
|
radius = radiusApple * 4
|
||||||
canvas!!.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
|
canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
|
||||||
|
|
||||||
radius = radiusSun * 5
|
radius = radiusApple * 5
|
||||||
canvas!!.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
|
canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), radius.toFloat(), paint)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1,18 +1,63 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/bluetoothStatusTV"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text=""
|
||||||
|
android:padding="5dp"
|
||||||
|
android:textAlignment="center"
|
||||||
|
android:textColor="#000"
|
||||||
|
android:textSize="20sp"/>
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/bluetoothIv"
|
||||||
|
android:layout_width="100dp"
|
||||||
|
android:layout_height="100dp"
|
||||||
|
android:src="@drawable/ic_bluetooth_off"/>
|
||||||
|
<Button
|
||||||
|
android:id="@+id/turnOnBtn"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Hello World!"
|
style="@style/Widget.AppCompat.Button.Colored"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
android:text="Turn On"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
android:minWidth="200dp"/>
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
<Button
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
android:id="@+id/turnOffBtn"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
style="@style/Widget.AppCompat.Button.Colored"
|
||||||
|
android:text="Turn Off"
|
||||||
|
android:minWidth="200dp"/>
|
||||||
|
<Button
|
||||||
|
android:id="@+id/discoverableBtn"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
style="@style/Widget.AppCompat.Button.Colored"
|
||||||
|
android:text="Discoverable"
|
||||||
|
android:minWidth="200dp"/>
|
||||||
|
<Button
|
||||||
|
android:id="@+id/pairedBtn"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
style="@style/Widget.AppCompat.Button.Colored"
|
||||||
|
android:text="Get Paired Devices"
|
||||||
|
android:minWidth="200dp"/>
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/pairedTv"
|
||||||
|
android:padding="5dp"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textColor="#000"/>
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
||||||
|
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
Loading…
x
Reference in New Issue
Block a user