android natvice直接在surface上绘图
android 的application 都是有窗口系统的,会把各种事件给阻拦掉.但是,用c++写的程序是直接在linux上运行的,并没有窗口这一概念,所以可以向鼠标一样,只绘制图像而不响应任何窗口时间.具体实现是通过SurfaceComposerClient,这个类相当于surfaceflinger的代理,可以创建一个显示图像的surface.注意,不同系统的接口可能不一样.若有问题,...
·
android 7上直接 surface用 skia 画图
#include<SkImageDecoder.h>
#include <utils/RefBase.h>
#include <utils/Looper.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/Surface.h>
#include <SkBitmap.h>
#include <SkCanvas.h>
#include <SkColor.h>
#include <SkPaint.h>
#include <SkXfermode.h>
#include <android/native_window.h>
#include <unistd.h>
#include <cutils/log.h>
#include <ui/DisplayInfo.h>
using namespace android;
int main()
{
SkBitmap bitmap;
SkImageDecoder::DecodeFile("/sdcard/test.png", &bitmap);
sp<SurfaceComposerClient> pSurfaceComposerClient = new SurfaceComposerClient();
int32_t width = bitmap.height();
int32_t height = bitmap.width();
ALOGI("width= %d \n", width);
ALOGI("height= %d \n", height);
sp<SurfaceControl> surfaceControl = pSurfaceComposerClient->createSurface(
String8("test"), width, height, PIXEL_FORMAT_RGBA_8888,
ISurfaceComposerClient::eHidden);
if (surfaceControl == NULL || !surfaceControl->isValid()) {
ALOGE("Error creating sprite surface.");
return -1;
}
sp<Surface> surface = surfaceControl->getSurface();
ANativeWindow_Buffer outBuffer;
status_t status = surface->lock(&outBuffer, NULL);
if (status) {
ALOGE("Error %d locking sprite surface before drawing.", status);
} else {
SkBitmap surfaceBitmap;
ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
//新版本已经放弃此函数
// surfaceBitmap.setConfig(SkBitmap::kARGB_8888_Config,
// outBuffer.width, outBuffer.height, bpr);
// surfaceBitmap.setPixels(outBuffer.bits);
SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, kPremul_SkAlphaType);
surfaceBitmap.installPixels(info, outBuffer.bits,bpr);
SkCanvas surfaceCanvas(surfaceBitmap);
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
surfaceCanvas.drawBitmap(bitmap, 0, 0, &paint);
if (outBuffer.width > uint32_t(bitmap.width())) {
paint.setColor(0); // transparent fill color
surfaceCanvas.drawRectCoords(bitmap.width(), 0,
outBuffer.width, bitmap.height(), paint);
}
if (outBuffer.height > uint32_t(bitmap.height())) {
paint.setColor(0); // transparent fill color
surfaceCanvas.drawRectCoords(0, bitmap.height(),
outBuffer.width, outBuffer.height, paint);
}
status = surface->unlockAndPost();
if (status) {
ALOGE("Error %d unlocking and posting sprite surface after drawing.", status);
}
}
int x = 0, y = 0;
SurfaceComposerClient::openGlobalTransaction();
//surfaceControl->setAlpha(1.0f);
surfaceControl->setLayer(100000);
//surfaceControl->setMatrix(1.0, 1.0, 1.0, 1.0);
SurfaceComposerClient::closeGlobalTransaction();
while(true){
ALOGI("openGlobalTransaction \n");
SurfaceComposerClient::openGlobalTransaction();
surfaceControl->setPosition(x, y);
x = (x+10) %1920;
y = (y+10) %1280;
static bool isShow = false;
if(!isShow){
surfaceControl->show();
isShow = true;
}
ALOGI("closeGlobalTransaction \n");
SurfaceComposerClient::closeGlobalTransaction();
sleep(1);
}
return 0;
}
android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
SurfaceTest.cpp
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
libui \
libgui \
libbinder \
libskia
LOCAL_MODULE:= SurfaceTest
LOCAL_MODULE_TAGS := tests
include $(BUILD_EXECUTABLE)
最简单的 surface 创建
SurfaceTest.cpp
#include <cutils/memory.h>
#include <utils/Log.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <android/native_window.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
using namespace android;
int main()
{
// set up the thread-pool
sp<ProcessState> proc(ProcessState::self());
ProcessState::self()->startThreadPool();
// create a client to surfaceflinger
sp<SurfaceComposerClient> client = new SurfaceComposerClient();
sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),
160, 240, PIXEL_FORMAT_RGB_565, 0);
sp<Surface> surface = surfaceControl->getSurface();
SurfaceComposerClient::openGlobalTransaction();
surfaceControl->setLayer(100000);
SurfaceComposerClient::closeGlobalTransaction();
ANativeWindow_Buffer outBuffer;
surface->lock(&outBuffer, NULL);
ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
android_memset16((uint16_t*)outBuffer.bits, 0xF800, bpr*outBuffer.height);
surface->unlockAndPost();
surface->lock(&outBuffer,NULL);
android_memset16((uint16_t*)outBuffer.bits, 0x07E0, bpr*outBuffer.height);
surface->unlockAndPost();
SurfaceComposerClient::openGlobalTransaction();
surfaceControl->setSize(320, 240);
SurfaceComposerClient::closeGlobalTransaction();
IPCThreadState::self()->joinThreadPool();
return 0;
}
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
SurfaceTest.cpp
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
libui \
libgui \
libbinder
LOCAL_MODULE:= SurfaceTest
LOCAL_MODULE_TAGS := tests
include $(BUILD_EXECUTABLE)
在此基础上修改编译通过复制自
更多推荐
所有评论(0)