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