【问题标题】:Pebble Date Layer卵石日期层
【发布时间】:2015-03-22 08:08:18
【问题描述】:

我正在尝试添加一个天气文本层并且编译成功但由于某种原因它不会安装在模拟器上。这段代码是您在完成网站上的前两个教程后得到的,任何涉及日期(图层、字体)的内容都是由我添加的。我已经将它作为子层添加到 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();
}

【问题讨论】:

    标签: pebble-sdk


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 2011-06-23
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      相关资源
      最近更新 更多