Videopip.kt 此代码用于画中画模式。
package com.example.tex
import android.app.PictureInPictureParams
import android.content.res.Configuration
import android.graphics.Point
import android.os.Build
import android.os.Bundle
import android.util.Rational
import androidx.appcompat.app.AppCompatActivity
@Suppress("DEPRECATION")
class Videopip : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?){
print("Inner")
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
val mpipParams=PictureInPictureParams.Builder()
val display=windowManager.defaultDisplay
val point=Point()
display.getRealSize(point)
mpipParams.setAspectRatio(Rational(point.x,point.y))
enterPictureInPictureMode(mpipParams.build())
}
}
override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration?) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
if (isInPictureInPictureMode){
}else{
}
}
}
MainActivity.kt 这段代码是mainActivity调用平台通道的代码。
package com.example.tex
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val channel = "PictureinPicture"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger,
channel).setMethodCallHandler {
call, result ->
if (call.method=="pips"){
Videopip()
result.success("Hai")
}
}
}
}
此代码调用方法调用这是调用方法的颤振代码。
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const pip = const MethodChannel('PictureinPicture');
Future<void> pips() async {
String? pipoutput;
try {
var result = await pip.invokeMethod('pips');
setState(() {
pipoutput = result;
});
} on PlatformException catch (e) {
print(e.toString());
}
print(pipoutput!);
}
@override
Widget build(BuildContext context) {
return Container(
child: ElevatedButton(
child: Text('Click Me'),
onPressed: pips,
),
);
}
}
按下按钮时。该应用程序想要进入点子模式。但它不起作用。甚至消息“Hai”消息也会传递给颤振,但 pip 模式不起作用。我不知道我在哪里做错了。请有人帮忙解决这个问题。
AndroidManifyt.XML
<application
android:label="tex"
android:icon="@mipmap/ic_launcher">
<activity android:name=".Videopip"
android:supportsPictureInPicture="true"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
/>
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">