Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 31  Custom Views and Touch Events


Tracking across motion events


BoxDrawingView is intended to draw boxes on the screen, not just log coordinates. There are a few
problems to solve to get there.


First, to define a box, you need two points: the origin point (where the finger was initially placed) and
the current point (where the finger currently is).


To define a box, then, requires keeping track of data from more than one MotionEvent. You will store
this data in a Box object.


Create a class named Box to represent the data that defines a single box.


Listing 31.6  Adding Box (Box.java)


public class Box {
private PointF mOrigin;
private PointF mCurrent;


public Box(PointF origin) {
mOrigin = origin;
mCurrent = origin;
}


public PointF getCurrent() {
return mCurrent;
}


public void setCurrent(PointF current) {
mCurrent = current;
}


public PointF getOrigin() {
return mOrigin;
}
}


When the user touches BoxDrawingView, a new Box will be created and added to a list of existing boxes
(Figure 31.4).


Figure 31.4  Objects in DragAndDraw

Free download pdf