android ndk - ANativeWindow_lock不一致?

时间:2012-09-13 18:33:11

标签: android c android-ndk

我正在为android 2.3编码 我写了这个程序;它从(0,0)到(99,99)绘制一条100像素长的白色对角线。

有一个错误:代码只在第一次执行时正确绘制屏幕 每次之后,它会绘制两条较短的对角线;绿黄线和蓝线 每条线看起来大约是正确长度的一半 绿黄色线似乎从(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;

        }

    }

}

1 个答案:

答案 0 :(得分:0)

也许试试:

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

调用drawscreen之前