Android 8.0 中如何實現(xiàn)視頻通話的畫中畫模式的示例
Android 8.0 當中允許 Activiy 以畫中畫模式展現(xiàn)。這是一種多窗口模式的改進加強,在視頻類應用中用處非常大,有了這種模式,就可以在視頻通話或者觀看直播的過程當中打開另外的應用而不用退出當前視頻。更詳細的就不再累述了,大家去閱讀官方文檔 就行
這里以 Agora SDK 為例來給大家展示下該特性,實際上不用 Agora SDK 做任何修改。
準備環(huán)境
- Android 8.0 或以上版本手機
- Agora SDK 1.14.0 或以上 版本
- Android Studio 3.0 或以上版本(非必需)
如何實現(xiàn)畫中畫模式
默認應用是不支持畫中畫模式的,需要給視頻所在的 Activity 做些配置,如下在 AndroidManifest.xml 加上屬性 resizeableActivity/supportsPictureInPicture 并均設置為 true
android:resizeableActivity="true" android:supportsPictureInPicture="true" android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
為了進入畫中畫模式,Activty 必需要用 enterPictureInPictureMode(PictureInPictureParams params) 方法,非常的簡單,但是為了告訴系統(tǒng)進入畫中畫模式之后,Activity 界面在整個屏幕當中的布局,我們需要設置一些參數(shù)。我們這里簡單設置下,具體在使用的時候需要根據(jù)屏幕的分辨率動態(tài)取設置,更多信息參考官方文檔。
PictureInPictureParams params = new PictureInPictureParams.Builder() .setAspectRatio(new Rational(10, 16)) .build();
當然需要在程序當中控制 Acticity 界面當中的內容,比如我們可以隱藏自己本地的預覽畫面,隱藏不需要的按鈕信息等等,這個實現(xiàn)也非常簡單。
@Override public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) { super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig); FrameLayout container = findViewById(R.id.local_video_view_container); SurfaceView surfaceView = (SurfaceView) container.getChildAt(0); surfaceView.setZOrderMediaOverlay(!isInPictureInPictureMode); surfaceView.setVisibility(isInPictureInPictureMode ? View.GONE : View.VISIBLE); container.setVisibility(isInPictureInPictureMode ? View.GONE : View.VISIBLE); }
另外值得一說的是,進入畫中畫模式,系統(tǒng)會觸發(fā)生命周期的方法 onPause/onResume 方法,我們需要根據(jù)需要適當?shù)淖鲂┎僮?,比如是畫中畫模式的話,就不做任何操作,音視頻流繼續(xù),否則的話,就關閉視頻流,反正在后臺也看不見視頻。
另外Android 8.0 畫中畫demo
記錄一下簡單的demo ,方便以后用到:
package com.example.myapplication; import android.annotation.TargetApi; import android.app.PictureInPictureParams; import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.util.Rational; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.TextView; /** * 畫中畫 */ public class TestPIPActivity extends AppCompatActivity { private static final String TAG = "TestPIPActivity"; private PictureInPictureParams.Builder mPictureInPictureParamsBuilder; @TargetApi(Build.VERSION_CODES.O) @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); FrameLayout content = new FrameLayout(this); setContentView(content,new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); if(Build.VERSION.SDK_INT == Build.VERSION_CODES.O){ mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder(); final TextView textView = new TextView(this); textView.setText("test PIP"); textView.setTextSize(20); FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); fl.gravity = Gravity.CENTER ; textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//主要操作 Rational aspectRatio = new Rational(10,10); mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build(); enterPictureInPictureMode(mPictureInPictureParamsBuilder.build()); } }); content.addView(textView,fl); }else{ TextView descTv = new TextView(this); descTv.setText("當前版本不支持..."); descTv.setTextSize(20); FrameLayout.LayoutParams Tvfl = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); Tvfl.gravity = Gravity.CENTER ; content.addView(descTv,Tvfl); } } @Override public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) { super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig); Log.d(TAG,String.valueOf(isInPictureInPictureMode)); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android中ListView的幾種常見的優(yōu)化方法總結
Android中的ListView應該算是布局中幾種最常用的組件之一,本篇文章主要做了三種優(yōu)化總結,有興趣的可以了解一下。2017-02-02Android中應用界面主題Theme使用方法和頁面定時跳轉應用
在Android SDK中內置了下面的Theme,可以按標題欄Title Bar和狀態(tài)欄Status Bar是否可見來分類,感興趣的朋友可以了解下哈2013-06-06Android 滑動Scrollview標題欄漸變效果(仿京東toolbar)
這篇文章主要介紹了Android 滑動Scrollview標題欄漸變效果(仿京東toolbar),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01Android實現(xiàn)viewpager實現(xiàn)循環(huán)輪播效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)viewpager實現(xiàn)循環(huán)輪播效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03