diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt index 435268a..5507442 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt @@ -23,6 +23,7 @@ import android.view.ViewGroup import androidx.annotation.UiThread import com.github.wangyiqian.stockchart.childchart.base.IChildChart import com.github.wangyiqian.stockchart.entities.EmptyKEntity +import com.github.wangyiqian.stockchart.entities.GestureEvent import com.github.wangyiqian.stockchart.entities.Highlight import com.github.wangyiqian.stockchart.listener.OnKEntitiesChangedListener import com.github.wangyiqian.stockchart.util.checkMainThread @@ -123,7 +124,7 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet } override fun findLastNotEmptyKEntityIdxInDisplayArea(): Int? { - if(childCharts.isEmpty()) return null + if (childCharts.isEmpty()) return null val chartDisplayArea = childCharts[0].getChartDisplayArea() tmp4FloatArray[0] = chartDisplayArea.left tmp4FloatArray[1] = 0f @@ -143,7 +144,7 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet } override fun findFirstNotEmptyKEntityIdxInDisplayArea(): Int? { - if(childCharts.isEmpty()) return null + if (childCharts.isEmpty()) return null val chartDisplayArea = childCharts[0].getChartDisplayArea() tmp4FloatArray[0] = chartDisplayArea.left tmp4FloatArray[1] = 0f @@ -353,6 +354,21 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet notifyChanged() matrixHelper.checkScrollBack() } + + override fun onTap(x: Float, y: Float) { + childCharts.forEach { childChart -> + val childChartX = x - childChart.view().left + val childChartY = y - childChart.view().top + tmp2FloatArray[0] = childChartX + tmp2FloatArray[1] = childChartY + childChart.mapPointsReal2Value(tmp2FloatArray) + val valueX = tmp2FloatArray[0] + val valueY = tmp2FloatArray[1] + val gestureEvent = GestureEvent(childChartX, childChartY, valueX, valueY) + childChart.onTap(gestureEvent) + } + } + } override fun computeScroll() { diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/TouchHelper.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/TouchHelper.kt index 336ccad..83e7b2a 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/TouchHelper.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/TouchHelper.kt @@ -150,6 +150,11 @@ class TouchHelper(private val stockChart: IStockChart, private val callBack: Cal return true } + override fun onSingleTapConfirmed(e: MotionEvent): Boolean { + callBack.onTap(e.x, e.y) + return super.onSingleTapConfirmed(e) + } + interface CallBack { /** @@ -181,5 +186,10 @@ class TouchHelper(private val stockChart: IStockChart, private val callBack: Cal * 手指离开屏幕 */ fun onTouchLeave() + + /** + * 点击 + */ + fun onTap(x: Float, y: Float) } } \ No newline at end of file diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/BaseChildChart.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/BaseChildChart.kt index 26cbd71..0b13228 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/BaseChildChart.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/BaseChildChart.kt @@ -18,6 +18,7 @@ import android.view.View import android.view.ViewGroup import com.github.wangyiqian.stockchart.IStockChart import com.github.wangyiqian.stockchart.StockChart +import com.github.wangyiqian.stockchart.entities.GestureEvent import com.github.wangyiqian.stockchart.listener.OnKEntitiesChangedListener /** @@ -242,4 +243,5 @@ abstract class BaseChildChart @JvmOverloads constructo override fun getConfig() = chartConfig + override fun onTap(event: GestureEvent) {} } \ No newline at end of file diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/IChildChart.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/IChildChart.kt index fcb5cbd..5154fa1 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/IChildChart.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/IChildChart.kt @@ -17,6 +17,7 @@ import android.graphics.Matrix import android.graphics.Path import android.graphics.RectF import android.view.View +import com.github.wangyiqian.stockchart.entities.GestureEvent import com.github.wangyiqian.stockchart.entities.Highlight import com.github.wangyiqian.stockchart.entities.IKEntity @@ -85,4 +86,9 @@ interface IChildChart { * 获取对应配置 */ fun getConfig(): BaseChildChartConfig + + /** + * 点击 + */ + fun onTap(event: GestureEvent) } \ No newline at end of file diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/kdjchart/KdjChart.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/kdjchart/KdjChart.kt index d0c7489..a33aede 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/kdjchart/KdjChart.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/kdjchart/KdjChart.kt @@ -17,7 +17,6 @@ import android.graphics.Canvas import android.graphics.Paint import com.github.wangyiqian.stockchart.IStockChart import com.github.wangyiqian.stockchart.childchart.base.BaseChildChart -import kotlin.math.round /** * @author wangyiqian E-mail: wangyiqian9891@gmail.com @@ -118,7 +117,8 @@ class KdjChart(stockChart: IStockChart, chartConfig: KdjChartConfig) : chartConfig.index?.let { index -> indexList?.let { indexList -> val highlight = getHighlight() - var indexIdx = highlight?.getIdx() ?: stockChart.findLastNotEmptyKEntityIdxInDisplayArea() + var indexIdx = + highlight?.getIdx() ?: stockChart.findLastNotEmptyKEntityIdxInDisplayArea() indexTextPaint.textSize = index.textSize var left = index.textMarginLeft val top = index.textMarginTop diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/entities/GestureEvent.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/entities/GestureEvent.kt new file mode 100644 index 0000000..bc81686 --- /dev/null +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/entities/GestureEvent.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2021 WangYiqian + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + * + */ + +package com.github.wangyiqian.stockchart.entities + +/** + * @author wangyiqian E-mail: wangyiqian9891@gmail.com + * @version 创建时间: 2021/5/13 + */ +open class GestureEvent( + /** + * 实际坐标x + */ + var x: Float = 0f, + + /** + * 实际坐标y + */ + var y: Float = 0f, + + /** + * 逻辑坐标x + */ + var valueX: Float = 0f, + + /** + * 逻辑坐标y + */ + var valueY: Float = 0f +) { + + /** + * 获取对应K线数据下标 + */ + fun getIdx() = (valueX).toInt() + + override fun equals(other: Any?): Boolean { + if (other == null || other !is Highlight) return false + return x == other.x && y == other.y && valueX == other.valueX && valueY == other.valueY + } + + override fun toString(): String { + return "GestureEvent(x=$x, y=$y, valueX=$valueX, valueY=$valueY)" + } + + +} \ No newline at end of file diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/entities/Highlight.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/entities/Highlight.kt index 1a18124..83bcc04 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/entities/Highlight.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/entities/Highlight.kt @@ -18,36 +18,5 @@ package com.github.wangyiqian.stockchart.entities * @version 创建时间: 2021/2/6 */ class Highlight( - /** - * 实际坐标x - */ - var x: Float = 0f, - - /** - * 实际坐标y - */ - var y: Float = 0f, - - /** - * 逻辑坐标x - */ - var valueX: Float = 0f, - - /** - * 逻辑坐标y - */ - var valueY: Float = 0f -) { - - /** - * 获取对应K线数据下标 - */ - fun getIdx() = (valueX).toInt() - - override fun equals(other: Any?): Boolean { - if (other == null || other !is Highlight) return false - return x == other.x && y == other.y && valueX == other.valueX && valueY == other.valueY - } - - -} \ No newline at end of file + x: Float = 0f, y: Float = 0f, valueX: Float = 0f, valueY: Float = 0f +) : GestureEvent(x, y, valueX, valueY) \ No newline at end of file diff --git a/samples/build.gradle b/samples/build.gradle index fc1a47e..7d88eca 100644 --- a/samples/build.gradle +++ b/samples/build.gradle @@ -70,6 +70,6 @@ dependencies { androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation 'com.google.android:flexbox:2.0.1' -// implementation project(':lib') - implementation 'com.github.wangyiqian:StockChart:1.0.3' + implementation project(':lib') +// implementation 'com.github.wangyiqian:StockChart:1.0.3' } \ No newline at end of file