2011-09-06 19:23:41 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
2014-01-22 14:43:47 +00:00
|
|
|
static void make_bitmap(SkBitmap* bitmap, SkIRect* center) {
|
2011-09-06 19:23:41 +00:00
|
|
|
const int kFixed = 28;
|
|
|
|
const int kStretchy = 8;
|
|
|
|
const int kSize = 2*kFixed + kStretchy;
|
2012-08-02 14:03:32 +00:00
|
|
|
|
2014-01-25 16:46:20 +00:00
|
|
|
bitmap->allocN32Pixels(kSize, kSize);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(*bitmap);
|
2012-12-06 21:47:40 +00:00
|
|
|
canvas.clear(SK_ColorTRANSPARENT);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
|
|
|
|
const SkScalar strokeWidth = SkIntToScalar(6);
|
|
|
|
const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
paint.setColor(0xFFFF0000);
|
|
|
|
canvas.drawRoundRect(r, radius, radius, paint);
|
|
|
|
r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
|
|
|
|
paint.setColor(0x8800FF00);
|
|
|
|
canvas.drawRect(r, paint);
|
|
|
|
r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
|
|
|
|
paint.setColor(0x880000FF);
|
|
|
|
canvas.drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace skiagm {
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
class NinePatchStretchGM : public GM {
|
|
|
|
public:
|
|
|
|
SkBitmap fBM;
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
NinePatchStretchGM() {}
|
2011-09-06 19:23:41 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("ninepatch-stretch");
|
|
|
|
}
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
virtual SkISize onISize() {
|
2011-09-06 19:23:41 +00:00
|
|
|
return make_isize(400, 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
SkBitmap bm;
|
|
|
|
SkIRect center;
|
2014-01-22 14:43:47 +00:00
|
|
|
make_bitmap(&bm, ¢er);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
// amount of bm that should not be stretched (unless we have to)
|
|
|
|
const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
const SkTSize<SkScalar> size[] = {
|
|
|
|
{ fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
|
|
|
|
{ fixed * 4 / 5, fixed * 4 }, // shrink in X
|
|
|
|
{ fixed * 4, fixed * 4 / 5 }, // shrink in Y
|
|
|
|
{ fixed * 4, fixed * 4 }
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
SkScalar x = SkIntToScalar(100);
|
|
|
|
SkScalar y = SkIntToScalar(100);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
SkPaint paint;
|
2013-10-31 17:28:30 +00:00
|
|
|
paint.setFilterLevel(SkPaint::kLow_FilterLevel);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
for (int iy = 0; iy < 2; ++iy) {
|
|
|
|
for (int ix = 0; ix < 2; ++ix) {
|
|
|
|
int i = ix * 2 + iy;
|
|
|
|
SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
|
|
|
|
size[i].width(), size[i].height());
|
2011-09-07 12:27:43 +00:00
|
|
|
canvas->drawBitmapNine(bm, center, r, &paint);
|
2011-09-06 19:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-09-06 19:23:41 +00:00
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new NinePatchStretchGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|