iOS vs Android - How to Draw: Styling
The series comparing the mobile operating systems moves onto the differences in styling.
Join the DZone community and get the full member experience.Join For Free
My blog posts until this point have mainly revolved around programming for iOS in Objective-C. There have been some posts around C#, however, they're more about switching from C# to Objective-C.
However, the past year, I've been splitting my time equally between iOS and Android development.
Since more and more mobile app developers are supporting both platforms simultaneously, I figured why not blog about both platforms!
This series of posts will revolve around drawing various things in both platforms. You'll be pleasantly surprised to see that both are actually very similar!
Part 2 - Styling
Part 2 - Styling
Next, we'll talk about how to set colors, stroke, and other painting type flags for your drawings.
This is where Android and iOS have a slight deviation. Whereas in iOS you can set these properties directly on the CGContextRef, in Android you must create a secondary object called Paint, where all of these properties live.
First let's look at a basic one, Background Color
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
Paint p = new Paint(); p.setColor(Color.RED);
Note, how in the Android snippet, we had to create a paint object. While in the iOS Snippet, we use our context, and call a static method to set the color.
Now let's look at adding a Stroke, this is a bit more tricky:
CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor); CGContextSetLineWidth(ctx, 2);
Paint p = new Paint();p.setColor(Color.BLACK);p.setStrokeWidth(2);
in iOS, its just like we did with Background Color, we just the appropriate properties, although instead of it being called stroke width, its called LineWidth. In Android, though, you should note that the color property we're setting is the SAME property we set for the background color. That means if you want the stroke to be the same color as the background color, that you'll have to draw your path twice, with 2 different paint objects.
A convenient trick when drawing is to clip your region. For example, you may want to limit what the user sees, but it'd be hard to draw to that limit. SO instead, you draw outside your viewport, but you clip everything outside of it:
CGContextClipToRect(ctx, CGRectMake(0, 0, 100, 100));
canvas.clipRect(new android.graphics.Rect(0, 0, 100, 100));
This next part isn't quite styling, but it does affect the out come of the drawing. Transforms. With Transforms, you can rotate or move the point at which you're drawing into in your drawing context.
In Android and iOS they're both handled very similar:
CGContextTranslateCTM(ctx, x, y); CGContextRotateCTM(ctx, radians);
canvas.translate(x, y); canvas.rotate(degrees);
In iOS, it's worth noting that Rotate is in Radians while in Android it's in Degrees!
Finally, in drawing frameworks, you're generally passing around your context (CGContextRef and Canvas), which means that certain styling properties or transforms may be applied to your context, that shouldn't be applied everywhere. For example, i maybe set a rotate transform for a rectangle that i'm drawing. However, when i go to draw a line later on, i might not want that transform! Does that mean i have to undo the transform? Or is there a shortcut?
That's where Save/Restore come into play:
CGContextSaveGState(ctx); // Drawing Code CGContextRestoreGState(ctx);
canvas.save(); // Drawing Code canvas.restore();
Basically, when you call save on your context, you're saying remember all the settings i had in place before i saved, and when you call restore later, its restores it back to your last save. And save/restore are stack based, meaning you can call save multiple times, and then restore later on:
Save() // 1 Save() // 2 Restore() // Restore before 2 Restore() // Restore Before 1
And that covers the basics of styling.
Written by Stephen Zaharuk at Infragistics
Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.