1、導入谷歌官方提供的庫:
commonwidget、common、panowidget(全景圖)、videowidget(視頻)
或者添加依賴:
dependencies {
compile project(':libraries-common')
compile project(':libraries-commonwidget')
compile project(':libraries-panowidget')
}
版本要求:
<uses-sdkandroid:minSdkVersion="19"android:targetSdkVersion="22"/>
2、配置清單文件:
<uses-permissionandroid:name="android.permission.INTERNET"/> <uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/> <applicationandroid:label="SimpleVrPanoramaActivity" android:largeHeap="true" android:theme="@android:style/Theme.Holo.Light"> <activityandroid:name=".SimpleVrPanoramaActivity"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> <categoryandroid:name="com.google.intent.category.CARDBOARD"/> </intent-filter> </activity> </application>
3、布局加載 全景資源(圖片)控件
<com.google.vr.sdk.widgets.pano.VrPanoramaView android:id="@+id/picView" android:layout_width="match_parent" android:layout_height="match_parent"> </com.google.vr.sdk.widgets.pano.VrPanoramaView>
4、初始化控件以及監控加載事件
publicclassSimpleVrPanoramaActivityextendsActivity { privatestaticfinalString TAG ="VrPanorama"; privateVrPanoramaViewpanoWidgetView;//上面說的Google提供給我們現實全景圖片的View privateString fileUri ="first.jpg";//assets文件夾下的文件名 privateOptionspanoOptions =newOptions();//VrPanoramaView需要的設置 privateImageLoaderTask backgroundImageLoaderTask;//異步加載圖片 @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); panoWidgetView =(VrPanoramaView) findViewById(R.id.pano_view);//初始化VrPanoramaView panoWidgetView.setEventListener(newActivityEventListener());//為VrPanoramaView添加監聽 //如果有任務在執行則停止它 if(backgroundImageLoaderTask !=null) { backgroundImageLoaderTask.cancel(true); } //設置inputType 為TYPE_STEREO_OVER_UNDER. panoOptions.inputType =Options.TYPE_STEREO_OVER_UNDER; //創建一個任務 backgroundImageLoaderTask =newImageLoaderTask(); //執行任務。將圖片名(根據項目實際情況傳)和設置傳入 backgroundImageLoaderTask.execute(Pair.create(fileUri, panoOptions)); } //異步任務 classImageLoaderTaskextendsAsyncTask<Pair<String,Options>,Void,Boolean>{ @Override protectedBoolean doInBackground(Pair<String,Options>... fileInformation) { InputStream istr =null; try { istr = getAssets().open(fileInformation[0].first);//獲取圖片的輸入流 Bitmap bitmap =BitmapFactory.decodeStream(istr);//創建bitmap //參數一為圖片的bitmap,參數二為 VrPanoramaView 所需要的設置 panoWidgetView.loadImageFromBitmap(bitmap, fileInformation[0].second); } catch(IOException e) { Log.e(TAG,"Could not decode default bitmap: "+ e); returnfalse; } finally { try{ istr.close();//關閉InputStream } catch(IOException e) { //...} } returntrue; } } privateclassActivityEventListenerextendsVrPanoramaEventListener{ @Override publicvoid onLoadSuccess() { //圖片加載成功 Log.e(TAG,"onLoadSuccess"); } @Override publicvoid onLoadError(String errorMessage) { //圖片加載失敗 Log.e(TAG,"Error loading pano: "+ errorMessage); } @Override publicvoid onClick() { //當我們點擊了VrPanoramaView 時候出發 super.onClick(); Log.e(TAG,"onClick"); } @Override publicvoid onDisplayModeChanged(int newDisplayMode) { //改變顯示模式時候出發(全屏模式和紙板模式) super.onDisplayModeChanged(newDisplayMode); Log.e(TAG,"onDisplayModeChanged"); } } @Override protectedvoid onPause() { panoWidgetView.pauseRendering();//暫停3D渲染和跟蹤 super.onPause(); } @Override protectedvoid onResume() { super.onResume(); panoWidgetView.resumeRendering();//恢復3D渲染和跟蹤 } @Override protectedvoid onDestroy() { panoWidgetView.shutdown();//關閉渲染下並釋放相關的內存 if(backgroundImageLoaderTask !=null) { backgroundImageLoaderTask.cancel(true);//停止異步任務 } super.onDestroy(); } }
5、界面其他元素設置:
setFullscreenButtonEnabled (false);//隱藏全屏模式按鈕
setVrModeButtonEnabled(false);//隱藏VR模式按鈕
vrImage.setInfoButtonEnabled(false);//隱藏信息按鈕
6、Options參數:
①
public static final int TYPE_MONO = 1;
- 圖像被預期以覆蓋沿着其水平軸360度,而垂直范圍是根據圖像的寬高比來計算。
- 例如,如果一個1000x250像素的圖像,給出所述全景將覆蓋360x90度與垂直范圍是-45至+45度
②
public static final int TYPE_STEREO_OVER_UNDER = 2;
- 包含兩個大小相等的投影 全景圖垂直疊加。頂部圖像被顯示給左眼、底部圖像被顯示給右眼
- 圖像將覆蓋沿水平軸360度,而垂直范圍是根據圖像的寬高比來計算。
- 例如,如果一個1000x500像素的圖像中給出(即1000x250像素/每個眼睛),全景將覆蓋360x90度與垂直范圍是-45至+45度。
