android画中画比例不对,android - Android画中画比例视图 - SO中文参考 - www.soinside.com...

这是另一种使用片段来显示缩放UI的解决方案。与我的previous solution不同,此解决方案的优势在于能够显示针对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() - 它启动PIP模式 - 在onSaveInstanceState()之后调用,所以当前模式不能存储在activity类的字段中。它必须存储在配置更改中幸存的其他位置。此处使用应用程序类中的字段。

public class MyApplication extends Application {

public boolean inPipMode = false;

}

全屏模式的片段布局:

android:layout_width="match_parent"

android:layout_height="match_parent">

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" />

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" />

PIP模式的片段布局:

android:layout_width="match_parent"

android:layout_height="match_parent">

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"/>

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值