Skip to content

Commit e0e7c18

Browse files
committedJun 28, 2022
Improve picture in picture transition
1 parent 4c1765f commit e0e7c18

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
 

‎app/src/main/java/com/perflyst/twire/activities/stream/StreamActivity.java

+4
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ public void onBackPressed() {
200200
public void onUserLeaveHint() {
201201
super.onUserLeaveHint();
202202

203+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
204+
return;
205+
}
206+
203207
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
204208
mStreamFragment.prePictureInPicture();
205209
enterPictureInPictureMode();

‎app/src/main/java/com/perflyst/twire/fragments/StreamFragment.java

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.app.PendingIntent;
5+
import android.app.PictureInPictureParams;
56
import android.content.BroadcastReceiver;
67
import android.content.ComponentName;
78
import android.content.Context;
@@ -22,6 +23,7 @@
2223
import android.transition.Transition;
2324
import android.util.DisplayMetrics;
2425
import android.util.Log;
26+
import android.util.Rational;
2527
import android.view.Display;
2628
import android.view.DisplayCutout;
2729
import android.view.LayoutInflater;
@@ -653,6 +655,28 @@ public void onPlayWhenReadyChanged(boolean isPlaying, int _ignored) {
653655
}
654656
}
655657

658+
@RequiresApi(api = Build.VERSION_CODES.O)
659+
@Override
660+
public void onSurfaceSizeChanged(int width, int height) {
661+
Rect videoRect = new Rect();
662+
mVideoView.getVideoSurfaceView().getGlobalVisibleRect(videoRect);
663+
664+
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
665+
videoRect.left += totalVerticalInset;
666+
videoRect.right += totalVerticalInset;
667+
}
668+
669+
PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder()
670+
.setAspectRatio(new Rational(16, 9))
671+
.setSourceRectHint(videoRect);
672+
673+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
674+
builder.setAutoEnterEnabled(true);
675+
}
676+
677+
mActivity.setPictureInPictureParams(builder.build());
678+
}
679+
656680
/**
657681
* Hides the preview image and updates the state
658682
*/

0 commit comments

Comments
 (0)
Please sign in to comment.