这里有另一种解决方案,它使用片段来展示缩放的UI。与我
之前的解决方案不同,这个解决方案的优点在于能够展示为PIP模式优化的UI。(例如,在PIP模式下可以隐藏某些视图。)
以下代码使用onPictureInPictureModeChanged()监听模式变化,并在下次重启时更改UI。(因为在PIP模式下不需要工具栏,所以在进入PIP模式之前将其隐藏。)
public class Activity extends AppCompatActivity {
private static final String FRAGMENT_TAG_FULL = "fragment_full";
private static final String FRAGMENT_TAG_PIP = "fragment_pip";
private MyApplication mApplication;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApplication = (MyApplication) getApplicationContext();
setContentView(R.layout.activity);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
if (!mApplication.inPipMode) {
showFullFragment();
} else {
showPipFragment();
}
}
@Override
protected void onUserLeaveHint() {
mToolbar.setVisibility(View.GONE);
PictureInPictureParams params = new PictureInPictureParams.Builder().build();
enterPictureInPictureMode(params);
}
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
if (isInPictureInPictureMode) {
mApplication.inPipMode = true;
} else {
mApplication.inPipMode = false;
}
}
private void showFullFragment() {
Fragment fragment = new FragmentFull();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_content, fragment, FRAGMENT_TAG_FULL)
.commit();
mToolbar.setVisibility(View.VISIBLE);
}
private void showPipFragment() {
Fragment fragment = new FragmentPip();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_content, fragment, FRAGMENT_TAG_PIP)
.commit();
mToolbar.setVisibility(View.GONE);
}
}
由于 onUserLeaveHint() 方法会在 onSaveInstanceState() 方法之后调用,这将启动 PIP 模式,因此当前模式无法存储在 activity 类的字段中。它必须存储在其他地方,以便在配置更改时保留。此处使用应用程序类中的字段。
public class MyApplication extends Application {
public boolean inPipMode = false;
}
全屏模式下的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello World!"
android:textSize="36sp" />
<TextView
android:id="@+id/text_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:layout_centerHorizontal="true"
android:text="🙂"
android:textSize="28sp" />
</RelativeLayout>
PIP模式下的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello World!"
android:textSize="10sp"/>
</RelativeLayout>
结果:
![result](https://istack.dev59.com/JCUu4.gif)