iOS vs Android - How To Draw - Part 1 - Intro

Stephen Zaharuk / Tuesday, January 12, 2016

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 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, thats called CGContextRef and in Android its 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. 

By Stephen Zaharuk (SteveZ)