If you have any questions about Android native (natvie) C development, see http://emck.avaw.com /? P = 205
Although the program can be compiled through the cross environment and pushed to Android for execution, it is only a console program. Is it monotonous? Next, let's take a look at how to use the framebuffer Technology in Linux to draw images on Android. Here we will not detail the framebuffer Technology in Linux. please google it.
The main steps for operating framebuffer are as follows:
1. Open an available framebuffer device;
2. Map the physical memory space of the video card to the user space through MMAP calls;
3. Change and display the pixel data in the memory space;
4. Disable the framebuffer device when exiting.
The following example draws a gradient progress bar using framebuffer. The Code framebuf. C is as follows:
# Include <unistd. h>
# Include <stdio. h>
# Include <fcntl. h>
# Include <Linux/FB. h>
# Include <sys/Mman. h>
Inline static unsigned short int make16color (unsigned char R, unsigned char g, unsigned char B)
{
Return (
(R> 3) & 31) <11) |
(G> 2) & 63) <5) |
(B> 3) & 31 ));
}
Int main (){
Int fbfd = 0;
Struct fb_var_screeninfo vinfo;
Struct fb_fix_screeninfo finfo;
Long int screensize = 0;
Char * FBP = 0;
Int x = 0, y = 0;
Int guage_height = 20, step = 10;
Long int location = 0;
// Open the file for reading and writing
Fbfd = open ("/dev/graphics/fb0", o_rdwr );
If (! Fbfd ){
Printf ("error: cannot open framebuffer device./N ");
Exit (1 );
}
Printf ("the framebuffer device was opened successfully./N ");
// Get fixed screen information
If (IOCTL (fbfd, fbioget_fscreeninfo, & finfo )){
Printf ("error reading fixed information./N ");
Exit (2 );
}
// Get variable screen information
If (IOCTL (fbfd, fbioget_vscreeninfo, & vinfo )){
Printf ("error reading variable information./N ");
Exit (3 );
}
Printf ("sizeof (unsigned short) = % d/N", sizeof (unsigned short ));
Printf ("% DX % d, % dbpp/N", vinfo. xres, vinfo. yres, vinfo. bits_per_pixel );
Printf ("xoffset: % d, yoffset: % d, line_length: % d/N", vinfo. xoffset, vinfo. yoffset, finfo. line_length );
// Figure out the size of the screen in bytes
Screensize = vinfo. xres * vinfo. yres * vinfo. bits_per_pixel/8 ;;
// Map the device to memory
FBP = (char *) MMAP (0, screensize, prot_read | prot_write, map_shared,
Fbfd, 0 );
If (INT) FBP =-1 ){
Printf ("error: failed to map framebuffer device to memory./N ");
Exit (4 );
}
Printf ("the framebuffer device was mapped to memory successfully./N ");
// Set to black color first
Memset (FBP, 0, screensize );
// Draw rectangle
Y = (vinfo. yres-guage_height)/2-2; // where we are going to put the pixel
For (x = Step-2; x <vinfo. xres-step + 2; X ++ ){
Location = (x + vinfo. xoffset) * (vinfo. bits_per_pixel/8) +
(Y + vinfo. yoffset) * finfo. line_length;
* (Unsigned short int *) (FBP + location) = 255;
}
Y = (vinfo. yres + guage_height)/2 + 2; // where we are going to put the pixel
For (x = Step-2; x <vinfo. xres-step + 2; X ++ ){
Location = (x + vinfo. xoffset) * (vinfo. bits_per_pixel/8) +
(Y + vinfo. yoffset) * finfo. line_length;
* (Unsigned short int *) (FBP + location) = 255;
}
X = Step-2;
For (y = (vinfo. yres-guage_height)/2-2; y <(vinfo. yres + guage_height)/2 + 2; y ++ ){
Location = (x + vinfo. xoffset) * (vinfo. bits_per_pixel/8) +
(Y + vinfo. yoffset) * finfo. line_length;
* (Unsigned short int *) (FBP + location) = 255;
}
X = vinfo. xres-step + 2;
For (y = (vinfo. yres-guage_height)/2-2; y <(vinfo. yres + guage_height)/2 + 2; y ++ ){
Location = (x + vinfo. xoffset) * (vinfo. bits_per_pixel/8) +
(Y + vinfo. yoffset) * finfo. line_length;
* (Unsigned short int *) (FBP + location) = 255;
}
// Figure out where in memory to put the pixel
For (x = step; x <vinfo. xres-step; X ++ ){
For (y = (vinfo. yres-guage_height)/2; y <(vinfo. yres + guage_height)/2; y ++ ){
Location = (x + vinfo. xoffset) * (vinfo. bits_per_pixel/8) +
(Y + vinfo. yoffset) * finfo. line_length;
If (vinfo. bits_per_pixel = 32 ){
* (FBP + location) = 100; // some blue
* (FBP + location + 1) = 15 + (X-100)/2; // a little green
* (FBP + location + 2) = 200-(y-100)/5; // a lot of red
* (FBP + location + 3) = 0; // no transparency
} Else {// assume 16bpp
Unsigned char B = 255 * x/(vinfo. xres-step );
Unsigned char G = 255; // (X-100)/6 a little green
Unsigned char r = 255; // a lot of red
Unsigned short int T = make16color (R, G, B );
* (Unsigned short int *) (FBP + location) = T;
}
}
// Printf ("x = % d, temp = % d/N", X, temp );
// Sleep to see it
Usleep (200 );
}
// Clean framebuffer
Munmap (FBP, screensize );
Close (fbfd );
Return 0;
}
Note: In the android environment, the framebuffer device is not like/dev/fb0 in Linux,/Dev/graphics/fb0,
Fbfd = open ("/dev/graphics/fb0", o_rdwr );
Enable the framebuffer device,
FBP = (char *) MMAP (0, screensize, prot_read | prot_write, map_shared,
Fbfd, 0 );
Map the device to a memory, and then you can operate on the memory space to display the image you want to draw.
Don't forget to close the device:
Munmap (FBP, screensize );
Close (fbfd );
As follows:
Android framebuffer