2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2011-04-25 12:43:45 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
2018-01-23 16:24:08 +00:00
|
|
|
#include "SkBitmap.h"
|
2011-04-25 12:43:45 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
|
|
|
|
static SkBitmap createBitmap(int n) {
|
|
|
|
SkBitmap bitmap;
|
2014-02-17 02:55:57 +00:00
|
|
|
bitmap.allocN32Pixels(n, n);
|
2011-04-25 12:43:45 +00:00
|
|
|
bitmap.eraseColor(SK_ColorGREEN);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-25 12:43:45 +00:00
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-25 12:43:45 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas.drawOval(r, paint);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(n)/15);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas.drawLine(0, 0, r.fRight, r.fBottom, paint);
|
|
|
|
canvas.drawLine(0, r.fBottom, r.fRight, 0, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-25 12:43:45 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:12:36 +00:00
|
|
|
class AARectView : public SampleView {
|
2011-04-25 12:43:45 +00:00
|
|
|
SkBitmap fBitmap;
|
|
|
|
enum {
|
|
|
|
N = 64
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
AARectView() {
|
2017-12-19 14:18:59 +00:00
|
|
|
fBitmap = createBitmap(N);
|
2017-12-19 20:20:13 +00:00
|
|
|
|
2011-04-25 12:43:45 +00:00
|
|
|
fWidth = N;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2017-12-19 20:20:13 +00:00
|
|
|
protected:
|
2011-04-25 12:43:45 +00:00
|
|
|
// overrides from SkEventSink
|
2017-12-19 20:20:13 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
2011-04-25 12:43:45 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "AA Rects");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:20:13 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
|
|
|
|
SkPaint bluePaint;
|
|
|
|
bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
|
|
|
|
SkPaint bmpPaint;
|
2016-03-09 02:50:00 +00:00
|
|
|
bmpPaint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode));
|
2011-04-25 12:43:45 +00:00
|
|
|
bluePaint.setStrokeWidth(3);
|
|
|
|
bmpPaint.setStrokeWidth(3);
|
|
|
|
|
|
|
|
SkPaint paints[] = { bluePaint, bmpPaint };
|
|
|
|
|
|
|
|
SkRect rect;
|
|
|
|
|
|
|
|
SkScalar dx = SkIntToScalar(80);
|
|
|
|
SkScalar dy = SkIntToScalar(100);
|
|
|
|
SkMatrix matrix;
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
|
2011-04-25 12:43:45 +00:00
|
|
|
for (int stroke = 0; stroke < 2; ++stroke) {
|
|
|
|
paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
|
|
|
|
for (int a = 0; a < 3; ++ a) {
|
|
|
|
paints[p].setAntiAlias(a > 0);
|
|
|
|
paints[p].setAlpha(a > 1 ? 0x80 : 0xff);
|
|
|
|
|
|
|
|
canvas->save();
|
2013-11-25 19:44:07 +00:00
|
|
|
rect = SkRect::MakeLTRB(0.f,
|
|
|
|
0.f,
|
|
|
|
40.f,
|
|
|
|
40.f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
rect = SkRect::MakeLTRB(0.5f,
|
|
|
|
0.5f,
|
|
|
|
40.5f,
|
|
|
|
40.5f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
rect = SkRect::MakeLTRB(0.5f,
|
|
|
|
0.5f,
|
|
|
|
40.f,
|
|
|
|
40.f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
2013-11-25 19:44:07 +00:00
|
|
|
rect = SkRect::MakeLTRB(0.75f,
|
|
|
|
0.75f,
|
|
|
|
40.75f,
|
|
|
|
40.75f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
|
|
|
canvas->save();
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas->translate(.33f, .67f);
|
|
|
|
rect = SkRect::MakeLTRB(0.0f,
|
|
|
|
0.0f,
|
|
|
|
40.0f,
|
|
|
|
40.0f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
|
|
|
canvas->save();
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setRotate(45.f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->concat(matrix);
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas->translate(20.0f / sqrtf(2.f),
|
|
|
|
20.0f / sqrtf(2.f));
|
|
|
|
rect = SkRect::MakeLTRB(-20.0f,
|
|
|
|
-20.0f,
|
|
|
|
20.0f,
|
|
|
|
20.0f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
|
|
|
canvas->save();
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas->rotate(90.f);
|
|
|
|
rect = SkRect::MakeLTRB(0.0f,
|
|
|
|
0.0f,
|
|
|
|
40.0f,
|
|
|
|
-40.0f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
|
|
|
canvas->save();
|
2013-11-25 19:44:07 +00:00
|
|
|
canvas->rotate(90.f);
|
|
|
|
rect = SkRect::MakeLTRB(0.5f,
|
|
|
|
0.5f,
|
|
|
|
40.5f,
|
|
|
|
-40.5f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
|
|
|
canvas->save();
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setScale(-1.f, -1.f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->concat(matrix);
|
2013-11-25 19:44:07 +00:00
|
|
|
rect = SkRect::MakeLTRB(0.5f,
|
|
|
|
0.5f,
|
|
|
|
-40.5f,
|
|
|
|
-40.5f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
|
|
|
canvas->save();
|
2013-11-25 19:44:07 +00:00
|
|
|
matrix.setScale(2.1f, 4.1f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->concat(matrix);
|
2013-11-25 19:44:07 +00:00
|
|
|
rect = SkRect::MakeLTRB(0.1f,
|
|
|
|
0.1f,
|
|
|
|
19.1f,
|
|
|
|
9.1f);
|
2011-04-25 12:43:45 +00:00
|
|
|
canvas->drawRect(rect, paints[p]);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(dx, 0);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, dy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-25 12:43:45 +00:00
|
|
|
private:
|
|
|
|
int fWidth;
|
|
|
|
|
2011-05-05 14:12:36 +00:00
|
|
|
typedef SampleView INHERITED;
|
2011-04-25 12:43:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new AARectView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|