卵石日期层

时间:2015-03-22 08:08:19

标签: pebble-sdk

我尝试添加天气文本图层并且编译成功但由于某种原因它无法在模拟器上安装。这段代码就是你在网站上完成前两个教程时得到的代码,并且我添加了引用日期(图层,字体)的任何内容。我已经将它作为子图层添加到Window中,但没有任何结果。

#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();
}

1 个答案:

答案 0 :(得分:0)

我在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);