Rss feed of the tag

A robot with a brush : using Paint on Android (Part 1)

Written by Xavier Gouchet - 08 february 2012 -

It's rather common to work with the Canvas when building an app or a Live Wallpaper on Android. The canvas can seem very simple at first, but the Paint class can offer a lot more than what you may think.

The first thing one uses the Paint for is to draw primitives. The Canvas class comes with a limited number of primitives (which you can draw using the drawXXX(...) method). These primitives are shown in the image below : Arcs, Circles, Ovals, Lines, Points, Rectangles (with or with round corners) and text.


When drawing each primitive, a Paint instance is require, to define the color, the style (do we paint the outline, the filling, or both), and severall basic options like antialising and image filtering. But we can also add some effects on the Paint to enhance the basic primitives. For instance, blur the primitives a little, using one of the following lines (corresponding images below).

paint.setMaskFilter(new BlurMaskFilter(5, Blur.NORMAL)); 
paint.setMaskFilter(new BlurMaskFilter(5, Blur.SOLID));
paint.setMaskFilter(new BlurMaskFilter(5, Blur.INNER));
paint.setMaskFilter(new BlurMaskFilter(5, Blur.OUTER));


This effect was really trendy in the 1990s internet, and still in most Powerpoint slideshows. The emboss will make anything look like a 3D object, with simulated highlight and shadow on the edges (like on the image below).

paint.setMaskFilter(new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.5f, 4.0f, 10.0f));


Another trendy effect is the shadow, to simulate a ... well you guessed, a shadow on the canvas behind the primitive being drawn.

paint.setShadowLayer(5, 10, 5, Color.LTGRAY);