From 2f4b35f1316c8301c8be5846c95edf402e568bb6 Mon Sep 17 00:00:00 2001 From: wangyiqian Date: Mon, 28 Nov 2022 22:33:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9B=E6=9B=B4=E5=A4=9A=E7=9A=84?= =?UTF-8?q?=E6=89=8B=E5=8A=BF=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +- .../wangyiqian/stockchart/StockChart.kt | 20 +++++++- .../wangyiqian/stockchart/TouchHelper.kt | 34 ++++++++++++-- .../childchart/macdchart/MacdChartConfig.kt | 6 +-- .../stockchart/listener/OnGestureListener.kt | 46 ++++++++++++++++++- samples/build.gradle | 4 +- 6 files changed, 101 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index eef2fac..d220f7e 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ Data.loadDayData(this, 0) { kEntities: List -> |gridLineStrokeWidth|背景网格线条宽度| |chartMainDisplayAreaPaddingLeft|主数据显示区域的左内间距,主数据一般指数据线| |chartMainDisplayAreaPaddingRight|主数据显示区域的右内间距,主数据一般指数据线| +|onGestureListener|手势监听| ### K线图配置`KChartConfig` @@ -183,7 +184,7 @@ Data.loadDayData(this, 0) { kEntities: List -> |costPriceLineWidth|成本线宽度| |indexStrokeWidth|指标线条宽度| |barSpaceRatio|柱子之间的空间占比柱子宽度| -|index|需要展示的指标类型| +|index|需要展示的指标类型,目前只有MA、EMA、BOLL| |indexColors|指标线的颜色| |leftLabelConfig|左侧标签配置| |rightLabelConfig|右侧标签配置| 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 8eaf7b7..fa4daac 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt @@ -282,7 +282,7 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet for (i in 1..config.gridHorizontalLineCount) { canvas.drawLine( - config.horizontalGridLineLeftOffsetCalculator?.invoke(this)?:0f, + config.horizontalGridLineLeftOffsetCalculator?.invoke(this) ?: 0f, top, width.toFloat(), top, @@ -312,6 +312,7 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet if (getConfig().scaleAble) { requestDisallowInterceptTouchEvent(true) matrixHelper.handleTouchScaleBegin(focusX) + getConfig().getOnGestureListeners().forEach { it.onScaleBegin(focusX) } } } @@ -327,11 +328,13 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet if (getConfig().scrollAble) { requestDisallowInterceptTouchEvent(true) matrixHelper.handleTouchScroll(distanceX) + getConfig().getOnGestureListeners().forEach { it.onHScrolling() } } } override fun onTriggerFling(velocityX: Float, velocityY: Float) { matrixHelper.handleFlingStart(velocityX, velocityY) + getConfig().getOnGestureListeners().forEach { it.onFlingBegin() } } override fun onLongPressMove(x: Float, y: Float) { @@ -369,9 +372,11 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet highlightMap.keys.forEach { it.getConfig().onHighlightListener?.onHighlightEnd() } + getConfig().getOnGestureListeners().forEach { it.onTouchLeave() } highlightMap.clear() notifyChanged() matrixHelper.checkScrollBack() + } override fun onTap(x: Float, y: Float) { @@ -386,6 +391,19 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet val gestureEvent = GestureEvent(childChartX, childChartY, valueX, valueY) childChart.onTap(gestureEvent) } + getConfig().getOnGestureListeners().forEach { it.onTap(x, y) } + } + + override fun onLongPressBegin(x: Float, y: Float) { + getConfig().getOnGestureListeners().forEach { it.onLongPressBegin(x, y) } + } + + override fun onLongPressing(x: Float, y: Float) { + getConfig().getOnGestureListeners().forEach { it.onLongPressing(x, y) } + } + + override fun onLongPressEnd(x: Float, y: Float) { + getConfig().getOnGestureListeners().forEach { it.onLongPressEnd(x, y) } } } 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 103be3c..23bec88 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/TouchHelper.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/TouchHelper.kt @@ -61,21 +61,29 @@ internal class TouchHelper(private val stockChart: IStockChart, private val call MotionEvent.ACTION_MOVE -> { if (isLongPressing && event.getPointerId(event.actionIndex) == inLongPressingPointerId) { callBack.onLongPressMove(event.x, event.y) + callBack.onLongPressing(event.x, event.y) } } MotionEvent.ACTION_UP -> { - isLongPressing = false + if (isLongPressing) { + isLongPressing = false + callBack.onLongPressEnd(event.x, event.y) + } isTouchScalePointersLeave = true callBack.onTouchLeave() } MotionEvent.ACTION_CANCEL -> { - isLongPressing = false + if (isLongPressing) { + isLongPressing = false + callBack.onLongPressEnd(event.x, event.y) + } isTouchScalePointersLeave = true callBack.onTouchLeave() } MotionEvent.ACTION_POINTER_UP -> { if (isLongPressing && event.getPointerId(event.actionIndex) == inLongPressingPointerId) { isLongPressing = false + callBack.onLongPressEnd(event.x, event.y) } } } @@ -97,7 +105,12 @@ internal class TouchHelper(private val stockChart: IStockChart, private val call } override fun onLongPress(e: MotionEvent) { - isLongPressing = true + if (!isLongPressing) { + isLongPressing = true + callBack.onLongPressBegin(e.x, e.y) + } else { + callBack.onLongPressing(e.x, e.y) + } inLongPressingPointerId = e.getPointerId(0) callBack.onLongPressMove(e.x, e.y) super.onLongPress(e) @@ -190,5 +203,20 @@ internal class TouchHelper(private val stockChart: IStockChart, private val call * 点击 */ fun onTap(x: Float, y: Float) + + /** + * 开始长按 + */ + fun onLongPressBegin(x: Float, y: Float) {} + + /** + * 长按中 + */ + fun onLongPressing(x: Float, y: Float) {} + + /** + * 结束长按 + */ + fun onLongPressEnd(x: Float, y: Float) {} } } \ No newline at end of file diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/macdchart/MacdChartConfig.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/macdchart/MacdChartConfig.kt index 8ebc6d5..70b9b9d 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/macdchart/MacdChartConfig.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/macdchart/MacdChartConfig.kt @@ -36,13 +36,13 @@ class MacdChartConfig( // dif线颜色 var difLineColor: Int = DEFAULT_MACD_DIF_LINE_COLOR, // dif线宽度 - val difLineStrokeWidth: Float = DEFAULT_MACD_DIF_LINE_STROKE_WIDTH, + var difLineStrokeWidth: Float = DEFAULT_MACD_DIF_LINE_STROKE_WIDTH, // dea线颜色 var deaLineColor: Int = DEFAULT_MACD_DEA_LINE_COLOR, // dea线宽度 - val deaLineStrokeWidth: Float = DEFAULT_MACD_DEA_LINE_STROKE_WIDTH, + var deaLineStrokeWidth: Float = DEFAULT_MACD_DEA_LINE_STROKE_WIDTH, // macd文字颜色 - val macdTextColor: Int = DEFAULT_MACD_TEXT_COLOR, + var macdTextColor: Int = DEFAULT_MACD_TEXT_COLOR, // 柱子之间的空间占比柱子宽度 var barSpaceRatio: Float = DEFAULT_MACD_BAR_SPACE_RATIO, // 需要展示的指标配置 diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/listener/OnGestureListener.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/listener/OnGestureListener.kt index 59effce..b4df470 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/listener/OnGestureListener.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/listener/OnGestureListener.kt @@ -13,6 +13,8 @@ package com.github.wangyiqian.stockchart.listener +import com.github.wangyiqian.stockchart.entities.GestureEvent + /** * @author wangyiqian E-mail: wangyiqian9891@gmail.com * @version 创建时间: 2021/11/29 @@ -20,7 +22,47 @@ package com.github.wangyiqian.stockchart.listener interface OnGestureListener { /** - * 是否正在缩放 + * 滑动中 + */ + fun onHScrolling() {} + + /** + * 开始fling + */ + fun onFlingBegin() {} + + /** + * 手指离开屏幕 + */ + fun onTouchLeave() {} + + /** + * 开始缩放 + */ + fun onScaleBegin(focusX: Float) {} + + /** + * 缩放中 + */ + fun onScaling(totalScaleX: Float) {} + + /** + * 单击 + */ + fun onTap(x: Float, y: Float) {} + + /** + * 开始长按 + */ + fun onLongPressBegin(x: Float, y: Float) {} + + /** + * 长按中 + */ + fun onLongPressing(x: Float, y: Float) {} + + /** + * 结束长按 */ - fun onScaling(totalScaleX: Float){} + fun onLongPressEnd(x: Float, y: Float) {} } \ No newline at end of file diff --git a/samples/build.gradle b/samples/build.gradle index 09248c9..76d40c4 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.1.11' + implementation project(':lib') +// implementation 'com.github.wangyiqian:StockChart:1.1.11' } \ No newline at end of file