Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Protected Member Functions | Package Functions | List of all members
io.flutter.embedding.android.FlutterImageView Class Reference
Inheritance diagram for io.flutter.embedding.android.FlutterImageView:
io.flutter.embedding.engine.renderer.RenderSurface io.flutter.plugin.platform.PlatformOverlayView

Classes

enum  SurfaceKind
 

Public Member Functions

ImageReader getImageReader ()
 
 FlutterImageView (@NonNull Context context, int width, int height, SurfaceKind kind)
 
 FlutterImageView (@NonNull Context context)
 
 FlutterImageView (@NonNull Context context, @NonNull AttributeSet attrs)
 
Surface getSurface ()
 
FlutterRenderer getAttachedRenderer ()
 
void attachToRenderer (@NonNull FlutterRenderer flutterRenderer)
 
void detachFromRenderer ()
 
void pause ()
 
void resume ()
 
boolean acquireLatestImage ()
 
void resizeIfNeeded (int width, int height)
 
void closeImageReader ()
 
FlutterRenderer getAttachedRenderer ()
 
void attachToRenderer (@NonNull FlutterRenderer renderer)
 
void detachFromRenderer ()
 
void pause ()
 
void resume ()
 

Protected Member Functions

void onDraw (Canvas canvas)
 
void onSizeChanged (int width, int height, int oldWidth, int oldHeight)
 

Package Functions

 FlutterImageView ( @NonNull Context context, @NonNull ImageReader imageReader, SurfaceKind kind)
 

Detailed Description

Paints a Flutter UI provided by an android.media.ImageReader onto a android.graphics.Canvas.

A FlutterImageView is intended for situations where a developer needs to render a Flutter UI, but also needs to render an interactive io.flutter.plugin.platform.PlatformView.

This View takes an android.media.ImageReader that provides the Flutter UI in an android.media.Image and renders it to the android.graphics.Canvas in
onDraw
.

Definition at line 44 of file FlutterImageView.java.

Constructor & Destructor Documentation

◆ FlutterImageView() [1/4]

io.flutter.embedding.android.FlutterImageView.FlutterImageView ( @NonNull Context  context,
int  width,
int  height,
SurfaceKind  kind 
)
inline

Constructs a FlutterImageView with an android.media.ImageReader that provides the Flutter UI.

Definition at line 74 of file FlutterImageView.java.

74 {
75 this(context, createImageReader(width, height), kind);
76 }
int32_t height
int32_t width

◆ FlutterImageView() [2/4]

io.flutter.embedding.android.FlutterImageView.FlutterImageView ( @NonNull Context  context)
inline

Definition at line 78 of file FlutterImageView.java.

78 {
79 this(context, 1, 1, SurfaceKind.background);
80 }

◆ FlutterImageView() [3/4]

io.flutter.embedding.android.FlutterImageView.FlutterImageView ( @NonNull Context  context,
@NonNull AttributeSet  attrs 
)
inline

Definition at line 82 of file FlutterImageView.java.

82 {
83 this(context, 1, 1, SurfaceKind.background);
84 }

◆ FlutterImageView() [4/4]

io.flutter.embedding.android.FlutterImageView.FlutterImageView ( @NonNull Context  context,
@NonNull ImageReader  imageReader,
SurfaceKind  kind 
)
inlinepackage

Definition at line 87 of file FlutterImageView.java.

88 {
89 super(context, null);
90 this.imageReader = imageReader;
91 this.kind = kind;
92 init();
93 }

Member Function Documentation

◆ acquireLatestImage()

boolean io.flutter.embedding.android.FlutterImageView.acquireLatestImage ( )
inline

Acquires the next image to be drawn to the android.graphics.Canvas. Returns true if there's an image available in the queue.

Definition at line 190 of file FlutterImageView.java.

190 {
191 if (!isAttachedToFlutterRenderer) {
192 return false;
193 }
194 // 1. `acquireLatestImage()` may return null if no new image is available.
195 // 2. There's no guarantee that `onDraw()` is called after `invalidate()`.
196 // For example, the device may not produce new frames if it's in sleep mode
197 // or some special Android devices so the calls to `invalidate()` queued up
198 // until the device produces a new frame.
199 // 3. While the engine will also stop producing frames, there is a race condition.
200 final Image newImage = imageReader.acquireLatestImage();
201 if (newImage != null) {
202 // Only close current image after acquiring valid new image
203 closeCurrentImage();
204 currentImage = newImage;
205 invalidate();
206 }
207 return newImage != null;
208 }
CanvasImage Image
Definition: dart_ui.cc:55

◆ attachToRenderer()

void io.flutter.embedding.android.FlutterImageView.attachToRenderer ( @NonNull FlutterRenderer  flutterRenderer)
inline

Invoked by the owner of this FlutterImageView when it wants to begin rendering a Flutter UI to this FlutterImageView.

Implements io.flutter.embedding.engine.renderer.RenderSurface.

Definition at line 142 of file FlutterImageView.java.

