Bitmap initialization code

@Override protected void onSizeChanged (int w, int h, int oldw, int oldh) { Bitmap img =

Bitmap.createBitmap(w, h,Bitmap.Config.ARGB_8888) ; <—

Canvas canvas = new Canvas();

Add contextual data to image

Convert image to JPEG

canvas.drawBitmap(img, 0, 0, null);

_canvas.drawColor(Color.WHITE); }

@Override protected void onDraw(Canvas canvas) { if (_bitmap != null) { O Draw image canvas .drawBitmap (_bitmap, 0, 0, null); <-' on screen

@Override G Handle Touch Events public boolean onTouchEvent(MotionEvent event) { <1—I 0-e-> capture signature!) int action = event.getAction(); int X = (int)event.getX(); int Y = (int)event.getY(); switch (action ) { case MotionEvent.ACTION_UP: // reset location lastX = -1; break;

case MotionEvent.ACTION_DOWN: if (lastX != -1){

if ((int) event.getX() != lastX) { _canvas.drawLine(lastX, lastY, X, Y, _paint);

lastX = (int)event.getX(); lastY = (int)event.getY(); break;

case MotionEvent.ACTION_MOVE: if (lastX != -1){ _canvas.drawLine(lastX, lastY, X, Y, _paint);

lastX = (int)event.getX(); lastY = (int)event.getY(); break;

invalidate(); return true;

The closejobView extends the base View class O- The Bitmap and Canvas classes Q work together to form the drawing surface for this Activity. Note the call to the Canvas.drawColor method, which sets the background color to WHITE. When the onDraw() method is invoked, the canvas draws its associated bitmap with a call to drawBitmap() ©.

The logic for where to draw relies on the onTouchEvent method G, which receives an instance of the MotionEvent class. The MotionEvent class tells what happened and where. ACTION_UP, ACTION_DOWN, and ACTION_MOVE are the events captured, with some logic to guide when and where to draw. Once the signature is complete, the Save method © is responsible for converting the contents of the image to a form usable for submission to the server. Note that additional text is drawn on the signature Q. In this case, it is little more than a shameless plug for this book's webpage; however, this could also be location-based data. Why is this important? Imagine someone forging a signature. Could happen, but it would be more challenging and of less value to a rogue mobile technician if the GPS/location data were actually stamped on the job, along with the date and time. When converting the image to our desired JPEG format, there is an additional input argument to this method—an OutputStream, used to store the image data. This OutputStream reference was actually an input argument to the Save method.

Now that the UI has been created and a signature drawn on the screen, let's look at the code used to close thejob. Closing thejob involves capturing the signature and sending it to the server via an HTTP POST. The class DoCloseJob is shown in listing 12.19.

0 0

Post a comment