Browse Source

draw function is not working as planed activate_btn "touched" just work when activate other action

master
Feyzu 3 years ago
parent
commit
08bb22639a
4 changed files with 47 additions and 35 deletions
  1. +27
    -10
      app/src/main/java/com/example/acapendulum20/canvas/CanvasPendulum.kt
  2. +17
    -22
      app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt
  3. +2
    -2
      build.gradle
  4. +1
    -1
      gradle/wrapper/gradle-wrapper.properties

+ 27
- 10
app/src/main/java/com/example/acapendulum20/canvas/CanvasPendulum.kt View File

@ -2,6 +2,7 @@ package com.example.acapendulum20.canvas
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
@ -11,32 +12,48 @@ class CanvasPendulum @JvmOverloads constructor(
) : View(context, attrs, defStyleAttr) {
val paint: Paint = Paint()
lateinit var canvas: Canvas
private val paint: Paint = Paint()
private lateinit var canvas: Canvas
private var pendulumLineStopX = 200f
private var pendulumLineStopY = 600f
//var pendulumOnMove = false
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
paint.style = Paint.Style.STROKE
//paint.strokeWidth = 10f
//canvas?.drawCircle(500f, 700f, 70f, paint)
paint.strokeWidth = 9f
canvas?.drawLine(455f,0f,500f, 630f,paint)
//if canvas not NULL then this canvas = it
canvas?.let {
this.canvas = it
}
}
fun drawPendulum() {
fun startPendulum() {
//pendulumOnMove = false
paint.style = Paint.Style.STROKE
paint.strokeWidth = 10f
canvas.drawCircle(500f, 700f, 70f, paint)
//paint.strokeWidth = 10f
//canvas.drawCircle(200f, 700f, 70f, paint)// cx&cy -> Variable
paint.strokeWidth = 9f
canvas.drawLine(455f,0f,500f, 630f,paint)
canvas.drawColor(Color.WHITE)
canvas.drawLine(455f,0f,pendulumLineStopX, pendulumLineStopY,paint) //stopX&stopY -> Variable
println("excess to startPendulum function")
}
fun stopDrawingPendulum() {
canvas.drawCircle(400f, 300f, 70f, paint)
//canvas.drawCircle(400f, 300f, 70f, paint)
//pendulumOnMove = false
//canvas.drawColor(Color.WHITE)
//canvas.drawLine(455f,0f,500f, 630f,paint) //stopX&stopY -> Variable
println("excess to stopDrawingPendulum function")
}
}


+ 17
- 22
app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt View File

@ -1,6 +1,6 @@
package com.example.acapendulum20.fragments.pendulum
import android.graphics.Canvas
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MotionEvent
@ -8,58 +8,53 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.acapendulum20.R
import com.example.acapendulum20.canvas.CanvasPendulum
import com.google.android.material.canvas.CanvasCompat
import kotlinx.android.synthetic.main.fragment_pendulum.view.*
class PendulumFragment : Fragment() {
@SuppressLint("ClickableViewAccessibility")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_pendulum, container, false)
val mCanvasPendulum: CanvasPendulum
mCanvasPendulum = view.findViewById(R.id.canvasContainer)!!
val mCanvasPendulum: CanvasPendulum = view.findViewById(R.id.canvasContainer)!!
//activate the magnet
val button = view.findViewById<Button>(R.id.activate_btn)
/*
button.setOnClickListener{
mCanvasPendulum.drawBob()
mCanvasPendulum.drawLine()
}*/
button.setOnTouchListener { v, event ->
val action = event.action
when(action){
//button.setOnClickListener{
// mCanvasPendulum.startPendulum()
//}
button.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
mCanvasPendulum.drawPendulum()
println("button gedrückt")
mCanvasPendulum.startPendulum()
}
MotionEvent.ACTION_MOVE -> { }
//MotionEvent.ACTION_MOVE -> {}
MotionEvent.ACTION_UP -> {
println("button losgelassen")
mCanvasPendulum.stopDrawingPendulum()
}
MotionEvent.ACTION_CANCEL -> {
}
//MotionEvent.ACTION_CANCEL -> { }
else ->{
}
//else -> {}
}
true
}
//System.currentTimeMillis()
//System.nanoTime()


+ 2
- 2
build.gradle View File

@ -10,8 +10,8 @@ buildscript {
}
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}


+ 1
- 1
gradle/wrapper/gradle-wrapper.properties View File

@ -1,6 +1,6 @@
#Tue Apr 19 09:09:32 CEST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

Loading…
Cancel
Save