Skip to content

10. Extension

davemorrissey edited this page Mar 23, 2015 · 6 revisions

This view has been designed from the ground up to be easily extendable, and provides several public and protected methods that allow you to inspect the state of the view, add your own overlays, and customise touch event handling. The sample app includes three simple examples of subclasses that add overlays, and these are a great place to start if you want to understand how to create a subclass of your own.

Sample classes

Class Description
PinView Displays a pin anchored to a point on the image.
CircleView Displays a circle that moves and scales with the image.
FreehandView A very simple freehand drawing demonstration. The user can draw a freehand line, which will scale and move with the image. Illustrates how to extend event detection.

Overriding onDraw

To add your own overlays, you can override the onDraw method. You must always call the superclass method from the start of your method using super.onDraw(canvas) - if you don't, the view will never initialise. You must also check the view is ready by calling isReady(). Before the view is ready, the methods listed below may throw NullPointerExceptions or return invalid coordinates.

Overriding event detection

You can override the onTouchEvent method and customise event detection in any way you want, handle some events yourself and pass others to the view. This is a complex topic and requires a strong understanding of how Android touch events work, so it's beyond the scope of this wiki. For an example, please see the FreehandView sample class.

Checking the view state

Until the dimensions of the view and the source image are known, the scale and center cannot be calculated, so it isn't possible to convert view to image coordinates or draw anchored overlays. To determine whether the view is ready to display an image (which may be the preview if you are using one), call isReady(). You can receive notification of this event by overriding onReady().

If you want to know whether the full size image (or its base layer tiles when using tiling) has been loaded, call isImageLoaded() or receive a notification by overriding onImageLoaded(). If you are not using a preview image, this event occurs at the same moment as onReady.

Clone this wiki locally