blob: bb1dd5a3f1bddd9525d059d7549200604a7d602c [file] [log] [blame]
Leon Scroggins III7a10b332018-01-12 11:24:30 -05001/*
2 * Copyright 2018 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
Leon Scroggins III42ee2842018-01-14 14:46:51 -05008#include "SkAndroidCodec.h"
Leon Scroggins III7a10b332018-01-12 11:24:30 -05009#include "SkAnimatedImage.h"
10#include "SkAnimTimer.h"
11#include "SkCanvas.h"
Leon Scroggins III7a10b332018-01-12 11:24:30 -050012#include "SkPaint.h"
13#include "SkPictureRecorder.h"
14#include "SkRect.h"
15#include "SkScalar.h"
16#include "SkString.h"
17
18#include "SampleCode.h"
19#include "Resources.h"
20
21static constexpr char kPauseKey = 'p';
22static constexpr char kResetKey = 'r';
23
24class SampleAnimatedImage : public SampleView {
25public:
26 SampleAnimatedImage()
27 : INHERITED()
28 , fRunning(false)
29 , fYOffset(0)
30 {}
31
32protected:
33 void onDrawBackground(SkCanvas* canvas) override {
34 SkPaint paint;
35 paint.setAntiAlias(true);
36 constexpr SkScalar kTextSize = 20;
37 paint.setTextSize(kTextSize);
38
39 SkString str = SkStringPrintf("Press '%c' to start/pause; '%c' to reset.",
40 kPauseKey, kResetKey);
41 const char* text = str.c_str();
42 SkRect bounds;
43 paint.measureText(text, strlen(text), &bounds);
44 fYOffset = bounds.height();
45
46 canvas->drawText(text, strlen(text), 5, fYOffset, paint);
47 fYOffset *= 2;
48 }
49
50 void onDrawContent(SkCanvas* canvas) override {
51 if (!fImage) {
52 return;
53 }
54
55 canvas->translate(0, fYOffset);
56
57 canvas->drawDrawable(fImage.get());
58 canvas->drawDrawable(fDrawable.get(), fImage->getBounds().width(), 0);
59 }
60
61 bool onAnimate(const SkAnimTimer& animTimer) override {
62 if (!fImage) {
63 return false;
64 }
65
66 fImage->update(animTimer.msec());
67 return true;
68 }
69
70 void onOnceBeforeDraw() override {
71 sk_sp<SkData> file(GetResourceAsData("images/alphabetAnim.gif"));
72 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(file));
73 if (!codec) {
74 return;
75 }
76
Leon Scroggins III42ee2842018-01-14 14:46:51 -050077 fImage = SkAnimatedImage::Make(SkAndroidCodec::MakeFromCodec(std::move(codec)));
Leon Scroggins III7a10b332018-01-12 11:24:30 -050078 if (!fImage) {
79 return;
80 }
81
82 SkPictureRecorder recorder;
83 auto canvas = recorder.beginRecording(fImage->getBounds());
84 canvas->drawDrawable(fImage.get());
85 fDrawable = recorder.finishRecordingAsDrawable();
86 }
87
88 bool onQuery(SkEvent* evt) override {
89 if (SampleCode::TitleQ(*evt)) {
90 SampleCode::TitleR(evt, "AnimatedImage");
91 return true;
92 }
93
94 SkUnichar uni;
95 if (fImage && SampleCode::CharQ(*evt, &uni)) {
96 switch (uni) {
97 case kPauseKey:
98 if (fRunning) {
99 fImage->stop();
100 fRunning = false;
101 } else {
102 fImage->start();
103 fRunning = true;
104 }
105 return true;
106 case kResetKey:
107 fImage->reset();
108 return true;
109 default:
110 break;
111 }
112 }
113 return this->INHERITED::onQuery(evt);
114 }
115
116private:
117 sk_sp<SkAnimatedImage> fImage;
118 sk_sp<SkDrawable> fDrawable;
119 bool fRunning;
120 SkScalar fYOffset;
121 typedef SampleView INHERITED;
122};
123
124///////////////////////////////////////////////////////////////////////////////
125
126static SkView* MyFactory() {
127 return new SampleAnimatedImage;
128}
129
130static SkViewRegister reg(MyFactory);