SkCanvas 는 Skia를 위한 drawing context 이다. SkCanvas는 행렬 스택과 픽셀이 그려질 디스플레이 위치 등을 알고있다. 하지만, Cairo나 awt 등의 API contexts와는 다르게 color, pen size등의 다른 그리기를 위한 속성은 없다. 이러한 속성은 그리기를 호출 할 때마다 SkPaint에서정 해주어야 한다.
canvas.save();
canvas.rotate(SkIntToScalar(45));
canvas.drawRect(rect, paint);
canvas.restore();
위는 45도 돌아간 직사각형을 그리는 코드다. rect의 색과 스타일을 명확하게 묘사하는 것은 canvas가 아닌 paint다.
canvas 를 그리기 시작하는 것은 매우 직관적이다. 그것은 canvas를 그리기 원하는 곳이 어디인지 요청하는 것이다. 전형적으로, 픽셀은 bitmap에 의해 찍히게되고, 단지 canvas를 만들때 bitmap을 설정해주기 만 하면 된다.
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config,200,100);
bitmap.allocPixels();// have the bitmap allocate memory for the pixels offscreen
양자택일로, 비트맵에 명시적으로 메모리를 할당할수도 있다. 그렇게 하면 그리기 원하는 place나 screen에 할당 할 수 있다.
bitmap.setConfig(SkBitmap::kARGB_8888_Config,200,100);
bitmap.setPixels(address);
setConfig()의 4번째 파라미터는 optional로, 각각의 row pixels사이의 rowBytes를 설정하는 파라미터이다. 만약에 이것을 설정하지 않는다면, 기본 값 (usually rounded to be a multiple of 4) 이 width와 the pixel-size로 config 파라미터에 계산되 설정 된다.
이제 그려보자!
SkCanvas canvas(bitmap);
이것이 전부다. 만약에 원한다면, bitmap을 canvas를 다 만들고서 설정해도 된다. 그렇게 해도 똑같은 작업을 할 수 있다.
canvas.setBitmapDevice(bitmap);
To begin with, we might want to erase the entire canvas. We can do this by drawing an enormous rectangle, but there are easier ways to do it.
canvas.drawPaint(paint);
완전한 canvas (though respecting the current clip of course)를 color나 shader (and xfermode) 를 설정한 paint로 채운다. 만약에 paint의 shader를 사용하면, 현재 canvas의 matrix를 소중히 해야 한다. (see SkShader).
단지 한가지 색으로 채우고 싶다면 (with an optional xfermode), 그냥 drawColor()를 호출하고 pait를 canvas 자체에 설정 하라.
canvas.drawColor(SK_ColorWHITE);
다른 draw API들도 비슷하다. 각각의 paint parameter가 존재한다.
canvas.drawRect(rect, paint);
canvas.drawOval(oval, paint);
canvas.drawCircle(x, y, radius, paint);
canvas.drawRoundRect(rect, rx, ry, paint);
canvas.drawPath(path, paint);
canvas.drawBitmap(bitmap, x, y,&paint);
canvas.drawBitmapRect(bitmap,&srcRect, dstRect,&paint);
canvas.drawBitmapMatrix(bitmap, matrix,&paint);
canvas.drawText(text, length, x, y, paint);
canvas.drawPosText(text, length, pos[], paint);
canvas.drawTextOnPath(text, length, path, paint);
In some of the calls, we pass a pointer, rather than a reference, to the paint. In those instances, the paint parameter may be null. In all other cases the paint parameter is required.
'SKIA' 카테고리의 다른 글
Graphics and Skia (0) | 2011.08.03 |
---|---|
[Chromium] PlatformCanvas로부터 GraphicsContext 만들기 (1) | 2011.07.08 |