pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • Examples
  • Community
  • Blog
  • More
Privacy
Cookies
Publish
Get Updates
Follow Us
Subscribe to the Newsletter

Tips and Tricks - Transparent Images

  • 13 May 2015
  • Chris Lewis
  • Beautiful Code
  • Comments

Ever wondered how to draw transparent images in a Pebble app? This post will walk you through the process.

In this post, we'll be using a sample image with a transparency component, shown below:

When adding your project resource, ensure you set its ‘type’ correctly. On CloudPebble, this is done when uploading the resource and choosing the 'PNG' type. In the SDK, this is done with the png type in the project's appinfo.json.

"media": [
  {
    "type": "png",
    "name": "GLOBE",
    "file": "globe.png"
  }
]

This will create a resource ID to use in code:

RESOURCE_ID_GLOBE

Simply create a GBitmap with this resource and draw the image with GCompOpSet as the compositing mode:

static GBitmap *s_bitmap;
static Layer *s_canvas_layer;
static void window_load(Window *window) {
  Layer *window_layer = window_get_root_layer(window);

  // Create GBitmap
  s_bitmap = gbitmap_create_with_resource(RESOURCE_ID_GLOBE);

  // Create canvas Layer
  s_canvas_layer = layer_create(layer_get_bounds(window_layer));
  layer_set_update_proc(s_canvas_layer, layer_update_proc);
  layer_add_child(window_layer, s_canvas_layer);
}
static void layer_update_proc(Layer *layer, GContext *ctx) {
  // Draw the image with the correct compositing mode
  graphics_context_set_compositing_mode(ctx, GCompOpSet);
  graphics_draw_bitmap_in_rect(ctx, s_bitmap, gbitmap_get_bounds(s_bitmap));
}

When drawing on a TextLayer underneath s_canvas_layer, the result looks like this:

result-aplite

See a full demo of this technique on the pebble-examples GitHub repo.

Job done! Any transparent pixels in the original image will be drawn as clear, leaving the color beneath unaffected.

Read the Image Resources guide to learn more about transparent PNGs.

Conclusion

So there you have it. Using these examples you can easily implement transparency on all Pebble platforms. To learn more, read the GCompOp documentation or the pebble-examples/feature-image-transparent SDK example.


You need JavaScript enabled to read and post comments.
Get Updates
Follow Us
Subscribe to the Newsletter

Overview

  • Conclusion

Categories

  • All Posts
  • #makeawesomehappen
  • At the Pub
  • Beautiful Code
  • CloudPebble
  • Down the Rabbit Hole
  • Freshly Baked
  • Timeline

Authors

  • Thomas Sarlandie
  • Niharika Bedekar
  • Jon Barlow
  • Katharine Berry
  • Cherie Williams
  • Chris Lewis
  • Team Pebble
  • Cat Haines
  • Katherine McAuliffe
  • Alex Lin
  • Alexey Komissarouk
  • Brad Murray
  • Kirby Kohlmorgen
  • Ryan Case
  • Ryan Perry-Nguyen
  • Tom Maremaa
  • Meiguro
  • Keegan Lillo
  • Łukasz Zalewski

Subscribe to the Pebble Developers Newsletter