如对Android原生(Natvie)C开发还任何疑问,请参阅《Android原生(Native)C开发之一:环境搭建篇》:http://blog.sina.com.cn/s/blog_4a0a39c30100auh9.html
虽然现在能通过交叉环境编译程序,并push到Android上执行,但那只是console台程序,是不是有些单调呢?下面就要看如何通过Linux的 framebuffer 技术在Android上画图形,关于Linux的framebuffer技术,这里就不再详细讲解了,请大家google一下。
操作framebuffer的主要步骤如下:
1、打开一个可用的FrameBuffer设备;
2、通过mmap调用把显卡的物理内存空间映射到用户空间;
3、更改内存空间里的像素数据并显示;
4、退出时关闭framebuffer设备。
下面的这个例子简单地用framebuffer画了一个渐变的进度条,代码 framebuf.c 如下:
#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;
}
注意,在Android环境,framebuffer设备不是象linux一样的 /dev/fb0,而是 /dev/graphics/fb0 ,
fbfd = open("/dev/graphics/fb0", O_RDWR);
打开framebuffer设备,
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
fbfd, 0);
将设备map到一块内存,然后就可以操作这块内存空间来显示你想画的图形了。
最后别忘了关闭设备:
munmap(fbp, screensize);
close(fbfd);
效果图如下:
分享到:
相关推荐
android下读取framebuffer的内容 程序运行就会把framebuffer的内容保存在/sdcard/123.raw下面, 要想打开还需要在文件上插入如bitmap的文件头即可
QUALCOMM 平台 android framebuffer 命令行测试工具 源代码
framebuffer驱动全篇
一个完整的android framebuffer截图程序。支持16位和24位,支持所有屏幕大小。比上一个版本多了对屏幕指定部位进行截图的功能。
在手机root下,通过Framebuffer获取手机系统界面上的截图,可以显示出来
一个完整的android framebuffer截图程序。支持16位和24位,支持所有屏幕大小。
总部设在南加州、专注于Android平台并提供其移动应用开发及解决方案的迈奔无线(mAPPn Inc.)也投入技术和人力参与了本书的工作。 参与本书编写的还有于仕林、张宇、张超等人,赵家维、黄亮、沈桢、徐威特、杨钰等...
Android4.0下使用framebuffer抓屏并保存成bmp文件
一个完整的android framebuffer截图程序。支持16位、24位和32位,支持所有屏幕大小。16位和32位的全部功能经测试,可以完美使用。24位暂时没有测试过。
FrameBuffer电子烟 根据许可。 位于。 这是为了什么? 这是为了填补Kobo开发人员和修补匠发现自己没有在设备屏幕上打印内容的内置方式时所感到的空白! 在eips了Kindle上无处不在的eips之后,当它搬到Kobo时特别...
linux下基于framebuffer运行,运行前先将framebuffer设置为16位色.
S3C2440上LCD驱动(FrameBuffer)实例开发讲解
framebuffer驱动全篇.docframebuffer驱动全篇.doc
第一篇Android的前世今生——Android概述篇 第1章Android的前世今生 1.1 Android的起源 1.2开放手机联盟 1.3开源与相关协议 1.4系统的升级与发展 第2章Android体系结构 2.1四层空间基本结构 2.1.1 Android系统底层...
framebuffer测试程序,帮你了解framebuffer流程
在Linux内核中,Framebuffer(帖缓冲)驱动是显示驱动的标准,Framebuffer将显示设备抽象为帖缓冲区,用户通过内存映射到进程地址空间之后,就可以直接进行读写操作,且写操作可以立即在屏幕上进行显示,在Linux内核/...
某网友整理的 framebuffer驱动全篇.doc 我转成了pdf,方便更多人的阅读。
Linux Framebuffer应用编程——jpeg显示在RGB888 Lcd显示屏 包含源代码.c文件,运行在Arm9开发板的可执行文件,一张jpg图像(分辨率800*533) ...或者可以去我的博客找《Linux Framebuffer编程之lcd屏幕显示jpeg图像》
在学习WebGL过程中,有一种技术叫二次绘制,就是把每一帧画面再次处理渲染,它能做好多高级效果的实现,如边缘检测,高斯模糊,SSAO等。二次绘制中最重要的就是FBO和RBO,接下来,我们来看看什么是FBO和RBO。 FBO...
framebuffer.c