Fork me on GitHub

利用Google VR教你打造全景图片展示

本文章主要参考Google VR中的文档,如果您能流利的读懂英文开发文档,可以去官网自行查看。

1.介绍VR View

VR view allows you to embed 360 degree VR media into websites on desktop and mobile, and native apps on Android and iOS. This technology is designed to enable developers of traditional apps to enhance the apps with immersive content.

VR视图允许你将360度的VR媒体嵌入桌面和移动的网站,以及Android和iOS上的原生应用。这项技术旨在使传统应用程序的开发者能够通过沉浸式的内容来增强应用程序。

VR view supports mono and stereo 360 images and videos. Images and video need to be stored in the equirectangular-panoramic (equirect-pano) format, which is a common format supported by many capture solutions.

VR视图支持mono和立体图像和视频。图像和视频需要存储在equi矩形-全景(equirect - pano)格式中,这是许多捕获解决方案支持的公共格式。

Image规格

  • VR查看图像可以保存为PNG,JPEG或GIF。Google建议使用JPEG改进压缩。
  • 为了获得最大的兼容性和性能,图像尺寸应该是2的倍数(例如,2048或4096)。
  • 单个图像应为2:1纵横比(例如4096×2048)。
  • 立体图像应为1:1纵横比(例如4096×4096)。
mono单个图像 stereo立体图像

先来看看效果:

2.使用VrPanoramaView嵌入全景图像

1.build.gradle

在app下的build.gradle中添加panowidget库

1
2
3
dependencies {
compile 'com.google.vr:sdk-panowidget:1.10.0'
}

2.AndroiManifest.xml

在使用VrPanoramaView的Acitivity的intent-filter节点添加过滤分类:
com.google.intent.category.CARDBOARD : 兼容Cardboard纸盒

1
2
3
4
5
<activity android:name=".VrPanoramaActivity" >
<intent-filter>
<category android:name="com.google.intent.category.CARDBOARD" />
</intent-filter>
</activity>

3.布局文件

只需在布局中添加一个控件

1
2
3
4
5
6
<com.google.vr.sdk.widgets.pano.VrPanoramaView
android:id="@+id/pano_view"
android:layout_margin="5dip"
android:layout_width="match_parent"
android:scrollbars="@null"
android:layout_height="250dip" />

4.加载全景图

A.初始化控件

1
VrPanoramaView vrPanView = (VrPanoramaView) findViewById(R.id.vr_pan_view);

B.读取图片

我们提前将一张选择好的全景图放在assets目录中,aa.jpg,将图片转为bitmap

1
2
3
4
5
6
7
8
/**获取assets中的图片,转为流**/
InputStream open = null;
try {
open = getAssets().open("aa.jpg");
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(open);

C.设置VrPanoramaView.Options

1
2
3
/**VrPanoramaView.Options 设置**/
VrPanoramaView.Options options = new VrPanoramaView.Options();
options.inputType = VrPanoramaView.Options.TYPE_MONO;

VrPanoramaView.Options有两种类型:

  • TYPE_MONO 360度单图(2:1纵横比)
    图像被预期以覆盖沿着其水平轴360度,而垂直范围是根据图像的宽高比来计算。例如,如果一个1000x250像素的图像,给出所述全景将覆盖360x90度与垂直范围是-45至+45度。

  • TYPE_STEREO_OVER_UNDER 立体图(1:1纵横比)
    包含两个大小相等的投影 全景图垂直叠加。顶部图像被显示给左眼、底部图像被显示给右眼。图像将覆盖沿水平轴360度,而垂直范围是根据图像的宽高比来计算。例如,如果一个1000x500像素的图像中给出(即1000x250像素每个眼睛),全景将覆盖360x90度与垂直范围是-45至+45度。

D.加载全景图

1
vrPanView.loadImageFromBitmap(bitmap, options);

E.设置加载监听VrPanoramaEventListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**设置加载图片监听**/
vrPanView.setEventListener(new VrPanoramaEventListener() {
/**
* 显示模式改变回调
* 1.默认
* 2.全屏模式
* 3.VR观看模式,即横屏分屏模式
*/
@Override
public void onDisplayModeChanged(int newDisplayMode) {
super.onDisplayModeChanged(newDisplayMode);
Log.d(TAG, "onDisplayModeChanged()->newDisplayMode=" + newDisplayMode);
}
/**
* 加载VR图片失败回调
*/
@Override
public void onLoadError(String errorMessage) {
super.onLoadError(errorMessage);
Log.d(TAG, "onLoadError()->errorMessage=" + errorMessage);
}
/**
* 加载VR图片成功回调
*/
@Override
public void onLoadSuccess() {
super.onLoadSuccess();
Log.d(TAG, "onLoadSuccess->图片加载成功");
}
/**
* 点击VR图片回调
*/
@Override
public void onClick() {
super.onClick();
Log.d(TAG, "onClick()");
}
});

F.在onPause、onResume、onDestroy中做出相应处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
protected void onPause() {
super.onPause();
vrPanView.pauseRendering();//暂停3D渲染和跟踪
}
@Override
protected void onResume() {
super.onResume();
vrPanView.resumeRendering();//恢复3D渲染和跟踪
}
@Override
protected void onDestroy() {
vrPanView.shutdown();//关闭渲染下并释放相关的内存
super.onDestroy();
}

G.一些其他方法

//是否隐藏左下角信息的按钮
vrPanView.setInfoButtonEnabled(boolean enabled);

//是否隐藏全屏按钮
vrPanView.setFullscreenButtonEnabled(boolean enabled);

未完,如果遇到新的继续添加

最后附上完整代码:

VrPanoramaActivity.java

0%