5package dev.flutter.scenarios;
7import static io.flutter.Build.API_LEVELS;
9import android.annotation.TargetApi;
14import android.graphics.SurfaceTexture;
15import android.view.Choreographer;
18import androidx.annotation.NonNull;
19import androidx.annotation.Nullable;
21import io.flutter.plugin.common.MessageCodec;
22import io.flutter.plugin.common.StringCodec;
23import io.flutter.plugin.platform.PlatformView;
24import io.flutter.plugin.platform.PlatformViewFactory;
25import java.nio.ByteBuffer;
27@TargetApi(API_LEVELS.API_23)
34 public ByteBuffer encodeMessage(@Nullable Object o) {
35 if (o instanceof String) {
43 public Object decodeMessage(@Nullable ByteBuffer byteBuffer) {
49 @SuppressWarnings(
"unchecked")
53 return new TexturePlatformView(context);
56 private static class TexturePlatformView
implements PlatformView {
57 static String
TAG =
"TexturePlatformView";
59 final TextureView textureView;
61 @SuppressWarnings(
"unchecked")
62 TexturePlatformView(@NonNull final Context context) {
63 textureView =
new TextureView(context);
64 textureView.setSurfaceTextureListener(
65 new TextureView.SurfaceTextureListener() {
67 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
68 Log.i(TAG,
"onSurfaceTextureAvailable");
69 final Canvas canvas = textureView.lockCanvas();
70 canvas.drawColor(Color.WHITE);
72 final Paint paint = new Paint();
73 paint.setColor(Color.GREEN);
74 canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 20, paint);
75 textureView.unlockCanvasAndPost(canvas);
76 Choreographer.getInstance()
77 .postFrameCallbackDelayed(
78 new Choreographer.FrameCallback() {
80 public void doFrame(long frameTimeNanos) {
81 textureView.invalidate();
88 public boolean onSurfaceTextureDestroyed(SurfaceTexture
surface) {
89 Log.
i(
TAG,
"onSurfaceTextureDestroyed");
94 public void onSurfaceTextureSizeChanged(SurfaceTexture
surface,
int width,
int height) {
95 Log.i(
TAG,
"onSurfaceTextureSizeChanged");
99 public void onSurfaceTextureUpdated(SurfaceTexture
surface) {
100 Log.i(
TAG,
"onSurfaceTextureUpdated");
107 public View getView() {
112 public void dispose() {}
TexturePlatformViewFactory()
PlatformView create(@NonNull Context context, int id, @Nullable Object args)
static void i(@NonNull String tag, @NonNull String message)
static final StringCodec INSTANCE
ByteBuffer encodeMessage(@Nullable String message)
String decodeMessage(@Nullable ByteBuffer message)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
void Log(const char *format,...) SK_PRINTF_LIKE(1