Home | History | Annotate | Download | only in samplecode
      1 #include "SampleCode.h"
      2 #include "SkView.h"
      3 #include "SkCanvas.h"
      4 #include "SkDevice.h"
      5 #include "SkPaint.h"
      6 #include "SkShader.h"
      7 
      8 static SkBitmap createBitmap(int n) {
      9     SkBitmap bitmap;
     10     bitmap.setConfig(SkBitmap::kARGB_8888_Config, n, n);
     11     bitmap.allocPixels();
     12     bitmap.eraseColor(SK_ColorGREEN);
     13 
     14     SkCanvas canvas(bitmap);
     15     SkRect r;
     16     r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
     17     SkPaint paint;
     18     paint.setAntiAlias(true);
     19 
     20     paint.setColor(SK_ColorRED);
     21     canvas.drawOval(r, paint);
     22     paint.setColor(SK_ColorBLUE);
     23     paint.setStrokeWidth(SkIntToScalar(n)/15);
     24     paint.setStyle(SkPaint::kStroke_Style);
     25     canvas.drawLine(0, 0, r.fRight, r.fBottom, paint);
     26     canvas.drawLine(0, r.fBottom, r.fRight, 0, paint);
     27 
     28     return bitmap;
     29 }
     30 
     31 class AARectView : public SampleView {
     32     SkBitmap fBitmap;
     33     enum {
     34         N = 64
     35     };
     36 public:
     37     AARectView() {
     38         fBitmap = createBitmap(N);
     39 
     40         fWidth = N;
     41     }
     42 
     43 protected:
     44     // overrides from SkEventSink
     45     virtual bool onQuery(SkEvent* evt) {
     46         if (SampleCode::TitleQ(*evt)) {
     47             SampleCode::TitleR(evt, "AA Rects");
     48             return true;
     49         }
     50         return this->INHERITED::onQuery(evt);
     51     }
     52 
     53     virtual void onDrawContent(SkCanvas* canvas) {
     54         canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
     55 
     56         SkPaint bluePaint;
     57         bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
     58         SkPaint bmpPaint;
     59         SkShader* bmpShader = SkShader::CreateBitmapShader(fBitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
     60         bmpPaint.setShader(bmpShader);
     61         bmpShader->unref();
     62 
     63         bluePaint.setStrokeWidth(3);
     64         bmpPaint.setStrokeWidth(3);
     65 
     66         SkPaint paints[] = { bluePaint, bmpPaint };
     67 
     68         SkRect rect;
     69 
     70         SkScalar dx = SkIntToScalar(80);
     71         SkScalar dy = SkIntToScalar(100);
     72         SkMatrix matrix;
     73         for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
     74             for (int stroke = 0; stroke < 2; ++stroke) {
     75                 paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
     76                 for (int a = 0; a < 3; ++ a) {
     77                     paints[p].setAntiAlias(a > 0);
     78                     paints[p].setAlpha(a > 1 ? 0x80 : 0xff);
     79 
     80                     canvas->save();
     81                         rect = SkRect::MakeLTRB(SkFloatToScalar(0.f),
     82                                                 SkFloatToScalar(0.f),
     83                                                 SkFloatToScalar(40.f),
     84                                                 SkFloatToScalar(40.f));
     85                         canvas->drawRect(rect, paints[p]);
     86                         canvas->translate(dx, 0);
     87 
     88                         rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
     89                                                 SkFloatToScalar(0.5f),
     90                                                 SkFloatToScalar(40.5f),
     91                                                 SkFloatToScalar(40.5f));
     92                         canvas->drawRect(rect, paints[p]);
     93                         canvas->translate(dx, 0);
     94 
     95                         rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
     96                                                 SkFloatToScalar(0.5f),
     97                                                 SkFloatToScalar(40.f),
     98                                                 SkFloatToScalar(40.f));
     99                         canvas->drawRect(rect, paints[p]);
    100                         canvas->translate(dx, 0);
    101 
    102                         rect = SkRect::MakeLTRB(SkFloatToScalar(0.75f),
    103                                                 SkFloatToScalar(0.75f),
    104                                                 SkFloatToScalar(40.75f),
    105                                                 SkFloatToScalar(40.75f));
    106                         canvas->drawRect(rect, paints[p]);
    107                         canvas->translate(dx, 0);
    108 
    109                         canvas->save();
    110                             canvas->translate(SkFloatToScalar(.33f), SkFloatToScalar(.67f));
    111                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.0f),
    112                                                     SkFloatToScalar(0.0f),
    113                                                     SkFloatToScalar(40.0f),
    114                                                     SkFloatToScalar(40.0f));
    115                             canvas->drawRect(rect, paints[p]);
    116                         canvas->restore();
    117                         canvas->translate(dx, 0);
    118 
    119                         canvas->save();
    120                             matrix.setRotate(SkFloatToScalar(45.f));
    121                             canvas->concat(matrix);
    122                             canvas->translate(SkFloatToScalar(20.0f / sqrtf(2.f)),
    123                                                 SkFloatToScalar(20.0f / sqrtf(2.f)));
    124                             rect = SkRect::MakeLTRB(SkFloatToScalar(-20.0f),
    125                                                     SkFloatToScalar(-20.0f),
    126                                                     SkFloatToScalar(20.0f),
    127                                                     SkFloatToScalar(20.0f));
    128                             canvas->drawRect(rect, paints[p]);
    129                         canvas->restore();
    130                         canvas->translate(dx, 0);
    131 
    132                         canvas->save();
    133                             canvas->rotate(SkFloatToScalar(90.f));
    134                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.0f),
    135                                                     SkFloatToScalar(0.0f),
    136                                                     SkFloatToScalar(40.0f),
    137                                                     SkFloatToScalar(-40.0f));
    138                             canvas->drawRect(rect, paints[p]);
    139                         canvas->restore();
    140                         canvas->translate(dx, 0);
    141 
    142                         canvas->save();
    143                             canvas->rotate(SkFloatToScalar(90.f));
    144                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
    145                                                     SkFloatToScalar(0.5f),
    146                                                     SkFloatToScalar(40.5f),
    147                                                     SkFloatToScalar(-40.5f));
    148                             canvas->drawRect(rect, paints[p]);
    149                         canvas->restore();
    150                         canvas->translate(dx, 0);
    151 
    152                         canvas->save();
    153                             matrix.setScale(SkFloatToScalar(-1.f), SkFloatToScalar(-1.f));
    154                             canvas->concat(matrix);
    155                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
    156                                                     SkFloatToScalar(0.5f),
    157                                                     SkFloatToScalar(-40.5f),
    158                                                     SkFloatToScalar(-40.5f));
    159                             canvas->drawRect(rect, paints[p]);
    160                         canvas->restore();
    161                         canvas->translate(dx, 0);
    162 
    163                         canvas->save();
    164                             matrix.setScale(SkFloatToScalar(2.1f), SkFloatToScalar(4.1f));
    165                             canvas->concat(matrix);
    166                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.1f),
    167                                                     SkFloatToScalar(0.1f),
    168                                                     SkFloatToScalar(19.1f),
    169                                                     SkFloatToScalar(9.1f));
    170                             canvas->drawRect(rect, paints[p]);
    171                         canvas->restore();
    172                         canvas->translate(dx, 0);
    173 
    174                     canvas->restore();
    175                     canvas->translate(0, dy);
    176                 }
    177             }
    178         }
    179     }
    180 
    181 private:
    182     int fWidth;
    183 
    184     typedef SampleView INHERITED;
    185 };
    186 
    187 //////////////////////////////////////////////////////////////////////////////
    188 
    189 static SkView* MyFactory() { return new AARectView; }
    190 static SkViewRegister reg(MyFactory);
    191 
    192