2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2015-02-02 20:55:02 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
static constexpr int INT_SIZE = 64;
|
|
|
|
static constexpr float SCALAR_SIZE = (float)INT_SIZE;
|
2011-07-06 13:59:47 +00:00
|
|
|
|
2012-09-25 15:37:50 +00:00
|
|
|
static void make_bitmap(SkBitmap* bitmap) {
|
2014-02-17 02:55:57 +00:00
|
|
|
bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
|
2012-09-25 15:37:50 +00:00
|
|
|
SkCanvas canvas(*bitmap);
|
2011-07-06 13:59:47 +00:00
|
|
|
|
2009-10-15 18:51:46 +00:00
|
|
|
canvas.drawColor(SK_ColorRED);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-09-25 15:37:50 +00:00
|
|
|
const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
|
2009-10-15 18:51:46 +00:00
|
|
|
const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
|
2019-04-03 14:27:45 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
|
2012-09-25 15:37:50 +00:00
|
|
|
canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
|
|
|
|
*value += *delta;
|
|
|
|
if (*value < min) {
|
|
|
|
*value = min;
|
|
|
|
*delta = - *delta;
|
|
|
|
} else if (*value > max) {
|
|
|
|
*value = max;
|
|
|
|
*delta = - *delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
|
|
|
|
bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
|
|
|
|
bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
|
2009-10-15 18:51:46 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class BitmapRectView : public Sample {
|
2019-07-11 20:32:53 +00:00
|
|
|
SkPoint fSrcPt = {0, 0};
|
|
|
|
SkPoint fSrcVec = {0.866025f, 0.5f};
|
2012-09-25 15:37:50 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
SkRect fSrcLimit = {-SCALAR_SIZE/4, -SCALAR_SIZE/4,
|
|
|
|
SCALAR_SIZE*5/4, SCALAR_SIZE*5/4};
|
|
|
|
SkRect fDstR[2] = {{10, 100, 260, 400}, {322.5, 100, 572.5, 400}};
|
|
|
|
SkBitmap fBitmap;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
SkString name() override { return SkString("BitmapRect"); }
|
2015-02-02 20:55:02 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2015-02-02 20:55:02 +00:00
|
|
|
this->setBGColor(SK_ColorGRAY);
|
2019-07-11 20:32:53 +00:00
|
|
|
make_bitmap(&fBitmap);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2019-07-11 20:32:53 +00:00
|
|
|
SkRect srcR = {fSrcPt.fX - 16, fSrcPt.fY - 16,
|
|
|
|
fSrcPt.fX + 16, fSrcPt.fY + 16};
|
2009-10-15 18:51:46 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
SkPaint paint(SkColors::kYellow);
|
2009-10-15 18:51:46 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-09-25 15:37:50 +00:00
|
|
|
|
|
|
|
canvas->translate(20, 20);
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, &paint);
|
2012-09-25 15:37:50 +00:00
|
|
|
canvas->drawRect(srcR, paint);
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
|
2019-07-11 20:32:53 +00:00
|
|
|
canvas->drawBitmapRect(fBitmap, srcR, fDstR[i], &paint,
|
2015-07-17 14:09:43 +00:00
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2012-09-25 15:37:50 +00:00
|
|
|
canvas->drawRect(fDstR[i], paint);
|
|
|
|
}
|
2015-02-02 20:55:02 +00:00
|
|
|
}
|
2012-09-25 15:37:50 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
bounce_pt(&fSrcPt, &fSrcVec, fSrcLimit);
|
2015-02-02 20:55:02 +00:00
|
|
|
return true;
|
2012-09-25 15:37:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
static constexpr int BIG_H = 120;
|
|
|
|
|
2012-09-25 15:37:50 +00:00
|
|
|
static void make_big_bitmap(SkBitmap* bm) {
|
|
|
|
static const char gText[] =
|
|
|
|
"We the people, in order to form a more perfect union, establish justice,"
|
|
|
|
" ensure domestic tranquility, provide for the common defense, promote the"
|
|
|
|
" general welfare and ensure the blessings of liberty to ourselves and our"
|
|
|
|
" posterity, do ordain and establish this constitution for the United"
|
|
|
|
" States of America.";
|
|
|
|
|
2019-01-02 17:21:01 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSize(SkIntToScalar(BIG_H));
|
2012-09-25 15:37:50 +00:00
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), SkTextEncoding::kUTF8));
|
2012-09-25 15:37:50 +00:00
|
|
|
|
2014-02-17 02:55:57 +00:00
|
|
|
bm->allocN32Pixels(BIG_W, BIG_H);
|
2012-09-25 15:37:50 +00:00
|
|
|
bm->eraseColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas.drawSimpleText(gText, strlen(gText), SkTextEncoding::kUTF8, 0, font.getSize()*4/5, font, SkPaint());
|
2012-09-25 15:37:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class BitmapRectView2 : public Sample {
|
2012-09-25 15:37:50 +00:00
|
|
|
SkBitmap fBitmap;
|
2019-07-11 20:32:53 +00:00
|
|
|
SkRect fSrcR = {0, 0, 3 * BIG_H, BIG_H};
|
|
|
|
SkRect fLimitR;
|
|
|
|
SkScalar fDX = 1;
|
|
|
|
SkRect fDstR[2] = {{20, 20, 620, 220}, {20, 270, 620, 470}};
|
2012-09-25 15:37:50 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("BigBitmapRect"); }
|
2018-06-07 17:11:37 +00:00
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2012-09-25 15:37:50 +00:00
|
|
|
this->setBGColor(SK_ColorGRAY);
|
2019-07-11 20:32:53 +00:00
|
|
|
make_big_bitmap(&fBitmap);
|
|
|
|
fLimitR = SkRect::Make(fBitmap.dimensions());
|
2012-09-25 15:37:50 +00:00
|
|
|
}
|
2012-09-26 02:01:18 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2012-09-25 15:37:50 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-09-25 15:37:50 +00:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
|
2015-08-06 17:02:53 +00:00
|
|
|
canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint,
|
2015-07-17 14:09:43 +00:00
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2012-09-25 15:37:50 +00:00
|
|
|
canvas->drawRect(fDstR[i], paint);
|
|
|
|
}
|
2015-02-02 20:55:02 +00:00
|
|
|
}
|
2012-09-26 02:01:18 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
SkScalar width = fSrcR.width();
|
|
|
|
bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
|
|
|
|
fSrcR.fRight = fSrcR.fLeft + width;
|
2015-02-02 20:55:02 +00:00
|
|
|
return true;
|
2012-09-25 15:37:50 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new BitmapRectView(); )
|
|
|
|
DEF_SAMPLE( return new BitmapRectView2(); )
|