DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • HTML5 on Android 4.0: Way Better, Still Behind iOS 5
  • How to Make Android Development Less Frustrating
  • How it Feels to Switch from Eclipse to Android Studio
  • The 12 Biggest Android App Development Trends in 2023

Trending

  • Modular Software Architecture: Advantages and Disadvantages of Using Monolith, Microservices and Modular Monolith
  • Exploring Sorting Algorithms: A Comprehensive Guide
  • Chronicle Services: Low Latency Java Microservices Without Pain
  • Cognitive AI: The Road To AI That Thinks Like a Human Being
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. iOS vs Android - How to Draw: Styling

iOS vs Android - How to Draw: Styling

The series comparing the mobile operating systems moves onto the differences in styling.

Josh Anderson user avatar by
Josh Anderson
·
Jan. 15, 16 · Tutorial
Like (3)
Save
Tweet
Share
2.73K Views

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

Part 2 - Styling

Part 3 - Drawing Shapes 

Part 4 - Drawing Text

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

iOS

CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);

Android

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:

iOS

CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextSetLineWidth(ctx, 2);

Android

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:

iOS

CGContextClipToRect(ctx, CGRectMake(0, 0, 100, 100));

Android

 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:

iOS

CGContextTranslateCTM(ctx, x, y);
CGContextRotateCTM(ctx, radians);

Android

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:

iOS

CGContextSaveGState(ctx);
// Drawing Code
CGContextRestoreGState(ctx);

Android

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: 

For example:

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

Android (robot)

Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • HTML5 on Android 4.0: Way Better, Still Behind iOS 5
  • How to Make Android Development Less Frustrating
  • How it Feels to Switch from Eclipse to Android Studio
  • The 12 Biggest Android App Development Trends in 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: