Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
AnimatedImageSlide Class Reference
Inheritance diagram for AnimatedImageSlide:
Slide SkRefCnt SkRefCntBase

Public Member Functions

 AnimatedImageSlide ()
 
void draw (SkCanvas *canvas) override
 
bool animate (double nanos) override
 
void load (SkScalar w, SkScalar h) override
 
bool onChar (SkUnichar uni) override
 
- Public Member Functions inherited from Slide
virtual SkISize getDimensions () const
 
virtual void gpuTeardown ()
 
virtual void draw (SkCanvas *canvas)=0
 
virtual bool animate (double nanos)
 
virtual void load (SkScalar winWidth, SkScalar winHeight)
 
virtual void resize (SkScalar winWidth, SkScalar winHeight)
 
virtual void unload ()
 
virtual bool onChar (SkUnichar c)
 
virtual bool onMouse (SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey modifiers)
 
virtual bool onGetControls (SkMetaData *)
 
virtual void onSetControls (const SkMetaData &)
 
const SkStringgetName ()
 
- Public Member Functions inherited from SkRefCntBase
 SkRefCntBase ()
 
virtual ~SkRefCntBase ()
 
bool unique () const
 
void ref () const
 
void unref () const
 

Additional Inherited Members

- Protected Attributes inherited from Slide
SkString fName
 

Detailed Description

Definition at line 25 of file AnimatedImageSlide.cpp.

Constructor & Destructor Documentation

◆ AnimatedImageSlide()

AnimatedImageSlide::AnimatedImageSlide ( )
inline

Definition at line 35 of file AnimatedImageSlide.cpp.

35{ fName = "AnimatedImage"; }
SkString fName
Definition: Slide.h:54

Member Function Documentation

◆ animate()

bool AnimatedImageSlide::animate ( double  nanos)
inlineoverridevirtual

Reimplemented from Slide.

Definition at line 61 of file AnimatedImageSlide.cpp.

61 {
62 if (!fImage) {
63 return false;
64 }
65
66 const double lastWallTime = fLastWallTime;
67 fLastWallTime = TimeUtils::NanosToMSec(nanos);
68
69 if (fRunning) {
70 fCurrentTime += fLastWallTime - lastWallTime;
71 if (fCurrentTime > fTimeToShowNextFrame) {
72 fTimeToShowNextFrame += fImage->decodeNextFrame();
73 if (fImage->isFinished()) {
74 fRunning = false;
75 }
76 }
77 }
78
79 return true;
80 }
bool isFinished() const
static SkMSec NanosToMSec(double nanos)
Definition: TimeUtils.h:16

◆ draw()

void AnimatedImageSlide::draw ( SkCanvas canvas)
inlineoverridevirtual

Implements Slide.

Definition at line 37 of file AnimatedImageSlide.cpp.

37 {
39 font.setSize(20);
40
41 SkString str = SkStringPrintf("Press '%c' to start/pause; '%c' to reset.",
43 const char* text = str.c_str();
45 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
46 fYOffset = bounds.height();
47
48 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 5, fYOffset, font, SkPaint());
49 fYOffset *= 2;
50
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 }
static constexpr char kPauseKey
static constexpr char kResetKey
@ kUTF8
uses bytes to represent UTF-8 or ASCII
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawDrawable(SkDrawable *drawable, const SkMatrix *matrix=nullptr)
Definition: SkCanvas.cpp:2574
SkRect getBounds()
Definition: SkDrawable.cpp:71
Definition: SkFont.h:35
const char * c_str() const
Definition: SkString.h:133
T * get() const
Definition: SkRefCnt.h:303
std::u16string text
Optional< SkRect > bounds
Definition: SkRecords.h:189
SkFont DefaultFont()
font
Font Metadata and Metrics.
constexpr float width() const
Definition: SkRect.h:762

◆ load()

void AnimatedImageSlide::load ( SkScalar  w,
SkScalar  h 
)
inlineoverridevirtual

Reimplemented from Slide.

Definition at line 82 of file AnimatedImageSlide.cpp.

82 {
83 sk_sp<SkData> file(GetResourceAsData("images/alphabetAnim.gif"));
84 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(file));
85 if (!codec) {
86 return;
87 }
88
89 fImage = SkAnimatedImage::Make(SkAndroidCodec::MakeFromCodec(std::move(codec)));
90 if (!fImage) {
91 return;
92 }
93
94 fTimeToShowNextFrame = fImage->currentFrameDuration();
95 SkPictureRecorder recorder;
96 auto canvas = recorder.beginRecording(fImage->getBounds());
97 canvas->drawDrawable(fImage.get());
98 fDrawable = recorder.finishRecordingAsDrawable();
99 }
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition: Resources.cpp:42
static std::unique_ptr< SkAndroidCodec > MakeFromCodec(std::unique_ptr< SkCodec >)
static sk_sp< SkAnimatedImage > Make(std::unique_ptr< SkAndroidCodec >, const SkImageInfo &info, SkIRect cropRect, sk_sp< SkPicture > postProcess)
int currentFrameDuration()
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
Definition: SkCodec.cpp:241
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkDrawable > finishRecordingAsDrawable()

◆ onChar()

bool AnimatedImageSlide::onChar ( SkUnichar  uni)
inlineoverridevirtual

Reimplemented from Slide.

Definition at line 101 of file AnimatedImageSlide.cpp.

101 {
102 if (fImage) {
103 switch (uni) {
104 case kPauseKey:
105 fRunning = !fRunning;
106 if (!fImage->isFinished()) {
107 return true;
108 }
109 [[fallthrough]];
110 case kResetKey:
111 fImage->reset();
112 fCurrentTime = fLastWallTime;
113 fTimeToShowNextFrame = fCurrentTime + fImage->currentFrameDuration();
114 return true;
115 default:
116 break;
117 }
118 }
119 return false;
120 }

The documentation for this class was generated from the following file: