VR在現在生活中已經隨處可見,在移動端上的應用更是屢見不鮮,在android 開發中VR更是應用在多個領域,汽車、家裝、景區等,下面我就使用google的vr-sdk簡單實現全景:
1.效果圖如下:
dc9fafe2e7b83d4edaa5[00-00-00--00-00-08].gif
2.引入vr-sdk
compile 'com.google.vr:sdk-panowidget:1.80.0'
我用的是google的最新版的,后續可能還會有更新
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.加載并展示圖片
在加載的時候設置加載事件監聽
vrPanoramaView.setEventListener(new ActivityEventListener());
其中view的方法如下所示:這里只列出了一些在demo圖片加載我采用的是從assets中進行異步加載,如果在實際開發中基本上都是從網絡中加載的
vrPanoramaView.setFullscreenButtonEnabled (false); //隱藏全屏模式按鈕
vrPanoramaView.setInfoButtonEnabled(false); //設置隱藏最左邊信息的按鈕
vrPanoramaView.setStereoModeButtonEnabled(false); //設置隱藏立體模型的按鈕
vrPanoramaView.setEventListener(new ActivityEventListener()); //設置監聽
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 所以用單聲道模式,
//圖片中其中有上下兩張圖片,戴上全景設備之后分別能在 左右眼中分別看到
//如果是單張的全景圖片就用TYPE_STEREO_OVER_UNDER =2就可以
ActivityEventListener的實現如下:
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();
}
}
這里只是對加載狀態做一個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;
}
}