SampleRepeatTile.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkShader.h"
  10. #include "samplecode/Sample.h"
  11. static void make_bitmap(SkBitmap* bm) {
  12. const int W = 100;
  13. const int H = 100;
  14. bm->allocN32Pixels(W, H);
  15. SkPaint paint;
  16. SkCanvas canvas(*bm);
  17. canvas.drawColor(SK_ColorWHITE);
  18. const SkColor colors[] = {
  19. SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
  20. };
  21. for (int ix = 0; ix < W; ix += 1) {
  22. SkScalar x = SkIntToScalar(ix) + SK_ScalarHalf;
  23. paint.setColor(colors[ix & 3]);
  24. canvas.drawLine(x, 0, x, SkIntToScalar(H - 1), paint);
  25. }
  26. paint.setColor(SK_ColorGRAY);
  27. canvas.drawLine(0, 0, SkIntToScalar(W), 0, paint);
  28. }
  29. static void make_paint(SkPaint* paint, SkTileMode tm) {
  30. SkBitmap bm;
  31. make_bitmap(&bm);
  32. paint->setShader(bm.makeShader(tm, tm));
  33. }
  34. class RepeatTileView : public Sample {
  35. public:
  36. RepeatTileView() {
  37. this->setBGColor(SK_ColorGRAY);
  38. }
  39. protected:
  40. SkString name() override { return SkString("RepeatTile"); }
  41. void onDrawContent(SkCanvas* canvas) override {
  42. SkPaint paint;
  43. make_paint(&paint, SkTileMode::kRepeat);
  44. // canvas->scale(SK_Scalar1*2, SK_Scalar1);
  45. canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
  46. canvas->drawPaint(paint);
  47. }
  48. private:
  49. typedef Sample INHERITED;
  50. };
  51. //////////////////////////////////////////////////////////////////////////////
  52. DEF_SAMPLE( return new RepeatTileView(); )