用大个的数字填充表盘会是怎么样的?
本来设想这样
但是就是麦步不支持超过20个layer害我以为哪有BUG,查到最后,发现创建到21个layer时返回-1.
最后成了这个样子
/*
* =====================================================================================
* WatchFaceName: xyz-reborn
* Description:
* Author: wisepragma
* Created: August 12th,2017
* HomePage:http://blog.csdn.net/wisepragma
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "maibu_sdk.h"
#include "maibu_res.h"
int32_t app_window_stack_push(Window *window);
int8_t maibu_get_battery_percent(int8_t *percent);
Window * app_window_stack_get_window_by_id(int32_t id);
void app_window_update(Window *window);
static const uint8_t N_MIN_HIDE_SEC=3;
static const char ORGIN_X=0,ORGIN_Y=0;//大数字起始位置
static const char ORGIN_SM_X=7,ORGIN_SM_Y=7;//小数字起始位置
static const char ORGIN_XSM_X=10,ORGIN_XSM_Y=10;//超小数字起始位置
static GSize WATCH_NUM_SIZE={0,0};//大数字的高与宽,从图片资源上获取
static GSize WATCH_SM_NUM_SIZE={0,0};//小数字的高与宽,从图片资源上获取
static GSize WATCH_XSM_NUM_SIZE={0,0};//超小数字的高与宽,从图片资源上获取
static uint32_t half_sec_cnt = 0;
//static struct date_time last_time;
static struct TIME_NUM
{
uint8_t value;
int8_t lyid;
GRect rc;
}T_num[4+8+16];
static P_Window p_win=NULL;
static int32_t win_id =-1;
static int32_t timer_id =-1;
static const uint16_t tnum_bmp[10] = {BMP_0, BMP_1, BMP_2, BMP_3, BMP_4, BMP_5, BMP_6, BMP_7, BMP_8, BMP_9};
static const uint16_t tnum_sm_bmp[11] = {SM_BMP_0, SM_BMP_1, SM_BMP_2, SM_BMP_3, SM_BMP_4, SM_BMP_5, SM_BMP_6, SM_BMP_7, SM_BMP_8, SM_BMP_9, SM_BMP_10 };
static const uint16_t tnum_xsm_bmp[11] = {XSM_BMP_0, XSM_BMP_1, XSM_BMP_2, XSM_BMP_3, XSM_BMP_4, XSM_BMP_5, XSM_BMP_6, XSM_BMP_7, XSM_BMP_8, XSM_BMP_9, XSM_BMP_10 };
static void fillGRect(GRect *grc,uint8_t x,uint8_t y,uint8_t h,uint8_t w)
{
grc->origin.x=x;
grc->origin.y=y;
grc->size.w=w;
grc->size.h=h;
}
static int8_t create_bmp_layer(Window *pwin, uint32_t bmpID,int16_t x,int16_t y,uint8_t h,uint8_t w/*,enum GColor ecolor*/)
{
int8_t lyid=-1;
GRect frame_bg;
GBitmap bitmap_bg;//height,width
res_get_user_bitmap(bmpID, &bitmap_bg);
fillGRect( &frame_bg,x,y,h,w );
LayerBitmap lb_bg = {bitmap_bg, frame_bg, GAlignLeft};
P_Layer layer_bitmap_bg = app_layer_create_bitmap(&lb_bg);
//app_layer_set_bg_color(layer_bitmap_bg, ecolor);
if(layer_bitmap_bg != NULL)
{
lyid=app_window_add_layer(pwin, layer_bitmap_bg);
}
return lyid;
}
//=========hour min======================
static void update_hour_min_layer(struct date_time *datetime)
{
GBitmap bitmap;
if(T_num[0].lyid == -1 )
{
//big
fillGRect( &T_num[0].rc, ORGIN_X, ORGIN_Y, WATCH_NUM_SIZE.h, WATCH_NUM_SIZE.w );
fillGRect( &T_num[1].rc, WATCH_NUM_SIZE.w, ORGIN_Y, WATCH_NUM_SIZE.h, WATCH_NUM_SIZE.w );
fillGRect( &T_num[2].rc, 2*WATCH_NUM_SIZE.w, ORGIN_Y, WATCH_NUM_SIZE.h, WATCH_NUM_SIZE.w );
fillGRect( &T_num[3].rc, 3*WATCH_NUM_SIZE.w, ORGIN_Y, WATCH_NUM_SIZE.h, WATCH_NUM_SIZE.w );
//small
GSize dt={60,32};
fillGRect( &T_num[4].rc, ORGIN_SM_X , ORGIN_SM_Y, WATCH_SM_NUM_SIZE.h, WATCH_SM_NUM_SIZE.w );
fillGRect( &T_num[5].rc, ORGIN_SM_X+dt.w , ORGIN_SM_Y, WATCH_SM_NUM_SIZE.h, WATCH_SM_NUM_SIZE.w );
fillGRect( &T_num[6].rc, ORGIN_SM_X+dt.w*2 , ORGIN_SM_Y, WATCH_SM_NUM_SIZE.h, WATCH_SM_NUM_SIZE.w );
fillGRect( &T_num[7].rc, ORGIN_SM_X +dt.w*3 , ORGIN_SM_Y, WATCH_SM_NUM_SIZE.h, WATCH_SM_NUM_SIZE.w );
fillGRect( &T_num[8].rc, ORGIN_SM_X , ORGIN_SM_Y+dt.h, WATCH_SM_NUM_SIZE.h, WATCH_SM_NUM_SIZE.w );
fillGRect( &T_num[9].rc, ORGIN_SM_X+dt.w , ORGIN_SM_Y+dt.h, WATCH_SM_NUM_SIZE.h, WATCH_SM_NUM_SIZE.w );
fillGRect( &T_num[10].rc, ORGIN_SM_X +dt.w *2 , ORGIN_SM_Y+dt.h, WATCH_SM_NUM_SIZE.h, WATCH_SM_NUM_SIZE.w );
fillGRect( &T_num[11].rc, ORGIN_SM_X +dt.w*3 , ORGIN_SM_Y+dt.h, WATCH_SM_NUM_SIZE.h, WATCH_SM_NUM_SIZE.w );
//extremely small
dt.h=24;
dt.w=32;
fillGRect( &T_num[12].rc, ORGIN_XSM_X , ORGIN_XSM_Y, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[13].rc, ORGIN_XSM_X+ dt.w , ORGIN_XSM_Y, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[14].rc, ORGIN_XSM_X+2* dt.w , ORGIN_XSM_Y, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[15].rc, ORGIN_XSM_X+3* dt.w , ORGIN_XSM_Y, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[16].rc, ORGIN_XSM_X , ORGIN_XSM_Y+dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[17].rc, ORGIN_XSM_X+ dt.w , ORGIN_XSM_Y+dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[18].rc, ORGIN_XSM_X+2* dt.w , ORGIN_XSM_Y+dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[19].rc, ORGIN_XSM_X+3* dt.w , ORGIN_XSM_Y+dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[20].rc, ORGIN_XSM_X , ORGIN_XSM_Y+12+2*dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[21].rc, ORGIN_XSM_X + dt.w, ORGIN_XSM_Y+12+2*dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[22].rc, ORGIN_XSM_X+2* dt.w , ORGIN_XSM_Y+12+2*dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[23].rc, ORGIN_XSM_X+3* dt.w , ORGIN_XSM_Y+12+2*dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[24].rc, ORGIN_XSM_X , ORGIN_XSM_Y+12+3*dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[25].rc, ORGIN_XSM_X+ dt.w , ORGIN_XSM_Y+12+3*dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[26].rc, ORGIN_XSM_X+2* dt.w , ORGIN_XSM_Y+12+3*dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
fillGRect( &T_num[27].rc, ORGIN_XSM_X+3* dt.w, ORGIN_XSM_Y+12+3*dt.h, WATCH_XSM_NUM_SIZE.h, WATCH_XSM_NUM_SIZE.w );
//最多能创建20个layer,之后会返回-1
// pos:0,lyid:0
// pos:1,lyid:1
// pos:2,lyid:2
// pos:3,lyid:3
// pos:4,lyid:4
// pos:5,lyid:5
// pos:6,lyid:6
// pos:7,lyid:7
// pos:8,lyid:8
// pos:9,lyid:9
// pos:10,lyid:10
// pos:11,lyid:11
// pos:12,lyid:12
// pos:13,lyid:13
// pos:14,lyid:14
// pos:15,lyid:15
// pos:16,lyid:16
// pos:17,lyid:17
// pos:18,lyid:18
// pos:19,lyid:19
// pos:20,lyid:-1
// pos:21,lyid:-1
// pos:22,lyid:-1
// pos:23,lyid:-1
// pos:24,lyid:-1
// pos:25,lyid:-1
// pos:26,lyid:-1
// pos:27,lyid:-1
}
//秒
if(half_sec_cnt!=0)
{
T_num[18].value = datetime->sec/10;
T_num[19].value = datetime->sec%10;
}
//时分
T_num[0].value= datetime->hour/10;
T_num[1].value= datetime->hour%10;
T_num[2].value= datetime->min/10;
T_num[3].value= datetime->min%10;
//月日
T_num[24].value = datetime->mon/10;
T_num[25].value = datetime->mon%10;
T_num[26].value = datetime->mday/10;
T_num[27].value = datetime->mday%10;
//年
// uint16_t year= datetime->year ;//bugfixed:uint8_t year;
for (int pos=23; pos>=20; pos--)//年份:[11]个[10]十[9]百[8]千
{
T_num[pos].value = datetime->year%10;
datetime->year = datetime->year/10;
}
//星期几
T_num[4].value = (datetime->wday==0)?8:datetime->wday;
//电量
int8_t percent=100;
maibu_get_battery_percent(&percent);
for (int pos=15; pos>=13; pos--)
{
T_num[pos].value = percent%10;
percent=percent/10;
}
// for(int i=0;i<=27;i++)
// {
// T_num[i].value=8;
// }
for(int pos=0;pos<=3;pos++)
{
if(T_num[pos].lyid == -1)
{
T_num[pos].lyid =create_bmp_layer( p_win, tnum_bmp[ T_num[pos].value], T_num[pos].rc.origin.x, T_num[pos].rc.origin.y,T_num[pos].rc.size.h, T_num[pos].rc.size.w );
}
else
{
P_Layer p_layer = app_window_get_layer_by_id(p_win, T_num[pos].lyid);
if (p_layer != NULL)
{
res_get_user_bitmap(tnum_bmp[ T_num[pos].value ], &bitmap);
app_layer_set_bitmap_bitmap(p_layer, &bitmap);
}
}
// printf("pos:%d,lyid:%d\n",pos,T_num[pos].lyid );
}
for(int pos=4;pos<=11;pos++)
{
if(pos==5||pos==8 ||pos==6||pos==7 || pos==9 || pos==10 || pos==11 ) continue;
// printf("s2.pos:%d,lyid:%d\n",pos,T_num[pos].lyid);//模拟调试使用
if(T_num[pos].lyid == -1)
{
T_num[pos].lyid =create_bmp_layer( p_win, tnum_sm_bmp[ T_num[pos].value], T_num[pos].rc.origin.x, T_num[pos].rc.origin.y,T_num[pos].rc.size.h, T_num[pos].rc.size.w );
}
else
{
P_Layer p_layer = app_window_get_layer_by_id(p_win, T_num[pos].lyid);
if (p_layer != NULL)
{
res_get_user_bitmap(tnum_sm_bmp[ T_num[pos].value ], &bitmap);
app_layer_set_bitmap_bitmap(p_layer, &bitmap);
}
}
// printf("pos:%d,lyid:%d\n",pos,T_num[pos].lyid );
}
for(int pos=12;pos<=27;pos++)
{
if(pos==12 || pos==13 || pos==16 || pos==17) continue;
if((pos==18 || pos==19) && half_sec_cnt==0 )
{
T_num[pos].value=10;
}
// T_num[pos].value=8;
if(T_num[pos].lyid == -1)
{
T_num[pos].lyid =create_bmp_layer( p_win, tnum_xsm_bmp[ T_num[pos].value], T_num[pos].rc.origin.x, T_num[pos].rc.origin.y,T_num[pos].rc.size.h, T_num[pos].rc.size.w );
}
else
{
P_Layer p_layer = app_window_get_layer_by_id(p_win, T_num[pos].lyid);
if (p_layer != NULL)
{
res_get_user_bitmap(tnum_xsm_bmp[ T_num[pos].value ], &bitmap);
app_layer_set_bitmap_bitmap(p_layer, &bitmap);
}
}
// printf("pos:%d,lyid:%d\n",pos,T_num[pos].lyid );
}
}
//================================
static void sec_callback(date_time_t tick_time, uint32_t millis,void *context)
{
half_sec_cnt++; //500ms刷新一次,避免秒慢于分钟更新
if(half_sec_cnt>=N_MIN_HIDE_SEC*60*2)//几钟后停止显示秒
{
app_service_timer_unsubscribe(timer_id);
half_sec_cnt=0;
// printf("......app_service_timer_unsubscribe\n" );
}
P_Window p_win =(P_Window)app_window_stack_get_window_by_id(win_id);
if (p_win == NULL) return;
struct date_time datetime;
app_service_get_datetime(&datetime);
update_hour_min_layer( &datetime);
app_window_update(p_win);
}
static void on_back_key()
{
P_Window p_win = app_window_stack_get_window_by_id(win_id);
if(p_win != NULL)
{
if(half_sec_cnt==0)
{
timer_id=app_window_timer_subscribe(p_win, 500, sec_callback, p_win);
}
}
}
static void time_change_callback(enum SysEventType type, void *context)
{
if (type == SysEventTypeTimeChange)
{
P_Window p_win =(P_Window)app_window_stack_get_window_by_id(win_id);
if (p_win == NULL) return;
struct date_time datetime;
app_service_get_datetime(&datetime);
update_hour_min_layer( &datetime );
app_window_update(p_win);
}
}
//---------------main-----------------------------------------------------------------------
int main()
{
#ifdef USE_SIMULATOR
simulator_init();
#endif
//....................切换表盘时,资源已销毁,要重新创建layer,否则将不能正常显示.............................................................
win_id =-1;
half_sec_cnt =-1;
timer_id =-1;
memset(T_num,-1,sizeof(T_num));
//.........................................................................................................................
p_win = app_window_create();
if (NULL == p_win) return 0;
struct date_time datetime;
app_service_get_datetime(&datetime);
GBitmap bitmap;
res_get_user_bitmap(*tnum_bmp, &bitmap);//BMP_ID中取得宽高信息
WATCH_NUM_SIZE.h=bitmap.height;
WATCH_NUM_SIZE.w=bitmap.width;
res_get_user_bitmap(*tnum_sm_bmp, &bitmap);//BMP_ID中取得宽高信息
WATCH_SM_NUM_SIZE.h=bitmap.height;
WATCH_SM_NUM_SIZE.w=bitmap.width;
res_get_user_bitmap(*tnum_xsm_bmp, &bitmap);//BMP_ID中取得宽高信息
WATCH_XSM_NUM_SIZE.h=bitmap.height;
WATCH_XSM_NUM_SIZE.w=bitmap.width;
update_hour_min_layer( &datetime );
//显示秒定时器
timer_id=app_window_timer_subscribe(p_win, 500, sec_callback, p_win);
maibu_service_sys_event_subscribe(time_change_callback);
app_window_click_subscribe(p_win, ButtonIdBack, on_back_key);
//ButtonIdDown ButtonIdUp ButtonIdBack,ButtonIdSelect
app_window_update(p_win);
if (p_win != NULL) win_id= app_window_stack_push(p_win);
//.................................................................................
#ifdef USE_SIMULATOR
simulator_wait();
#endif
return 0;
}
mai文件下载:
http://pan.baidu.com/s/1kUCZo6z