Android——画中画模式
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra("reason");
if (!reason.isEmpty() && (reason.equals("homekey") || reason.equals("recentapps"))) {
// Android8.0开始才提供画中画模式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isInPictureInPictureMode()) {
PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
// 设置宽高比例值,第一个参数表示分子,第二个参数表示分母
// 下面的10/5=2,表示画中画窗口的宽度是高度的两倍
Rational ratio = new Rational(10, 5);
builder.setAspectRatio(ratio);
// 进入画中画模式
enterPictureInPictureMode(builder.build());
}
}
}
}
};