1 package com.github.mikephil.charting.listener; 2 3 import android.view.GestureDetector; 4 import android.view.MotionEvent; 5 import android.view.View; 6 7 import com.github.mikephil.charting.charts.Chart; 8 import com.github.mikephil.charting.highlight.Highlight; 9 10 /** 11 * Created by philipp on 12/06/15. 12 */ 13 public abstract class ChartTouchListener<T extends Chart<?>> extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener { 14 15 public enum ChartGesture { 16 NONE, DRAG, X_ZOOM, Y_ZOOM, PINCH_ZOOM, ROTATE, SINGLE_TAP, DOUBLE_TAP, LONG_PRESS, FLING 17 } 18 19 /** 20 * the last touch gesture that has been performed 21 **/ 22 protected ChartGesture mLastGesture = ChartGesture.NONE; 23 24 // states 25 protected static final int NONE = 0; 26 protected static final int DRAG = 1; 27 protected static final int X_ZOOM = 2; 28 protected static final int Y_ZOOM = 3; 29 protected static final int PINCH_ZOOM = 4; 30 protected static final int POST_ZOOM = 5; 31 protected static final int ROTATE = 6; 32 33 /** 34 * integer field that holds the current touch-state 35 */ 36 protected int mTouchMode = NONE; 37 38 /** 39 * the last highlighted object (via touch) 40 */ 41 protected Highlight mLastHighlighted; 42 43 /** 44 * the gesturedetector used for detecting taps and longpresses, ... 45 */ 46 protected GestureDetector mGestureDetector; 47 48 /** 49 * the chart the listener represents 50 */ 51 protected T mChart; 52 ChartTouchListener(T chart)53 public ChartTouchListener(T chart) { 54 this.mChart = chart; 55 56 mGestureDetector = new GestureDetector(chart.getContext(), this); 57 } 58 59 /** 60 * Calls the OnChartGestureListener to do the start callback 61 * 62 * @param me 63 */ startAction(MotionEvent me)64 public void startAction(MotionEvent me) { 65 66 OnChartGestureListener l = mChart.getOnChartGestureListener(); 67 68 if (l != null) 69 l.onChartGestureStart(me, mLastGesture); 70 } 71 72 /** 73 * Calls the OnChartGestureListener to do the end callback 74 * 75 * @param me 76 */ endAction(MotionEvent me)77 public void endAction(MotionEvent me) { 78 79 OnChartGestureListener l = mChart.getOnChartGestureListener(); 80 81 if (l != null) 82 l.onChartGestureEnd(me, mLastGesture); 83 } 84 85 /** 86 * Sets the last value that was highlighted via touch. 87 * 88 * @param high 89 */ setLastHighlighted(Highlight high)90 public void setLastHighlighted(Highlight high) { 91 mLastHighlighted = high; 92 } 93 94 /** 95 * returns the touch mode the listener is currently in 96 * 97 * @return 98 */ getTouchMode()99 public int getTouchMode() { 100 return mTouchMode; 101 } 102 103 /** 104 * Returns the last gesture that has been performed on the chart. 105 * 106 * @return 107 */ getLastGesture()108 public ChartGesture getLastGesture() { 109 return mLastGesture; 110 } 111 112 113 /** 114 * Perform a highlight operation. 115 * 116 * @param e 117 */ performHighlight(Highlight h, MotionEvent e)118 protected void performHighlight(Highlight h, MotionEvent e) { 119 120 if (h == null || h.equalTo(mLastHighlighted)) { 121 mChart.highlightValue(null, true); 122 mLastHighlighted = null; 123 } else { 124 mChart.highlightValue(h, true); 125 mLastHighlighted = h; 126 } 127 } 128 129 /** 130 * returns the distance between two points 131 * 132 * @param eventX 133 * @param startX 134 * @param eventY 135 * @param startY 136 * @return 137 */ distance(float eventX, float startX, float eventY, float startY)138 protected static float distance(float eventX, float startX, float eventY, float startY) { 139 float dx = eventX - startX; 140 float dy = eventY - startY; 141 return (float) Math.sqrt(dx * dx + dy * dy); 142 } 143 } 144