Skip to content

Commit

Permalink
支持监听手势缩放
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Nov 29, 2021
1 parent 57486fc commit 3edce61
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ interface IStockChart {
* 获取显示区域最后一个不为空的K线数据点下标
*/
fun findFirstNotEmptyKEntityIdxInDisplayArea(): Int?

/**
* 获取缩放比例
*/
fun getTotalScaleX(): Float

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,19 @@ class MatrixHelper(private val stockChart: IStockChart) {
scalePx = tmp2FloatArray[0]
}

// 目前总共缩放了多少
fun getTotalScaleX(): Float{
xScaleMatrix.getValues(tmp9FloatArray)
return tmp9FloatArray[Matrix.MSCALE_X]
}

/**
* 处理双指缩放
*/
fun handleTouchScale(scaleFactor: Float) {
fixXScaleMatrix.reset()

// 计算目前总共缩放了多少
xScaleMatrix.getValues(tmp9FloatArray)
val totalScaleX = tmp9FloatArray[Matrix.MSCALE_X]
val totalScaleX = getTotalScaleX()

// 计算在限制范围内要缩放多少
var targetScaleFactor = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
return result
}

override fun getTotalScaleX() = matrixHelper.getTotalScaleX()

private fun checkChildViews() {
var needReAddViews = false // 是否需要重新添加view
var needRequestLayout = false // 是否需要重新requestLayout
Expand Down Expand Up @@ -325,6 +327,7 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
if (getConfig().scaleAble) {
requestDisallowInterceptTouchEvent(true)
matrixHelper.handleTouchScale(scaleFactor)
getConfig().getOnGestureListeners().forEach { it.onScaling(getTotalScaleX()) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.annotation.ColorInt
import androidx.annotation.UiThread
import com.github.wangyiqian.stockchart.childchart.base.*
import com.github.wangyiqian.stockchart.entities.IKEntity
import com.github.wangyiqian.stockchart.listener.OnGestureListener
import com.github.wangyiqian.stockchart.listener.OnLoadMoreListener
import com.github.wangyiqian.stockchart.util.checkMainThread

Expand Down Expand Up @@ -139,10 +140,29 @@ class StockChartConfig {
// 背景网格线条宽度
var gridLineStrokeWidth = DEFAULT_GRID_LINE_STROKE_WIDTH

// 加载更多监听
var onLoadMoreListener: OnLoadMoreListener? = null
set(value) {
field?.apply { removeOnLoadMoreListener(this) }
value?.apply { addOnLoadMoreListener(this)}
field = value

}

// 手势监听
var onGestureListener: OnGestureListener? = null
set(value) {
field?.apply { removeOnGestureListener(this) }
value?.apply { addOnGestureListener(this) }
field = value
}

val childChartFactories = mutableListOf<AbsChildChartFactory<*>>()

private var onLoadMoreListeners = mutableSetOf<OnLoadMoreListener>()

private var onGestureListeners = mutableSetOf<OnGestureListener>()

fun addChildCharts(vararg childChartFactories: AbsChildChartFactory<*>) {
this.childChartFactories.addAll(childChartFactories.toList())
}
Expand Down Expand Up @@ -251,4 +271,20 @@ class StockChartConfig {

fun getOnLoadMoreListeners() = onLoadMoreListeners

/**
* 添加手势监听
*/
fun addOnGestureListener(listener: OnGestureListener){
onGestureListeners.add(listener)
}

/**
* 移除手势监听
*/
fun removeOnGestureListener(listener: OnGestureListener){
onGestureListeners.remove(listener)
}

fun getOnGestureListeners() = onGestureListeners

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.listener

/**
* @author wangyiqian E-mail: [email protected]
* @version 创建时间: 2021/11/29
*/
interface OnGestureListener {

/**
* 是否正在缩放
*/
fun onScaling(totalScaleX: Float){}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package com.github.wangyiqian.stockchart.sample.sample2

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -34,6 +35,7 @@ import com.github.wangyiqian.stockchart.entities.EmptyKEntity
import com.github.wangyiqian.stockchart.entities.Highlight
import com.github.wangyiqian.stockchart.entities.IKEntity
import com.github.wangyiqian.stockchart.index.Index
import com.github.wangyiqian.stockchart.listener.OnGestureListener
import com.github.wangyiqian.stockchart.listener.OnHighlightListener
import com.github.wangyiqian.stockchart.listener.OnLoadMoreListener
import com.github.wangyiqian.stockchart.sample.DataMock
Expand All @@ -44,6 +46,7 @@ import com.github.wangyiqian.stockchart.sample.sample2.custom.CustomChartFactory
import com.github.wangyiqian.stockchart.util.DimensionUtil
import com.github.wangyiqian.stockchart.util.NumberFormatUtil
import kotlinx.android.synthetic.main.activity_sample2.*
import kotlinx.android.synthetic.main.activity_sample3.*
import kotlinx.android.synthetic.main.layout_sample2_option_buttons.*

/**
Expand Down Expand Up @@ -159,7 +162,7 @@ class Sample2Activity : AppCompatActivity() {
gridHorizontalLineCount = 4

// 设置滑动到左边界加载更多
addOnLoadMoreListener(object : OnLoadMoreListener {
onLoadMoreListener = object : OnLoadMoreListener {
override fun onLeftLoadMore() {
if (!isLoading) {
if (period != Period.FIVE_DAYS
Expand All @@ -174,7 +177,7 @@ class Sample2Activity : AppCompatActivity() {
}

override fun onRightLoadMore() {}
})
}
}


Expand Down

0 comments on commit 3edce61

Please sign in to comment.