2 * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 package org.achartengine;
\r
18 import org.achartengine.chart.AbstractChart;
\r
19 import org.achartengine.chart.RoundChart;
\r
20 import org.achartengine.chart.XYChart;
\r
21 import org.achartengine.renderer.DefaultRenderer;
\r
22 import org.achartengine.tools.Pan;
\r
23 import org.achartengine.tools.PanListener;
\r
24 import org.achartengine.tools.ZoomListener;
\r
26 import android.graphics.RectF;
\r
27 import android.view.MotionEvent;
\r
30 * A handler implementation for touch events for older platforms.
\r
32 public class TouchHandlerOld implements ITouchHandler {
\r
33 /** The chart renderer. */
\r
34 private DefaultRenderer mRenderer;
\r
35 /** The old x coordinate. */
\r
37 /** The old y coordinate. */
\r
39 /** The zoom buttons rectangle. */
\r
40 private RectF zoomR = new RectF();
\r
41 /** The pan tool. */
\r
43 /** The graphical view. */
\r
44 private GraphicalView graphicalView;
\r
47 * Creates an implementation of the old version of the touch handler.
\r
49 * @param view the graphical view
\r
50 * @param chart the chart to be drawn
\r
52 public TouchHandlerOld(GraphicalView view, AbstractChart chart) {
\r
53 graphicalView = view;
\r
54 zoomR = graphicalView.getZoomRectangle();
\r
55 if (chart instanceof XYChart) {
\r
56 mRenderer = ((XYChart) chart).getRenderer();
\r
58 mRenderer = ((RoundChart) chart).getRenderer();
\r
60 if (mRenderer.isPanEnabled()) {
\r
61 mPan = new Pan(chart);
\r
65 public boolean handleTouch(MotionEvent event) {
\r
66 int action = event.getAction();
\r
67 if (mRenderer != null && action == MotionEvent.ACTION_MOVE) {
\r
68 if (oldX >= 0 || oldY >= 0) {
\r
69 float newX = event.getX();
\r
70 float newY = event.getY();
\r
71 if (mRenderer.isPanEnabled()) {
\r
72 mPan.apply(oldX, oldY, newX, newY);
\r
76 graphicalView.repaint();
\r
79 } else if (action == MotionEvent.ACTION_DOWN) {
\r
80 oldX = event.getX();
\r
81 oldY = event.getY();
\r
82 if (mRenderer != null && mRenderer.isZoomEnabled() && zoomR.contains(oldX, oldY)) {
\r
83 if (oldX < zoomR.left + zoomR.width() / 3) {
\r
84 graphicalView.zoomIn();
\r
85 } else if (oldX < zoomR.left + zoomR.width() * 2 / 3) {
\r
86 graphicalView.zoomOut();
\r
88 graphicalView.zoomReset();
\r
92 } else if (action == MotionEvent.ACTION_UP) {
\r
96 return !mRenderer.isClickEnabled();
\r
100 * Adds a new zoom listener.
\r
102 * @param listener zoom listener
\r
104 public void addZoomListener(ZoomListener listener) {
\r
108 * Removes a zoom listener.
\r
110 * @param listener zoom listener
\r
112 public void removeZoomListener(ZoomListener listener) {
\r
116 * Adds a new pan listener.
\r
118 * @param listener pan listener
\r
120 public void addPanListener(PanListener listener) {
\r
121 if (mPan != null) {
\r
122 mPan.addPanListener(listener);
\r
127 * Removes a pan listener.
\r
129 * @param listener pan listener
\r
131 public void removePanListener(PanListener listener) {
\r
132 if (mPan != null) {
\r
133 mPan.removePanListener(listener);
\r