1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* Copyright 2013 Google Inc.
*
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
*/
#include "SkExample.h"
#include "SkApplication.h"
#include "SkDraw.h"
#include "SkGradientShader.h"
#include "SkGraphics.h"
class HelloSkia : public SkExample {
public:
HelloSkia(SkExampleWindow* window) : SkExample(window) {
fName = "HelloSkia";
fBGColor = SK_ColorWHITE;
fRotationAngle = SkIntToScalar(0);
fWindow->setupBackend(SkExampleWindow::kGPU_DeviceType);
// Another option is software rendering:
// fWindow->setupBackend(SkExampleWindow::kRaster_DeviceType);
}
protected:
void draw(SkCanvas* canvas) {
// Clear background
canvas->drawColor(fBGColor);
SkPaint paint;
paint.setColor(SK_ColorRED);
// Draw a rectangle with blue paint
SkRect rect = {
SkIntToScalar(10), SkIntToScalar(10),
SkIntToScalar(128), SkIntToScalar(128)
};
canvas->drawRect(rect, paint);
// Set up a linear gradient and draw a circle
{
SkPoint linearPoints[] = {
{SkIntToScalar(0), SkIntToScalar(0)},
{SkIntToScalar(300), SkIntToScalar(300)}
};
SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK};
SkShader* shader = SkGradientShader::CreateLinear(
linearPoints, linearColors, NULL, 2,
SkShader::kMirror_TileMode);
SkAutoUnref shader_deleter(shader);
paint.setShader(shader);
paint.setFlags(SkPaint::kAntiAlias_Flag);
canvas->drawCircle(SkIntToScalar(200), SkIntToScalar(200),
SkIntToScalar(64), paint);
// Detach shader
paint.setShader(NULL);
}
// Draw a message with a nice black paint.
paint.setFlags(
SkPaint::kAntiAlias_Flag |
SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotating.
SkPaint::kUnderlineText_Flag);
paint.setColor(SK_ColorBLACK);
paint.setTextSize(SkIntToScalar(20));
canvas->save();
static const char message[] = "Hello Skia!!!";
// Translate and rotate
canvas->translate(SkIntToScalar(300), SkIntToScalar(300));
fRotationAngle += SkDoubleToScalar(0.2);
if (fRotationAngle > SkDoubleToScalar(360.0)) {
fRotationAngle -= SkDoubleToScalar(360.0);
}
canvas->rotate(fRotationAngle);
// Draw the text:
canvas->drawText(message, strlen(message), SkIntToScalar(0), SkIntToScalar(0), paint);
canvas->restore();
// Invalidate the window to force a redraw. Poor man's animation mechanism.
this->fWindow->inval(NULL);
}
private:
SkScalar fRotationAngle;
SkColor fBGColor;
};
static SkExample* MyFactory(SkExampleWindow* window) {
return new HelloSkia(window);
}
// Register this class as a Skia Example.
SkExample::Registry registry(MyFactory);
|