Developer's Blog

【Android O】Picture In Picture を使ってみました

こんにちは。
アプリケーション共同開発部の池田です。

Picture In Picture は Android 7.0 で追加されたマルチウィンドウ モードの1つで、ユーザーが他のアプリを操作していても、動画などのコンテンツを表示することができます。
Android O では、従来 Android TV でのみ利用できた Picture In Picture が、すべての Android 端末で利用できるようになります。

本記事では Picture In Picture の使用方法と、使用感を紹介します。

※ Developer Preview 1 で確認しているため、今後動作が変わる可能性があります。

Picture In Picture の使用方法

使用の宣言

AndroidManifest で Picture In Picture を使用したい Activity の android:supportsPictureInPicture を true に設定します。

 
<activity android:name=".VideoActivity"
          android:supportsPictureInPicture="true">

Picture In Picture の開始

android:supportsPictureInPicture を true に設定した Activity で Activity.enterPictureInPictureMode() を呼び出すことで切り替わります。

Picture In Picture 時には、ウィンドウサイズはシステムにより自動的に決定され、ウィンドウにタッチイベントが届きませんが、Android O では Activity.enterPictureInPictureMode() や Activity.setPictureInPictureArgs() で PictureInPictureArgs オブジェクトを渡すことで以下の制御が可能です。

  • ウィンドウのアスペクト比の指定
  • Picture In Picture 専用のメニューの表示及びメニュータップ時に発行される PendingIntent の設定

PictureInPictureArgs args = new PictureInPictureArgs();
// Picture In Pictureのメニュータップ時のActionを設定
Intent intent = new Intent(ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags);
Icon icon = Icon.createWithResource(context, R.mipmap.ic_launcher);//Picture In Pictureのメニュー用アイコン
RemoteAction action = new RemoteAction(icon, "title", "contentDescription", pendingIntent);
List<RemoteAction> actions = new ArrayList<>(Arrays.asList(action));
args.setActions(actions);
//アスペクト比設定(1:2) 
args.setAspectRatio(2.0f); 
//Picture In Picture の開始
enterPictureInPictureMode(args);

 

使用感

Picture In Picture を開始すると、ウィンドウが他アプリの上に重ねて描画されます。ウィンドウをドラッグすることで任意の位置に移動できます。

   

ウィンドウを画面外までドラッグすると、ウィンドウの一部のみが表示されます。文字入力時など一時的に場所を空けたいときに便利です。

PictureInPictureArgs でメニューを表示するように設定した状態でウィンドウをタップすると、メニューが表示されます。

参考

 

フェンリルのオフィシャル Twitter アカウントでは、フェンリルプロダクトの最新情報などをつぶやいています。よろしければフォローしてください!

フェンリルの開発者アカウントでは、フェンリルプロダクトの最新開発情報などをつぶやいています。よろしければフォローしてください!

フェンリルの Facebook ページでは、最新トピックをお知らせしています。よろしければいいね!してください!

Sleipnir の Facebook ページでは、ユーザーの方たちとのコミュニケーションや最新情報の投稿などを行なっています。よろしければいいね!してください!

Copyright © 2019 Fenrir Inc. All rights reserved.