142 {
143 switch (kind) {
144 case background:
145 flutterRenderer.swapSurface(imageReader.getSurface());
146 break;
147 case overlay:
148 // Do nothing since the attachment is done by the handler of
149 // `FlutterJNI#createOverlaySurface()` in the native side.
150 break;
151 }
152 setAlpha(1.0f);
153 this.flutterRenderer = flutterRenderer;
154 isAttachedToFlutterRenderer = true;
155 }

◆ closeImageReader()

void io.flutter.embedding.android.FlutterImageView.closeImageReader ( )
inline

Closes the image reader associated with the current FlutterImageView.

Once the image reader is closed, calling acquireLatestImage will result in an
IllegalStateException
.

Definition at line 233 of file FlutterImageView.java.

233 {
234 imageReader.close();
235 }

◆ detachFromRenderer()

void io.flutter.embedding.android.FlutterImageView.detachFromRenderer ( )
inline

Invoked by the owner of this FlutterImageView when it no longer wants to render a Flutter UI to this FlutterImageView.

Implements io.flutter.embedding.engine.renderer.RenderSurface.

Definition at line 161 of file FlutterImageView.java.

161 {
162 if (!isAttachedToFlutterRenderer) {
163 return;
164 }
165 setAlpha(0.0f);
166 // Drop the latest image as it shouldn't render this image if this view is
167 // attached to the renderer again.
169 // Clear drawings.
170 currentBitmap = null;
171
172 // Close and clear the current image if any.
173 closeCurrentImage();
174 invalidate();
175 isAttachedToFlutterRenderer = false;
176 }

◆ getAttachedRenderer()

FlutterRenderer io.flutter.embedding.android.FlutterImageView.getAttachedRenderer ( )
inline

Returns the FlutterRenderer that is attached to this RenderSurface, or null if no FlutterRenderer is currently attached.

Implements io.flutter.embedding.engine.renderer.RenderSurface.

Definition at line 133 of file FlutterImageView.java.

133 {
134 return flutterRenderer;
135 }

◆ getImageReader()

ImageReader io.flutter.embedding.android.FlutterImageView.getImageReader ( )
inline

Definition at line 52 of file FlutterImageView.java.

52 {
53 return imageReader;
54 }

◆ getSurface()

Surface io.flutter.embedding.android.FlutterImageView.getSurface ( )
inline

Definition at line 127 of file FlutterImageView.java.

127 {
128 return imageReader.getSurface();
129 }

◆ onDraw()

void io.flutter.embedding.android.FlutterImageView.onDraw ( Canvas  canvas)
inlineprotected

Definition at line 238 of file FlutterImageView.java.

238 {
239 super.onDraw(canvas);
240 if (currentImage != null) {
241 updateCurrentBitmap();
242 }
243 if (currentBitmap != null) {
244 canvas.drawBitmap(currentBitmap, 0, 0, null);
245 }
246 }

◆ onSizeChanged()

void io.flutter.embedding.android.FlutterImageView.onSizeChanged ( int  width,
int  height,
int  oldWidth,
int  oldHeight 
)
inlineprotected

Definition at line 286 of file FlutterImageView.java.

286 {
287 if (width == imageReader.getWidth() && height == imageReader.getHeight()) {
288 return;
289 }
290 // `SurfaceKind.overlay` isn't resized. Instead, the `FlutterImageView` instance
291 // is destroyed. As a result, an instance with the new size is created by the surface
292 // pool in the native side.
293 if (kind == SurfaceKind.background && isAttachedToFlutterRenderer) {
295 // Bind native window to the new surface, and create a new onscreen surface
296 // with the new size in the native side.
297 flutterRenderer.swapSurface(imageReader.getSurface());
298 }
299 }

◆ pause()

void io.flutter.embedding.android.FlutterImageView.pause ( )
inline

Instructs this RenderSurface to stop forwarding Surface notifications to the FlutterRenderer that was previously connected with attachToRenderer(FlutterRenderer).

Implements io.flutter.embedding.engine.renderer.RenderSurface.

Definition at line 178 of file FlutterImageView.java.

178 {
179 // Not supported.
180 }

◆ resizeIfNeeded()

void io.flutter.embedding.android.FlutterImageView.resizeIfNeeded ( int  width,
int  height 
)
inline

Creates a new image reader with the provided size.

Definition at line 211 of file FlutterImageView.java.

211 {
212 if (flutterRenderer == null) {
213 return;
214 }
215 if (width == imageReader.getWidth() && height == imageReader.getHeight()) {
216 return;
217 }
218
219 // Close resources.
220 closeCurrentImage();
221 // Close the current image reader, then create a new one with the new size.
222 // Image readers cannot be resized once created.
224 imageReader = createImageReader(width, height);
225 }

◆ resume()

void io.flutter.embedding.android.FlutterImageView.resume ( )
inline

Instructs this RenderSurface to resume forwarding Surface notifications to the FlutterRenderer that was previously connected with attachToRenderer(FlutterRenderer).

Implements io.flutter.embedding.engine.renderer.RenderSurface.

Definition at line 182 of file FlutterImageView.java.

182 {
183 // Not supported.
184 }

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