Android開發(fā)中Google VR全景的實現(xiàn)

VR在現(xiàn)在生活中已經(jīng)隨處可見,在移動端上的應(yīng)用更是屢見不鮮,在android 開發(fā)中VR更是應(yīng)用在多個領(lǐng)域,汽車、家裝、景區(qū)等,下面我就使用google的vr-sdk簡單實現(xiàn)全景:

1.效果圖如下:

dc9fafe2e7b83d4edaa5[00-00-00--00-00-08].gif

2.引入vr-sdk

    compile 'com.google.vr:sdk-panowidget:1.80.0'

我用的是google的最新版的,后續(xù)可能還會有更新

3.layout中布局控件的引入

    <com.google.vr.sdk.widgets.pano.VrPanoramaView
        android:id="@+id/pano_view"
        android:layout_width="match_parent"
        android:scrollbars="@null"
        android:layout_height="match_parent"/>

4.加載并展示圖片

在加載的時候設(shè)置加載事件監(jiān)聽

 vrPanoramaView.setEventListener(new ActivityEventListener());

其中view的方法如下所示:這里只列出了一些在demo圖片加載我采用的是從assets中進行異步加載,如果在實際開發(fā)中基本上都是從網(wǎng)絡(luò)中加載的

        vrPanoramaView.setFullscreenButtonEnabled (false); //隱藏全屏模式按鈕
        vrPanoramaView.setInfoButtonEnabled(false); //設(shè)置隱藏最左邊信息的按鈕
        vrPanoramaView.setStereoModeButtonEnabled(false); //設(shè)置隱藏立體模型的按鈕
        vrPanoramaView.setEventListener(new ActivityEventListener()); //設(shè)置監(jiān)聽
        panoOptions.inputType = VrPanoramaView.Options.TYPE_MONO;
   //其中type_MONO源碼中有四種分別是
        private static final int TYPE_START_MARKER = 0;
        public static final int TYPE_MONO = 1;
        public static final int TYPE_STEREO_OVER_UNDER = 2;
        private static final int TYPE_END_MARKER = 3;
//在這里因為用到左右眼VR 所以用單聲道模式,
//圖片中其中有上下兩張圖片,戴上全景設(shè)備之后分別能在 左右眼中分別看到
//如果是單張的全景圖片就用TYPE_STEREO_OVER_UNDER =2就可以 

ActivityEventListener的實現(xiàn)如下:

    public class ActivityEventListener extends VrPanoramaEventListener {
        @Override
        public void onLoadSuccess() {
            loadImageSuccessful = true;
        }
        @Override
        public void onLoadError(String errorMessage) {
            loadImageSuccessful=false;
            Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
        }
    }

這里只是對加載狀態(tài)做一個toast,讀者可以進行自定義這些
進行異步加載圖片,避免堵塞UI線程

       if (backgroundImageLoaderTask != null) {
           // Cancel any task from a previous intent sent to this activity.
           backgroundImageLoaderTask.cancel(true);
       }
       backgroundImageLoaderTask = new ImageLoaderTask();
       backgroundImageLoaderTask.execute(Pair.create(fileUri, panoOptions));

具體加載過程如下:

 public class ImageLoaderTask extends AsyncTask<Pair<Uri, VrPanoramaView.Options>, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Pair<Uri, VrPanoramaView.Options>... fileInformation) {
            VrPanoramaView.Options panoOptions = null;  // It's safe to use null VrPanoramaView.Options.
            InputStream istr = null;
            if (fileInformation == null || fileInformation.length < 1
                    || fileInformation[0] == null || fileInformation[0].first == null) {
                AssetManager assetManager = getAssets();
                try {
                    istr = assetManager.open("1110.jpg");
                    panoOptions = new VrPanoramaView.Options();
                    panoOptions.inputType = VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER;
                } catch (IOException e) {
                    Log.e(TAG, "Could not decode default bitmap: " + e);
                    return false;
                }
            } else {
                try {
                    istr = new FileInputStream(new File(fileInformation[0].first.getPath()));
                    panoOptions = fileInformation[0].second;
                } catch (IOException e) {
                    Log.e(TAG, "Could not load file: " + e);
                    return false;
                }
            }

            vrPanoramaView.loadImageFromBitmap(BitmapFactory.decodeStream(istr), panoOptions);
            try {
                istr.close();
            } catch (IOException e) {
                Log.e(TAG, "Could not close input stream: " + e);
            }

            return true;
        }
    }

5.結(jié)束語

github-demo下載地址

如果對以上有疑問,或者是想交流的小伙伴們可以加QQ群:570650538

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容