2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkNinePatch.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkUnPreMultiply.h"
|
|
|
|
|
|
|
|
class NinePatchView : public SkView {
|
|
|
|
public:
|
|
|
|
SkBitmap fBM;
|
|
|
|
|
|
|
|
NinePatchView() {
|
2010-04-22 16:07:49 +00:00
|
|
|
SkImageDecoder::DecodeFile("/skimages/btn_default_normal_disable.9.png", &fBM);
|
2009-09-16 17:00:17 +00:00
|
|
|
|
|
|
|
// trim off the edge guide-lines
|
|
|
|
SkBitmap tmp;
|
|
|
|
SkIRect r;
|
|
|
|
r.set(1, 1, fBM.width() - 1, fBM.height() - 1);
|
|
|
|
fBM.extractSubset(&tmp, r);
|
|
|
|
fBM.swap(tmp);
|
2009-09-23 15:06:10 +00:00
|
|
|
|
|
|
|
fX = SkIntToScalar(fBM.width());
|
|
|
|
fY = 0;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "NinePatch");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawBG(SkCanvas* canvas) {
|
2010-04-22 16:07:49 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setDither(true);
|
|
|
|
p.setColor(0xFF909090);
|
|
|
|
canvas->drawPaint(p);
|
2009-09-23 15:06:10 +00:00
|
|
|
}
|
2010-04-22 16:07:49 +00:00
|
|
|
|
|
|
|
static void test_rects(SkCanvas* canvas, const SkBitmap& bm, const SkPaint* paint) {
|
|
|
|
static const SkIRect src[] = {
|
|
|
|
{ 0, 0, 18, 34 },
|
|
|
|
{ 18, 0, 19, 34 },
|
|
|
|
{ 19, 0, 36, 34 },
|
|
|
|
{ 0, 34, 18, 35 },
|
|
|
|
{ 18, 34, 19, 35 },
|
|
|
|
{ 19, 34, 36, 35 },
|
|
|
|
{ 0, 35, 18, 72 },
|
|
|
|
{ 18, 35, 19, 72 },
|
|
|
|
{ 19, 35, 36, 72 },
|
|
|
|
};
|
|
|
|
static const SkRect dst[] = {
|
|
|
|
{ 0, 0, 18, 34 },
|
|
|
|
{ 18, 0, 283, 34 },
|
|
|
|
{ 283, 0, 300, 34 },
|
|
|
|
{ 0, 34, 18, 163 },
|
|
|
|
{ 18, 34, 283, 163 },
|
|
|
|
{ 283, 34, 300, 163 },
|
|
|
|
{ 0, 163, 18, 200 },
|
|
|
|
{ 18, 163, 283, 200 },
|
|
|
|
{ 283, 163, 300, 200 },
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) {
|
|
|
|
canvas->drawBitmapRect(bm, &src[i], dst[i], paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-23 15:06:10 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
this->drawBG(canvas);
|
|
|
|
|
2010-04-22 16:07:49 +00:00
|
|
|
// canvas->scale(1.5f, 1.5f);
|
2009-09-16 17:00:17 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawBitmap(fBM, 0, 0);
|
|
|
|
|
|
|
|
SkIRect margins;
|
|
|
|
SkRect dst;
|
|
|
|
int d = 25;
|
|
|
|
|
|
|
|
margins.set(d, d, d, d);
|
2010-04-22 16:07:49 +00:00
|
|
|
margins.fLeft = fBM.width()/2 - 1;
|
|
|
|
margins.fTop = fBM.height()/2 - 1;
|
|
|
|
margins.fRight = fBM.width() - margins.fLeft - 1;
|
|
|
|
margins.fBottom = fBM.height() - margins.fTop - 1;
|
|
|
|
|
|
|
|
// canvas->translate(fX/5, fY/5);
|
|
|
|
canvas->translate(0, 76);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
dst.set(0, 0, SkIntToScalar(200), SkIntToScalar(200));
|
|
|
|
|
2010-04-22 16:07:49 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(false);
|
|
|
|
paint.setDither(true);
|
|
|
|
paint.setFilterBitmap(false);
|
|
|
|
// SkNinePatch::DrawNine(canvas, dst, fBM, margins, &paint);
|
|
|
|
test_rects(canvas, fBM, &paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 15:06:10 +00:00
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
|
|
|
|
fX = x / 1.5f;
|
|
|
|
fY = y / 1.5f;
|
2010-04-22 16:07:49 +00:00
|
|
|
fX = x; fY = y;
|
2009-09-23 15:06:10 +00:00
|
|
|
this->inval(NULL);
|
|
|
|
return this->INHERITED::onFindClickHandler(x, y);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
private:
|
2009-09-23 15:06:10 +00:00
|
|
|
SkScalar fX, fY;
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new NinePatchView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|
|
|
|
|