如何使用颤动画中画?

时间:2019-02-19 09:54:28

标签: flutter

有什么方法可以使用Flutter应用程序进行画中画吗?就像YouTube在观看视频并导航到另一个应用程序时所做的一样。

他们在这里谈论它: https://youtu.be/hBPd2q2dmXY

我搜索了它,却找不到任何信息

2 个答案:

答案 0 :(得分:1)

此功能称为“画中画模式”,与“画中画”相比,它在Google上应该容易得多。 有一个flutter包,但我还没有尝试过(不幸的是,它似乎只是android)https://pub.dartlang.org/packages/flutter_android_pip

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_android_pip/flutter_android_pip.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    //  initPlatformState();
  }
/*
  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await FlutterAndroidPip.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }*/

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: new Center(
          child: new RaisedButton(
            child: new Text("press"),
            onPressed: () {
              FlutterAndroidPip.enterPictureInPictureMode;
            },
          ),
        ),
      ),
    );
  }
}

答案 1 :(得分:1)

您所要询问的内容在flutter中不可用,您只能在本机中实现。 我制作了一个仅针对Android制作pip的应用程序。

为此首先在flutter main.dart中声明一个通道 像:-

static const platform = const MethodChannel('flutter.rortega.com.channel');

然后在按钮中单击写入:-

 await platform.invokeMethod('showNativeView');

在mainActivity.java中调用方法

在mainActivity.java中编写以下代码:

package com.kovafood;

import io.flutter.embedding.android.FlutterActivity;

import android.app.PictureInPictureParams;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.Rational;
import android.view.Display;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import androidx.multidex.MultiDex;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import androidx.annotation.NonNull;
public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "flutter.rortega.com.channel";

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler(
                        (call, result) -> {
                            if (call.method.equals("showNativeView")){
                                Display d = getWindowManager()
                                        .getDefaultDisplay();
                                Point p = new Point();
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                                    d.getSize(p);
                                }
                                int width = p.x;
                                int height = p.y;
                                Rational ratio
                                        = null;
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                    ratio = new Rational(width, height);
                                }
                                PictureInPictureParams.Builder
                                        pip_Builder
                                        = null;
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                    pip_Builder = new PictureInPictureParams.Builder();
                                }
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                    pip_Builder.setAspectRatio(ratio).build();
                                }
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                    enterPictureInPictureMode(pip_Builder.build());
                                }
                            } else {
                                result.notImplemented();
                            }
                        }
                );
    }
}

在androidManifest.xml中,在第一个活动之间添加

android:supportsPictureInPicture="true"