2012-09-13 125 views
1

我正在爲android 2.3編碼。
我寫了這個程序;它從(0,0)到(99,99)繪製一條白色,100像素長的對角線。android ndk - ANativeWindow_lock不一致?

存在一個錯誤:代碼只在第一次執行時正確地繪製屏幕。
每次之後,它畫兩條較短的對角線;一條綠黃線和一條藍線。
每一行出現大約正確長度的一半。
的黃綠色的線出現在(0,0)開始,但藍某似乎開始在各地(200,0)

我注意到,每次代碼執行時,ANativeWindow_Buffer.format可能會有所不同,不知道這是相關的。

有沒有人有任何想法是怎麼回事?

感謝您的幫助

#include <android/log.h> 
#include <android_native_app_glue.h> 

ANativeWindow_Buffer buffer; 

void Out(int x, int y) { 

    uint16_t * pPixel = buffer.bits; 

    /* Compute correct x,y */ 
    pPixel += (x + (y * buffer.stride)); 

    /* put a white pixel there */ 
    *pPixel = -1; 

} 


void drawscreen(struct android_app * app) { 

    unsigned u; 

    for (u = 0; u < 100; u++) { 

     Out(u, u); 

    } 

} 


static void android_handle_cmd(struct android_app * app, int32_t cmd) { 

    switch (cmd) { 

     case APP_CMD_INIT_WINDOW: 

      // The window is being shown. 
      if (NULL == app->window) { 

       return; 

      } 

      if (0 > ANativeWindow_lock(app->window, &buffer, NULL)) { 

       return; 

      } 

      drawscreen(app); 

      ANativeWindow_unlockAndPost(app->window); 
      break; 

    } 

} 


void android_main(struct android_app * app) { 

    app_dummy(); 

    app->onAppCmd  = android_handle_cmd; 

    for (;;) { 

     int events; 

     int iRet; 

     struct android_poll_source * source; 

     iRet = ALooper_pollAll(-1, NULL, &events, (void**)&source); 

     if (NULL != source) { 

      source->process(app, source); 
      continue; 

     } 

    } 

} 

回答

0

也許嘗試:調用drawscreen

int32_t w = ANativeWindow_getWidth(app->window); 
    int32_t h = ANativeWindow_getHeight(app->window); 
    ANativeWindow_setBuffersGeometry(app->window, w, h, WINDOW_FORMAT_RGB_565);