Browse Source

Pendel swinging now! sensor working! need magnet logic now and safe data to databank

master
Feyzu 3 years ago
parent
commit
b8f872d476
8 changed files with 179 additions and 99 deletions
  1. +8
    -1
      .idea/misc.xml
  2. +4
    -6
      app/build.gradle
  3. BIN
      app/src/main/elektromagnet-playstore.png
  4. +95
    -43
      app/src/main/java/com/example/acapendulum20/canvas/CanvasPendulum.kt
  5. +21
    -20
      app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt
  6. BIN
      app/src/main/res/drawable-xxhdpi/digitalsensor.png
  7. BIN
      app/src/main/res/drawable-xxhdpi/elektromagnet.png
  8. +51
    -29
      app/src/main/res/layout/fragment_pendulum.xml

+ 8
- 1
.idea/misc.xml View File

@ -3,15 +3,22 @@
<component name="DesignSurface">
<option name="filePathToZoomLevelMap">
<map>
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/drawable-v24/ic_launcher_foreground.xml" value="0.17" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/drawable-v24/vd_vector.xml" value="0.545" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/drawable/elektromagnet_background.xml" value="0.4345" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/drawable/ic_add.xml" value="0.17" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/drawable/ic_baseline_delete_24.xml" value="0.17" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/drawable/ic_launcher_background.xml" value="0.17" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/layout/activity_main.xml" value="0.29713114754098363" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/layout/custom_row.xml" value="0.384375" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/layout/fragment_add.xml" value="0.45416666666666666" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/layout/fragment_list.xml" value="0.45416666666666666" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/layout/fragment_pendulum.xml" value="0.49130434782608695" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/layout/fragment_pendulum.xml" value="0.5" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/layout/fragment_pendulum2.xml" value="0.5541666666666667" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/layout/fragment_update.xml" value="0.33" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/menu/delete_menu.xml" value="0.5541666666666667" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/mipmap-anydpi-v26/elektromagnet.xml" value="0.3165" />
<entry key="..\:/Users/Feyzu/AndroidStudioProjects/acaPendulum20/app/src/main/res/mipmap-anydpi-v26/elektromagnet_round.xml" value="0.5255" />
</map>
</option>
</component>


+ 4
- 6
app/build.gradle View File

@ -4,7 +4,6 @@ plugins {
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'org.jetbrains.kotlin.android'
//id 'android.navigation.safeargs.kotlin'
id 'androidx.navigation.safeargs'
}
@ -49,11 +48,10 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.core:core-ktx:1.7.0'
//implementation "org.jetbrians.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'


BIN
app/src/main/elektromagnet-playstore.png View File

Before After
Width: 512  |  Height: 512  |  Size: 72 KiB

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

@ -1,78 +1,130 @@
package com.example.acapendulum20.canvas
import android.R
import android.content.Context
import android.content.res.Resources
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin
class CanvasPendulum @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val paint: Paint = Paint()
private lateinit var canvas: Canvas
private var pendulumLineStopX = 200f
private var pendulumLineStopY = 600f
//var pendulumOnMove = false
private val mPendulum: Pendulum = Pendulum()
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 startPendulum() {
//pendulumOnMove = false
//println("ONDRAW")
//draw line
mPendulum.startPosition.x = (width/2).toFloat()
paint.isAntiAlias = true
paint.style = Paint.Style.STROKE
//paint.strokeWidth = 10f
//canvas.drawCircle(200f, 700f, 70f, paint)// cx&cy -> Variable
paint.strokeWidth = 9f
canvas.drawColor(Color.WHITE)
canvas.drawLine(455f,0f,pendulumLineStopX, pendulumLineStopY,paint) //stopX&stopY -> Variable
println("excess to startPendulum function")
}
paint.color = Color.RED
paint.strokeWidth = 10f
this.canvas.drawLine(mPendulum.startPosition.x, mPendulum.startPosition.y, mPendulum.stopPosition.x, mPendulum.stopPosition.y, paint)
//draw circle
paint.style = Paint.Style.FILL_AND_STROKE
paint.strokeWidth = 1f
paint.color = Color.GRAY
canvas?.drawCircle(mPendulum.stopPosition.x, mPendulum.stopPosition.y, mPendulum.ballr, paint)
//draw magnet as black circle
paint.style = Paint.Style.FILL_AND_STROKE
paint.strokeWidth = 1f
paint.color = Color.DKGRAY
canvas?.drawCircle((width/2).toFloat(),1150f, 30f, paint)
//draw sensor left
paint.style = Paint.Style.FILL_AND_STROKE
paint.strokeWidth = 1f
paint.color = Color.RED
if (mPendulum.angleVelocity > 0 ){
if (mPendulum.stopPosition.x < (width/3) - 30f && mPendulum.stopPosition.x +30 > (width/3) - 30f - mPendulum.ballr){
paint.style = Paint.Style.FILL_AND_STROKE
paint.strokeWidth = 10f
paint.color = Color.GREEN
}
}
canvas?.drawCircle((width/3).toFloat(),910f, 30f, paint)
//draw sensor right
paint.style = Paint.Style.FILL_AND_STROKE
paint.strokeWidth = 10f
paint.color = Color.RED
if (mPendulum.angleVelocity < 0 ){
if (mPendulum.stopPosition.x > (width/1.5) - 30f && mPendulum.stopPosition.x +30 < (width/1.5) + 30f + mPendulum.ballr){
paint.style = Paint.Style.FILL_AND_STROKE
paint.strokeWidth = 10f
paint.color = Color.GREEN
}
}
canvas?.drawCircle((width/1.5).toFloat(),910f, 30f, paint)
fun stopDrawingPendulum() {
//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")
startPendulum()
invalidate()
}
}
fun startPendulum() {
val gravity = 0.981f
val damping = 0.996f
mPendulum.angleAcceleration = ((-1 * gravity / mPendulum.r) * (mPendulum.angle)).toFloat()
mPendulum.angleVelocity += mPendulum.angleAcceleration
mPendulum.angleVelocity *= damping
mPendulum.angle += mPendulum.angleVelocity
//Polar to cartesian conversion
mPendulum.stopPosition.x = mPendulum.r * (sin(mPendulum.angle)).toFloat()
mPendulum.stopPosition.y = mPendulum.r * (cos(mPendulum.angle)).toFloat()
//Origin transition
mPendulum.stopPosition.x += mPendulum.startPosition.x
}
fun startMagnet(){
//if Circle = sensor -> circle/angleVelocity ++
if (mPendulum.stopPosition.x == canvas.width/2f) {
mPendulum.angleVelocity = 0f
}
}
fun stopMagnet(){
}
}
class Position(var x: Float, var y: Float){
}
/*<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" />*/
class Pendulum {
var startPosition: Position = Position(0f, 0f)
var stopPosition: Position = Position(0f,0f)
var r = 800f
//PI/6 Max ausschwingen
var angle : Double = Math.PI/6
var angleVelocity : Float = 0.0f //
var angleAcceleration : Float = 0.0f //
var ballr : Float = 70.0f //
}

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

