Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mobile Computing With Android ACST 4550 Bitmaps, Fonts and Gestures

Similar presentations


Presentation on theme: "Mobile Computing With Android ACST 4550 Bitmaps, Fonts and Gestures"— Presentation transcript:

1 Mobile Computing With Android ACST 4550 Bitmaps, Fonts and Gestures

2 Bitmaps In the Canvas class, Bitmap objects are used instead of standard Java Image objects to show images. PNG, GIF and JPEG images are supported. To load an image file, you must put it in one of the “drawable” folders and then use a command like the following that does not include the image’s file extension in the filename: Bitmap bmp = BitmapFactory.decodeResource(resource, R.drawable.filename); Ex: Bitmap carRight = BitmapFactory.decodeResource(resource, R.drawable.carright); The resource object is somewhat like the this keyword in that it accesses the current objects resource folders. You usually retrieve it using the getResources() method if you are in a View or the getApplicationResources() method in an Activity.

3 Bitmaps Once you have the Bitmap object you can place it anywhere on the canvas using its (x,y) coordinate. For instance, you could display the bitmap at position (40,20) as its top left corner like so: canvas.drawBitmap(carRight,40,20,paint); See SnowmanCarView.java

4 Bitmaps If you want to resize the bitmap, you need to create a Rect or RectF object that has the new width and height size dimensions in it, along with the (x,y) top left component. Then you can use that object to determine the location and the size of the bitmap like so: Rect newDim = new Rect(x,y,x+newWidth,y+newHeight); canvas.drawBitmap(carRight,null,newDim,paint);

5 Bitmaps If you want to draw just a portion of the bitmap, you can provide two Rect objects, where the first has the portion of the original Bitmap dimensions you wish to use, the second has the place you would like two draw that portion on the canvas. The following lines draws the top left quarter corner of a bitmap onto the top left corner of the screen at whatever new dimensions are desired: Rect oldDim = new Rect(0,0,bmpWidth/2,bmpHeight/2); Rect newDim = new Rect(0,0,newWidth,newHeight); canvas.drawBitmap(bmp,oldDim,newDim,paint);

6 Bitmaps If you want a bitmap to blend into its background, you can do so by setting the alpha value of the paint to whatever transparency level you want. The following example draws the bitmap at a half transparent alpha value because 128 is about half of 255: paint.setColor(Color.argb(128,0,0,0)); canvas.drawBitmap(bmp,x,y,paint);

7 Fonts Although you can load your own font files into the values resource folder, the Android OS only comes loaded with 3 basic fonts: MONOSPACE => fixed width font SERIF => fancier font (like Times Roman) SANS_SERIF => fancier font without feet You can use the following TypeFace styles with these fonts: TypeFace.NORMAL TypeFace.BOLD TypeFace.ITALIC TypeFace.BOLD_ITALIC See FontDemoView.java

8 FontMetrics The FontMetrics class provides you with information about the rendering of a particular Font using a particular Paint object. The FontMetrics object is returned by the getFontMetrics() method of the Paint class. There are no FontMetrics methods, but it provides the following 5 fields which give you the Font’s ascent and descent details : ascent, bottom, descent, leading and top. The FontMetrics class doesn’t provide a stringWidth() method to help with centering text on a Canvas, so instead you would use the measureText(String) method of the Paint class to do this like so, at some vertical position (vPos): paint.setTypeface(Typeface.create("SANS_SERIF",Typeface.BOLD)); paint.setTextSize(24); int labelWidth = (int) paint.measureText(label); int scrWidth = canvas.getWidth(); canvas.drawText(label,(scrWidth-labelWidth)/2, vPos, paint);

9 onTouchEvent() The onTouchEvent() handler is a simple method for handling touch screen motion events. If the screen is touched in a specific view, this method for that view is called. The argument sent to this method if a MotionEvent object. The method returns a boolean becauseiIf this method handles the event (by responding to it) the method should return true. The MotionEvent class has a getAction() method that reports a number of actions that might have occurred to trigger this event. We will only address three of them: ACTION_UP - A pressed gesture has finished ACTION_DOWN - A pressed gesture has started ACTION_MOVE - A change happened during a press gesture

10 onTouchEvent() The MotionEvent class also has a getX() and a getY() method that reports the x and y coordinate (in floats) of the point where the touch event occurred. Example: public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); // Here you would use the touch coordinates for some action } return true;

11 onSizeChanged() The onSizeChanged() handler is a simple method that is called whenever the size of the view has changed. You can use this method to find out the size of your view window because when the size of the view is first determined as your app is first starting up, this method is called. The following are the fields sent to this method: w the current width of this view h the current height of this view oldw the old width of this view, zero if first time oldh the old height of this view, zero if first time


Download ppt "Mobile Computing With Android ACST 4550 Bitmaps, Fonts and Gestures"

Similar presentations


Ads by Google