首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pebble Date图层

Pebble Date图层
EN

Stack Overflow用户
提问于 2015-03-22 16:08:19
回答 1查看 321关注 0票数 1

这段代码是当你在网站上完成前两个教程时得到的,任何涉及日期(图层,字体)的东西都是我添加的。我已经将其作为子层添加到窗口中,但是什么也没有出现。

代码语言:javascript
复制
#include <pebble.h>

static Window *s_main_window;
static TextLayer *s_time_layer;
static TextLayer *s_date_layer;
static GFont s_date_font;
static GFont s_time_font;
static BitmapLayer *s_background_layer;
static GBitmap *s_background_bitmap;

static void update_time() {
  // Get a tm structure
  time_t temp = time(NULL); 
//  time_t temp1 = date(NULL);
  struct tm *tick_time = localtime(&temp);
 // struct dt *tick_date = localdate(&temp1);

  // Create a long-lived buffer
  static char buffer[] = "00:00:00";
  static char buffer1[] = "00:00:00";

  // Write the current hours and minutes into the buffer
  if(clock_is_24h_style() == true) {
    // Use 24 hour format
    strftime(buffer, sizeof("00:00:00"), "%H:%M:%S", tick_time);
  } else {
    // Use 12 hour format
    strftime(buffer, sizeof("00:00:00"), "%I:%M:%S", tick_time);
  }

  // Display this time on the TextLayer
  text_layer_set_text(s_time_layer, buffer);

  strftime(buffer1, sizeof("mm/dd/yy"), "%m/%d/%y", tick_time);

  // Display this date on the TextLayer
  text_layer_set_text(s_date_layer, buffer1);
}

static void main_window_load(Window *window) {

  // Create GBitmap, then set to created BitmapLayer
  s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_BACKGROUND);
  s_background_layer = bitmap_layer_create(GRect(0, 0, 144, 168));
  bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
  layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_background_layer));

  // Create time TextLayer
  //s_time_layer = text_layer_create(GRect(0, 55, 144, 50));
  s_time_layer = text_layer_create(GRect(4, 62, 139, 50));
  text_layer_set_background_color(s_time_layer, GColorClear);
  text_layer_set_text_color(s_time_layer, GColorBlack);

  // Create date DateLayer
  //s_time_layer = text_layer_create(GRect(0, 55, 144, 50));
  s_date_layer = text_layer_create(GRect(8, 184, 139, 50));
  text_layer_set_background_color(s_date_layer, GColorClear);
  text_layer_set_text_color(s_date_layer, GColorWhite);

  // Create GFont for Time and Date
  s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_30));
  s_date_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_30));

  // Apply to TextLayer
  text_layer_set_font(s_time_layer, s_time_font);

  //Apply to DateLayer
  text_layer_set_font(s_date_layer, s_date_font);

  //Test text on watchface
  //text_layer_set_text(s_time_layer, "00:00");

  // Improve the layout to be more like a watchface
  //text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_30_BLACK));
  text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
  text_layer_set_text_alignment(s_date_layer, GAlignBottom);

  // Add time as a child layer to the Window's root layer
  layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer));

  //Add date as a child layer to the Window's root layer
  layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_date_layer));
}

static void main_window_unload(Window *window) {

  // Unload GFonts
  fonts_unload_custom_font(s_time_font);
  fonts_unload_custom_font(s_date_font);

  // Destroy TextLayer
  text_layer_destroy(s_time_layer);

  //Destroy DateLayer
  text_layer_destroy(s_date_layer);

  // Destroy GBitmap
  gbitmap_destroy(s_background_bitmap);

  // Destroy BitmapLayer
  bitmap_layer_destroy(s_background_layer);

}

static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  update_time();
}

static void init() {

  // Create main Window element and assign to pointer
  s_main_window = window_create();

  // Set handlers to manage the elements inside the Window
  window_set_window_handlers(s_main_window, (WindowHandlers) {
    .load = main_window_load,
    .unload = main_window_unload
  });

  // Show the Window on the watch, with animated=true
  window_stack_push(s_main_window, true);

  // Make sure the time and date is displayed from the start
  update_time();

  // Register with TickTimerService
  tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
  tick_timer_service_subscribe(SECOND_UNIT, tick_handler);
  tick_timer_service_subscribe(DAY_UNIT, tick_handler);
  tick_timer_service_subscribe(MONTH_UNIT, tick_handler);
  tick_timer_service_subscribe(YEAR_UNIT, tick_handler);

}

static void deinit() {

    // Destroy Window
    window_destroy(s_main_window);

}

int main(void) {
  init();
  app_event_loop();
  deinit();
}
EN

回答 1

Stack Overflow用户

发布于 2015-03-27 12:18:46

我在CloudPebble中测试了它,没有任何问题(我不得不删除你没有包含的bg图像和字体)。你犯了以下错误:

s_date_layer = text_layer_create(GRect(8, 184, 139, 50));

Y值184从屏幕底部(168)下方开始

text_layer_set_text_color(s_date_layer, GColorWhite);

它可能会在白色上绘制白色(没有看到您的背景)

tick_timer_service_subscribe(YEAR_UNIT, tick_handler);

只有最后一次订阅才算数,所以你要么只做如下操作:(适合你的用法)

tick_timer_service_subscribe(SECOND_UNIT, tick_handler);

或者在一次调用中添加/或所需的值,例如:

tick_timer_service_subscribe(SECOND_UNIT | DAY_UNIT, tick_handler);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29192257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档