Setting Up for Image Transformation

To move and zoom the image, we'll use a neat little feature on the ImageView class called matrix transformation. Using a matrix we can represent any kind of translation, rotation, or skew that we want to do to the image. We already turned it on by specifying android:scaleType= "matrix" in the res/layout/main.xml file. In the Touch class, we need to declare two matrices as fields (one for the current value and one for the original value before the transformation). We'll use them in the onTouch() method to transform the image. We also need a mode variable to tell whether we're in the middle of a drag or zoom gesture, and we need the start, mid, and oldDist variables for controlling zooming:

Download Touchv1/src/org/example/touch/

public class Touch extends Activity implements OnTouchListener { // These matrices will be used to move and zoom image Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix();

// We can be in one of these 3 states static final int NONE = 0; static final int DRAG = 1; static final int ZOOM = 2; int mode = NONE;

// Remember some things for zooming PointF start = new PointF(); PointF mid = new PointF(); float oldDist = 1f;

@Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v;

// Dump touch event to log dumpEvent(event);

// Handle touch events here...

switch (event.getAction() & MotionEvent.ACTION_MASK) { }


return true; // indicate event was handled

The matrix variable will be calculated inside the switch statement when we implement the gestures.

0 0

Post a comment