• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "SampleCode.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkGpuDevice.h"
12 
make_bitmap(SkBitmap * bitmap,GrContext * ctx,SkIRect * center)13 static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) {
14     SkDevice* dev;
15     SkCanvas canvas;
16 
17     const int kFixed = 28;
18     const int kStretchy = 8;
19     const int kSize = 2*kFixed + kStretchy;
20 
21     if (ctx) {
22         dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize);
23         *bitmap = dev->accessBitmap(false);
24     } else {
25         bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize);
26         bitmap->allocPixels();
27         dev = new SkDevice(*bitmap);
28     }
29 
30     canvas.setDevice(dev)->unref();
31     canvas.clear(0);
32 
33     SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
34     const SkScalar strokeWidth = SkIntToScalar(6);
35     const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
36 
37     center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
38 
39     SkPaint paint;
40     paint.setAntiAlias(true);
41 
42     paint.setColor(0xFFFF0000);
43     canvas.drawRoundRect(r, radius, radius, paint);
44     r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
45     paint.setColor(0x8800FF00);
46     canvas.drawRect(r, paint);
47     r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
48     paint.setColor(0x880000FF);
49     canvas.drawRect(r, paint);
50 }
51 
52 
53 class NinePatchView : public SampleView {
54 public:
NinePatchView()55 	NinePatchView() {}
56 
57 protected:
58     // overrides from SkEventSink
onQuery(SkEvent * evt)59     virtual bool onQuery(SkEvent* evt) {
60         if (SampleCode::TitleQ(*evt)) {
61             SampleCode::TitleR(evt, "NinePatch");
62             return true;
63         }
64         return this->INHERITED::onQuery(evt);
65     }
66 
onDrawContent(SkCanvas * canvas)67     virtual void onDrawContent(SkCanvas* canvas) {
68         SkBitmap bm;
69         SkIRect center;
70         make_bitmap(&bm, SampleCode::GetGr(), &center);
71 
72         // amount of bm that should not be stretched (unless we have to)
73         const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
74 
75         const SkTSize<SkScalar> size[] = {
76             { fixed * 4 / 5, fixed * 4 / 5 },   // shrink in both axes
77             { fixed * 4 / 5, fixed * 4 },       // shrink in X
78             { fixed * 4,     fixed * 4 / 5 },   // shrink in Y
79             { fixed * 4,     fixed * 4 }
80         };
81 
82         canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
83 
84         SkScalar x = SkIntToScalar(100);
85         SkScalar y = SkIntToScalar(100);
86 
87         SkPaint paint;
88         paint.setFilterBitmap(true);
89 
90         for (int iy = 0; iy < 2; ++iy) {
91             for (int ix = 0; ix < 2; ++ix) {
92                 int i = ix * 2 + iy;
93                 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
94                                             size[i].width(), size[i].height());
95                 canvas->drawBitmapNine(bm, center, r, &paint);
96             }
97         }
98     }
99 
100 private:
101     SkScalar fX, fY;
102     typedef SampleView INHERITED;
103 };
104 
105 //////////////////////////////////////////////////////////////////////////////
106 
MyFactory()107 static SkView* MyFactory() { return new NinePatchView; }
108 static SkViewRegister reg(MyFactory);
109 
110