Skip to content

Commit

Permalink
提供更多的手势监听
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Nov 28, 2022
1 parent 5d59876 commit 2f4b35f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Data.loadDayData(this, 0) { kEntities: List<IKEntity> ->
|gridLineStrokeWidth|背景网格线条宽度|
|chartMainDisplayAreaPaddingLeft|主数据显示区域的左内间距,主数据一般指数据线|
|chartMainDisplayAreaPaddingRight|主数据显示区域的右内间距,主数据一般指数据线|
|onGestureListener|手势监听|


### K线图配置`KChartConfig`
Expand Down Expand Up @@ -183,7 +184,7 @@ Data.loadDayData(this, 0) { kEntities: List<IKEntity> ->
|costPriceLineWidth|成本线宽度|
|indexStrokeWidth|指标线条宽度|
|barSpaceRatio|柱子之间的空间占比柱子宽度|
|index|需要展示的指标类型|
|index|需要展示的指标类型,目前只有MA、EMA、BOLL|
|indexColors|指标线的颜色|
|leftLabelConfig|左侧标签配置|
|rightLabelConfig|右侧标签配置|
Expand Down
20 changes: 19 additions & 1 deletion lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) }
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) }
}

}
Expand Down
34 changes: 31 additions & 3 deletions lib/src/main/java/com/github/wangyiqian/stockchart/TouchHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// 需要展示的指标配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,56 @@

package com.github.wangyiqian.stockchart.listener

import com.github.wangyiqian.stockchart.entities.GestureEvent

/**
* @author wangyiqian E-mail: [email protected]
* @version 创建时间: 2021/11/29
*/
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) {}
}
4 changes: 2 additions & 2 deletions samples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

0 comments on commit 2f4b35f

Please sign in to comment.