App
DisplayerTouchNavigator.java
Go to the documentation of this file.
1 package mhr.appandroid.displayer;
2 
3 import android.content.Context;
4 import android.view.GestureDetector;
5 import android.view.MotionEvent;
6 import android.view.ScaleGestureDetector;
7 import android.view.View;
8 import android.view.View.OnTouchListener;
9 
20 public class DisplayerTouchNavigator implements OnTouchListener {
21 
22  //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
23  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
29  protected class GestureListener extends GestureDetector.SimpleOnGestureListener {
30 
32  @Override
33  public boolean onDown(MotionEvent e) {
34  return true; // Vždy musí být přepsáno a vracet true, jinak se nepokračuje ve zpracování gesta.
35  }
36 
38  @Override
39  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
40  disp.movePositionBy(-distanceX, -distanceY);
41  return true;
42  }
43  }
44 
50  protected class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
51 
53  @Override
54  public boolean onScaleBegin(ScaleGestureDetector detector) {
55  lastX = detector.getFocusX();
56  lastY = detector.getFocusY();
57  return true;
58  }
59 
61  @Override
62  public boolean onScale(ScaleGestureDetector detector) {
63  final float x = detector.getFocusX();
64  final float y = detector.getFocusY();
65  disp.movePositionBy(x - lastX, y - lastY);
66  lastX = x;
67  lastY = y;
68  disp.adjustZoom(detector.getScaleFactor(), x, y);
69  return true;
70  }
71 
73  @Override
74  public void onScaleEnd(ScaleGestureDetector detector) {
75  }
76  }
77 
78  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
79 
80  //===== FIELDS ==============================================================================================================================//
81  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
82  protected Context context = null;
83  protected BitmapDisplayer disp = null;
84  protected GestureDetector gestureDetector = null;
85  protected ScaleGestureDetector scaleDetector = null;
86 
87  protected float lastX;
88  protected float lastY;
89 
90  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
91 
92  //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
93  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
94  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
105  this.disp = disp;
106  this.context = context;
107  gestureDetector = new GestureDetector(context, new GestureListener());
108  scaleDetector = new ScaleGestureDetector(context, new ScaleListener());
109  disp.getSurfaceView().setOnTouchListener(this);
110  }
111 
112  //===== METHODS =============================================================================================================================//
113  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
114  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
115 
117  @Override
118  public boolean onTouch(View v, MotionEvent ev) {
119  scaleDetector.onTouchEvent(ev);
120  gestureDetector.onTouchEvent(ev);
121  return true; // Zpracovali jsme
122  }
123 
124 }