有没有办法用flutter应用程序做画中画?有点像 YouTube 在您观看视频并导航到另一个应用程序时所做的事情。
他们在这里谈论它:https : //youtu.be/hBPd2q2dmXY
我搜索了它,但找不到任何有关它的信息
该功能称为“画中画模式”,它应该比“画中画”更容易谷歌搜索。有一个 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;
},
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
您所要求的在颤振中不可用,您必须仅在本机中实现它。我制作了一款仅适用于 android 的 pip 应用程序。
为此,首先在 flutter main.dart 中声明一个通道,例如:-
static const platform = const MethodChannel('flutter.rortega.com.channel');
Run Code Online (Sandbox Code Playgroud)
然后在按钮中单击写入:-
await platform.invokeMethod('showNativeView');
Run Code Online (Sandbox Code Playgroud)
调用 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();
}
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
在 androidManifest.xml 中,在第一个活动之间添加
android:supportsPictureInPicture="true"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4690 次 |
| 最近记录: |