Skip to content

Commit

Permalink
touch
Browse files Browse the repository at this point in the history
  • Loading branch information
FarshidRoohi committed Dec 1, 2019
1 parent c2ad8cc commit 12def42
Showing 1 changed file with 34 additions and 38 deletions.
72 changes: 34 additions & 38 deletions linegraph/src/main/java/ir/farshid_roohi/linegraph/LineChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import androidx.core.content.ContextCompat
import ir.farshid_roohi.utilites.GraphCanvasWrapper
import ir.farshid_roohi.utilites.GraphPath
import java.util.*
import kotlin.math.abs


/**
Expand Down Expand Up @@ -147,42 +150,36 @@ class LineChart : View {
private var lastY = 0f


// @SuppressLint("ClickableViewAccessibility")
// override fun onTouchEvent(event: MotionEvent?): Boolean = event?.let {
// when (event.action) {
// MotionEvent.ACTION_MOVE -> {
// isMoved = true
// locationX = event.x
// locationY = event.y
//
// distanceX += abs(lastX - event.x)
// distanceY += abs(lastY - event.y)
// lastY = event.y
// lastX = event.x
// invalidate()
// }
// MotionEvent.ACTION_UP -> {
// isMoved = false
// invalidate()
// }
// }
//
// true
// } ?: false

// private var valueMap: HashMap<Int, Float> = HashMap()
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean = event?.let {
when (event.action) {
MotionEvent.ACTION_MOVE -> {
isMoved = true
locationX = event.x
locationY = event.y
distanceX += abs(lastX - event.x)
distanceY += abs(lastY - event.y)
lastY = event.y
lastX = event.x
Log.d("TAG_TT", "distanceX ${locationX} | distanceY : ${locationY}")
invalidate()
}
MotionEvent.ACTION_UP -> {
isMoved = false
invalidate()
}
}

true
} ?: false

@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
if (canvas == null) {
return
}

if (chartEntities == null) {
canvas.drawColor(bgColor)

}

this.initializePaint()

this.xLength = (width - (mPaddingLeft + mPaddingRight)).toInt()
Expand Down Expand Up @@ -217,15 +214,14 @@ class LineChart : View {

canvas.save()
canvas.restore()
//
// if (isMoved) {
// Log.d("TAGA_AFF", "is moved")
//
// val index = (locationX / width).toInt()
// graphCanvasWrapper.drawLine(locationX, yLength.toFloat(), locationX, 0f, pBaseLine)
// val a = valueMap[index]
// graphCanvasWrapper.drawCircle(locationX, y, 8f, pCircle)
// }

if (isMoved) {
Log.d("TAGA_AFF", "is moved distanceX : $distanceX | distanceY : $distanceY | gap : $gap")

val index = (locationX / width).toInt()
graphCanvasWrapper.drawLine(locationX, yLength.toFloat(), locationX, 0f, pBaseLine)
graphCanvasWrapper.drawCircle(locationX, locationY, 8f, pCircle)
}

}

Expand Down

0 comments on commit 12def42

Please sign in to comment.