Android 画中画
在Android 12 系统的手机(oppo 小米),home进入画中画,然后在恢复全屏模式,然后点击界面上其他按钮打开新界面,会出现 startActivity新的界面会延迟好久 或者 打不开新界面的情况。
按照官方文档使用安卓12的.setAutoEnterEnabled(true)依然不管用;
跳转不了、或者延迟、好像新页面在当前界面下面,因为home或者旋转屏幕后就打开了。
奇怪的是非home,手动调用enterPictureInPictureMode,就没有这个问题。貌似和home有关系。
大概代码如下
Button btn_floating_open2 = findViewById(R.id.btn_floating_open2);
btn_floating_open2.setOnClickListener(v -> {
Intent intent = new Intent(FloatingActivity.this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
FloatingActivity.this.startActivity(intent);
});
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
enterPiPMode();
}
private void enterPiPMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// videoPlayer.setIsInPictureInPictureMode(true);
if (mPictureInPictureParamsBuilder == null) {
mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder();
}
// Calculate the aspect ratio of the PiP screen. 计算video的纵横比
int mVideoWith = videoPlayer.getWidth();
int mVideoHeight = videoPlayer.getHeight();
if (mVideoWith != 0 && mVideoHeight != 0) {
//设置param宽高比,根据宽高比例调整初始参数
Rational aspectRatio = new Rational(mVideoWith, mVideoHeight);
mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio);
}
//进入pip模式
enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}
}