Android 谷歌推荐的VR实现方式(分享)
时间:2020-12-02 13:13:54|栏目:Android代码|点击: 次
谷歌有专门的SDK来完成VR,我这次以一个全景图片的例子来说一下这个SDK实现VR的基本过程,首先全景图片就是百度地图里的那样,能够看到周围环境360的图片。
添加依赖
compile 'com.google.vr:sdk-panowidget:1.80.0'
添加权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.GET_TASKS"/>
实现代码
public class GoogleVRActivity extends AppCompatActivity { private VrPanoramaView mVrPanoramaView; private VrPanoramaView.Options paNormalOptions; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_google_vr); initVrPaNormalView(); } @Override protected void onPause() { super.onPause(); mVrPanoramaView.pauseRendering(); } @Override protected void onResume() { super.onResume(); mVrPanoramaView.resumeRendering(); } @Override protected void onDestroy() { // Destroy the widget and free memory. super.onDestroy(); mVrPanoramaView.shutdown(); } //初始化VR图片 private void initVrPaNormalView() { mVrPanoramaView = (VrPanoramaView) findViewById(R.id.mVrPanoramaView); paNormalOptions = new VrPanoramaView.Options(); paNormalOptions.inputType = VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER; // mVrPanoramaView.setFullscreenButtonEnabled (false); //隐藏全屏模式按钮 mVrPanoramaView.setInfoButtonEnabled(false); //设置隐藏最左边信息的按钮 mVrPanoramaView.setStereoModeButtonEnabled(false); //设置隐藏立体模型的按钮 mVrPanoramaView.setEventListener(new ActivityEventListener()); //设置监听 //加载本地的图片源 mVrPanoramaView.loadImageFromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.andes), paNormalOptions); //设置网络图片源 // panoWidgetView.loadImageFromByteArray(); } private class ActivityEventListener extends VrPanoramaEventListener { @Override public void onLoadSuccess() {//图片加载成功 } @Override public void onLoadError(String errorMessage) {//图片加载失败 } @Override public void onClick() {//当我们点击了VrPanoramaView 时候触发 super.onClick(); } @Override public void onDisplayModeChanged(int newDisplayMode) { //改变显示模式时候出发(全屏模式和纸板模式) super.onDisplayModeChanged(newDisplayMode); } } }
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dip" android:id="@+id/main_layout" android:orientation="vertical" > <TextView style="@style/ContentText" android:id="@+id/title" android:textSize="@dimen/title_text_size" android:textStyle="bold" android:textColor="@color/colorAccent" android:text="第二种谷歌官方VR演示" /> <TextView style="@style/ContentText" android:id="@+id/subtitle" android:textColor="@color/colorAccent" android:textSize="@dimen/caption_text_size" android:text="谷歌纸壳子demo" /> <TextView style="@style/ContentText" android:id="@+id/paragraph1" android:textColor="@color/colorAccent" android:textSize="@dimen/paragragh_text_size" android:text="鱼和熊掌不可兼得" /> <com.google.vr.sdk.widgets.pano.VrPanoramaView android:id="@+id/mVrPanoramaView" android:layout_width="match_parent" android:layout_height="250dip"/> </LinearLayout> </ScrollView>
VrPanoramaView这个类和Activity有着类似的生命周期,全屏显示是它自己设置的,这个VR 的SDK完成度很高,可以多去参考。
栏 目:Android代码
本文地址:http://www.codeinn.net/misctech/28717.html