@ -2,6 +2,7 @@ package com.example.acapendulum20.fragments.pendulum
import android.annotation.SuppressLint
import android.os.Bundle
import android.os.SystemClock
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
@ -10,6 +11,7 @@ import android.widget.Button
import androidx.fragment.app.Fragment
import com.example.acapendulum20.R
import com.example.acapendulum20.canvas.CanvasPendulum
import java.util.*
class PendulumFragment : Fragment() {
@ -32,27 +34,28 @@ class PendulumFragment : Fragment() {
//}
button.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
println("button gedrückt")
mCanvasPendulum.startPendulum()
button.setOnTouchListener(object : View.OnTouchListener{
var pushed = false
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
var steps: Int
var startTime: Long
if (event?.action == MotionEvent.ACTION_DOWN){
startTime = SystemClock.elapsedRealtime()
pushed = true
do {
mCanvasPendulum.startMagnet()
steps =- 1
}while (pushed)
}
//MotionEvent.ACTION_MOVE -> {}
MotionEvent.ACTION_UP -> {
println("button losgelassen")
mCanvasPendulum.stopDrawingPendulum()
if (event?.action == MotionEvent.ACTION_UP){
pushed = false
mCanvasPendulum.stopMagnet()
println("button action up")
}
//MotionEvent.ACTION_CANCEL -> { }
//else -> {}
return true
}
true
}
})
//System.currentTimeMillis()
@ -60,6 +63,4 @@ class PendulumFragment : Fragment() {
return view
}
}
}

BIN
app/src/main/res/drawable-xxhdpi/digitalsensor.png View File

Before After
Width: 61  |  Height: 61  |  Size: 2.7 KiB

BIN
app/src/main/res/drawable-xxhdpi/elektromagnet.png View File

Before After
Width: 138  |  Height: 98  |  Size: 10 KiB

+ 51
- 29
app/src/main/res/layout/fragment_pendulum.xml View File

@ -5,67 +5,66 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:padding="1dp"
tools:context=".fragments.pendulum.PendulumFragment">
<TextView
android:id="@+id/textView4"
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="48dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toEndOf="@+id/textView6" />
<TextView
android:id="@+id/textView5"
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="20dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4" />
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="48dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView5" />
<TextView
android:id="@+id/textView7"
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="48dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView6" />
app:layout_constraintStart_toEndOf="@+id/textView4" />
<TextView
android:id="@+id/textView8"
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="48dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toEndOf="@+id/textView8" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="20dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView6"
app:layout_constraintStart_toEndOf="@+id/textView9" />
@ -74,18 +73,31 @@
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="20dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView7"
app:layout_constraintStart_toEndOf="@+id/textView10" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="20dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView5"
app:layout_constraintStart_toEndOf="@+id/textView8" />
<Button
android:id="@+id/speichern_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginBottom="60dp"
android:text="Speichern"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.857"
app:layout_constraintStart_toEndOf="@+id/textView11" />
<Button
@ -95,20 +107,30 @@
android:text="Activate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.77" />
app:layout_constraintVertical_bias="0.803" />
<com.example.acapendulum20.canvas.CanvasPendulum
android:id="@+id/canvasContainer"
android:layout_width="347dp"
android:layout_height="440dp"
app:layout_constraintBottom_toTopOf="@+id/activate_btn"
android:layout_width="407dp"
android:layout_height="462dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.13" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="79dp"
android:layout_height="71dp"
android:src="@drawable/elektromagnet"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.139"/>
app:layout_constraintVertical_bias="0.657" />
</androidx.constraintlayout.widget.ConstraintLayout>

Loading…
Cancel
Save