Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

iOS vs Android: How to Draw Special Objects Part 1

DZone's Guide to

iOS vs Android: How to Draw Special Objects Part 1

Here's a guide to drawing in both iOS and Android, focused on the basics.

· Mobile Zone ·
Free Resource

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 1 - Intro

Part 2 - Styling

Part 3 - Drawing 

Part 4 - Text

Part 1 - Intro

Android and iOS are both Immediate rendering engines. So, we'll be drawing into a special object on each platform. 

In iOS, that's called CGContextRef and in Android it's called Canvas. 

In both cases, you can get a reference to these objects by overriding the respective draw methods in the respective View classes. 

iOS

@interface CustomView : UIView

@end

@implementation CustomView

-(void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
}

@end

Android

public class CustomView extends View
{
   public CustomView(Context ctx)
   {
       super(ctx);
   }

   @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }
}

So now we have our View's and we've created our respective drawing contexts. I'll cover the actual drawing in the next parts. 

There is one more thing I want to show you, though. 

You can tell iOS and Android that your View's dirty and you need to be redrawn. Once you do that, the respective draw methods will be automatically be invoked by the framework. 

iOS

 [_customView setNeedsDisplay];

Android

_customView.invalidate();

And that covers the intro. 

Stay tuned for the next post where I talk about styling and the differences between the 2 platforms. 

Topics:
android ,ios ,mobile

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}