赞
踩
VR在现在生活中已经随处可见,在移动端上的应用更是屡见不鲜,在android 开发中VR更是应用在多个领域,汽车、家装、景区等,下面我就使用google的vr-sdk简单实现全景:
compile 'com.google.vr:sdk-panowidget:1.80.0'
我用的是google的最新版的,后续可能还会有更新
<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"/>
在加载的时候设置加载事件监听
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;
// //加载本地的图片源
// vrPanoramaView.loadImageFromBitmap(BitmapFactory.decodeStream(istr), panoOptions);
//其中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;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。