VR開發基礎—全景圖

一、導入谷歌官方提供的庫:java

commonwidget、common、panowidget(全景圖)、videowidget(視頻)
或者添加依賴:
dependencies {
    compile project(':libraries-common')
    compile project(':libraries-commonwidget')
    compile project(':libraries-panowidget')
}
版本要求:
<uses-sdkandroid:minSdkVersion="19"android:targetSdkVersion="22"/>
二、配置清單文件:
<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>
三、佈局加載 全景資源(圖片)控件
<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>
四、初始化控件以及監控加載事件
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();
        }
    }
五、界面其餘元素設置:
setFullscreenButtonEnabled (false);//隱藏全屏模式按鈕
setVrModeButtonEnabled(false);//隱藏VR模式按鈕
vrImage.setInfoButtonEnabled(false);//隱藏信息按鈕
六、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度。
相關文章
相關標籤/搜索