Browse Source

draw function is not working while activate_btn "touched"

master
Feyzu 3 years ago
parent
commit
72e6ffca7d
5 changed files with 116 additions and 58 deletions
  1. +0
    -1
      .idea/gradle.xml
  2. +0
    -32
      app/src/main/java/com/example/acapendulum20/canvas/Canvas.kt
  3. +61
    -0
      app/src/main/java/com/example/acapendulum20/canvas/CanvasPendulum.kt
  4. +43
    -15
      app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt
  5. +12
    -10
      app/src/main/res/layout/fragment_pendulum.xml

+ 0
- 1
.idea/gradle.xml View File

@ -13,7 +13,6 @@
<option value="$PROJECT_DIR$/app" /> <option value="$PROJECT_DIR$/app" />
</set> </set>
</option> </option>
<option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings> </GradleProjectSettings>
</option> </option>
</component> </component>

+ 0
- 32
app/src/main/java/com/example/acapendulum20/canvas/Canvas.kt View File

@ -1,32 +0,0 @@
package com.example.acapendulum20.canvas
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
class CanvasView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
val paint: Paint = Paint()
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
paint.strokeWidth = 9f
canvas?.drawLine(455f,0f,500f, 630f,paint)
}
fun drawBob(canvas: Canvas?) {
paint.style = Paint.Style.STROKE
paint.strokeWidth = 10f
canvas?.drawCircle(500f, 700f, 70f, paint)
}
fun drawBob() {
println("hallo")
}
}

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

@ -0,0 +1,61 @@
package com.example.acapendulum20.canvas
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
class CanvasPendulum @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
val paint: Paint = Paint()
lateinit var canvas: Canvas
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
//if canvas not NULL then this canvas = it
canvas?.let {
this.canvas = it
}
}
fun drawPendulum() {
paint.style = Paint.Style.STROKE
paint.strokeWidth = 10f
canvas.drawCircle(500f, 700f, 70f, paint)
paint.strokeWidth = 9f
canvas.drawLine(455f,0f,500f, 630f,paint)
}
fun stopDrawingPendulum() {
canvas.drawCircle(400f, 300f, 70f, paint)
}
}
/*<view
class="com.example.acapendulum20.canvas.CanvasView"
android:layout_width="347dp"
android:layout_height="440dp"
app:layout_constraintBottom_toTopOf="@+id/activate_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.139" />*/

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

@ -1,42 +1,70 @@
package com.example.acapendulum20.fragments.pendulum package com.example.acapendulum20.fragments.pendulum
import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import android.graphics.Canvas
import android.os.Bundle import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.res.ResourcesCompat
import android.widget.Button
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.acapendulum20.R import com.example.acapendulum20.R
import com.example.acapendulum20.canvas.CanvasView
import com.example.acapendulum20.viewmodel.UserViewmodel
import kotlinx.android.synthetic.main.fragment_pendulum.*
import com.example.acapendulum20.canvas.CanvasPendulum
import com.google.android.material.canvas.CanvasCompat
import kotlinx.android.synthetic.main.fragment_pendulum.view.* import kotlinx.android.synthetic.main.fragment_pendulum.view.*
import kotlinx.android.synthetic.main.fragment_update.view.*
class PendulumFragment : Fragment() { class PendulumFragment : Fragment() {
private lateinit var mCanvasView: CanvasView
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
// Inflate the layout for this fragment // Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_pendulum, container, false) val view = inflater.inflate(R.layout.fragment_pendulum, container, false)
val mCanvasPendulum: CanvasPendulum
mCanvasPendulum = view.findViewById(R.id.canvasContainer)!!
//activate the magnet //activate the magnet
view.activate_btn.setOnClickListener{
mCanvasView.drawBob()
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){
MotionEvent.ACTION_DOWN -> {
mCanvasPendulum.drawPendulum()
}
MotionEvent.ACTION_MOVE -> { }
MotionEvent.ACTION_UP -> {
mCanvasPendulum.stopDrawingPendulum()
}
MotionEvent.ACTION_CANCEL -> {
}
else ->{
}
}
true
} }
//System.currentTimeMillis()
//System.nanoTime()
return view return view
} }
} }

+ 12
- 10
app/src/main/res/layout/fragment_pendulum.xml View File

@ -1,5 +1,6 @@
<?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"
<androidx.constraintlayout.widget.ConstraintLayout
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"
@ -60,15 +61,6 @@
app:layout_constraintBottom_toTopOf="@+id/textView5" app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toEndOf="@+id/textView8" /> app:layout_constraintStart_toEndOf="@+id/textView8" />
<view
class="com.example.acapendulum20.canvas.CanvasView"
android:layout_width="347dp"
android:layout_height="440dp"
app:layout_constraintBottom_toTopOf="@+id/activate_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.139" />
<TextView <TextView
android:id="@+id/textView10" android:id="@+id/textView10"
@ -108,5 +100,15 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.77" /> app:layout_constraintVertical_bias="0.77" />
<com.example.acapendulum20.canvas.CanvasPendulum
android:id="@+id/canvasContainer"
android:layout_width="347dp"
android:layout_height="440dp"
app:layout_constraintBottom_toTopOf="@+id/activate_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.139"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

Loading…
Cancel
Save