Putting it all together

So far, we've seen many different drawing primitives, clipping operations, and matrix transformations, but the most interesting part is when we combine it all together. In order to build great custom views, we've to use many different kinds of operations and transformations.

However, having so many operations available is a double-edged sword. We have to be careful when adding this complexity to our custom view, as we can compromise performance quite easily. We should check if we're applying, for example, too many or unnecessary clippings operations or if we aren’t optimizing enough or we aren’t maximizing reuse of clipping and transformation operations. In that case, we might even use the quickReject() method from the canvas object to quickly discard areas that will fall outside the clipping area.

Also, we need to keep track of all save() and restore() we're performing to our canvas. Performing additional restore() methods, not only means we have an issue with our code, but  is an actual error. If we have to change to different previously saved states, we can use the restoreToCount() method together with saving the state number in the call we do to save the state.

As we've mentioned before, and will mention again in the following chapters, avoid to allocate memory or create new instances of an object, inside the onDraw() method; especially remember this remark if at some point you think you have to create a new instance of a paint object inside the onDraw(). Reuse paint objects or initialize them, for instance, on the class constructor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.96.247