MPAndroidChart 提供了许多用于交互回调的方法,其中 OnChartValueSelectedListener
在点击高亮值时回调。
public interface OnChartValueSelectedListener {
/**
-
Called when a value has been selected inside the chart.
-
@param e The selected Entry.
-
@param dataSetIndex The index in the datasets array of the data object
-
the Entrys DataSet is in.
-
@param h the corresponding highlight object that contains information
-
about the highlighted position
*/
public void onValueSelected(Entry e, int dataSetIndex, Highlight h);
/**
- Called when nothing has been selected or an “un-select” has been made.
*/
public void onNothingSelected();
}
让你的类实现该接口并设置对 chart 进行监听,即可接受回调。
Simply let your class that should receive the callbacks implement this interface and set it as a listener to the chart:
chart.setOnChartValueSelectedListener(this);
手势回调
监听器 OnChartGestureListener
可以使得 chart 与手势操作进行交互。
public interface OnChartGestureListener {
/**
-
Callbacks when a touch-gesture has started on the chart (ACTION_DOWN)
-
@param me
-
@param lastPerformedGesture
*/
void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);
/**
-
Callbacks when a touch-gesture has ended on the chart (ACTION_UP, ACTION_CANCEL)
-
@param me
-
@param lastPerformedGesture
*/
void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);
/**
-
Callbacks when the chart is longpressed.
-
@param me
*/
public void onChartLongPressed(MotionEvent me);
/**
-
Callbacks when the chart is double-tapped.
-
@param me
*/
public void onChartDoubleTapped(MotionEvent me);
/**
-
Callbacks when the chart is single-tapped.
-
@param me
*/
public void onChartSingleTapped(MotionEvent me);
/**
-
Callbacks then a fling gesture is made on the chart.
-
@param me1
-
@param me2
-
@param velocityX
-
@param velocityY
*/
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY);
/**
-
Callbacks when the chart is scaled / zoomed via pinch zoom gesture.
-
@param me
-
@param scaleX scalefactor on the x-axis
-
@param scaleY scalefactor on the y-axis
*/
public void onChartScale(MotionEvent me, float scaleX, float scaleY);
/**
-
Callbacks when the chart is moved / translated via drag gesture.
-
@param me
-
@param dX translation distance on the x-axis
-
@param dY translation distance on the y-axis
*/
public void onChartTranslate(MotionEvent me, float dX, float dY);
}
让你的类实现该接口并设置对 chart 进行监听,即可接受回调。
chart.setOnChartGestureListener(this);
设置了监听器后,chart 会根据你的 setXXXEnable()
进行放缩移动等操作。不用在接口方法里对图表进行放缩移动等其他操作,接口方法可以让你实现其他对应功能,比如说你要打印放缩是的 ScaleX,ScaleY
:
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
Log.i(“Scale / Zoom”, "ScaleX: " + scaleX + ", ScaleY: " + scaleY);
}
打印的日志类似:
I/Gesture: START
I/Scale / Zoom: ScaleX: 1.0, ScaleY: 1.0
I/Scale / Zoom: ScaleX: 1.0, ScaleY: 1.0
I/Scale / Zoom: ScaleX: 1.0174584, ScaleY: 1.0174584
I/Scale / Zoom: ScaleX: 1.240304, ScaleY: 1.240304
I/Scale / Zoom: ScaleX: 1.4446417, ScaleY: 1.4446417
I/Scale / Zoom: ScaleX: 1.5617653, ScaleY: 1.5617653
I/Scale / Zoom: ScaleX: 1.0241176, ScaleY: 1.0241176
I/Scale / Zoom: ScaleX: 1.1038365, ScaleY: 1.1038365
I/Gesture: END, lastGesture: PINCH_ZOOM
下面是练习时写的一些 OnChartGestureListener
接口实现方法:
@Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
Log.i(“Gesture”, “START”);
}
@Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
Log.i(“Gesture”, "END, lastGesture: " + lastPerformedGesture);
// un-highlight values after the gesture is finished and no single-tap
if (lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP)
mChart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(…)
}
@Override
public void onChartLongPressed(MotionEvent me) {
Log.i(“LongPress”, “Chart longpressed.”);
}
@Override
public void onChartDoubleTapped(MotionEvent me) {
Log.i(“DoubleTap”, “Chart double-tapped.”);
}
@Override
public void onChartSingleTapped(MotionEvent me) {
Log.i(“SingleTap”, “Chart single-tapped.”);
}
@Override
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
-shRpJITg-1715710268028)]
[外链图片转存中…(img-diSGiLQI-1715710268030)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!