Google VR 使用详解

VR全景图片显示(本地)

1.搭建VR图片开发的环境

  • 1.1.导入从github搜索下载的google vr sdk 里面的引用库 common,commonwidget,panowidget(全景图片控件库)
  • 1.2.当前这三个库里面缺少序列相关的api,容易引用类找不到,未定义异常
  • 1.3.依赖三个库
    //出现类未定义错误的缺少库
    compile 'com.google.protobuf.nano:protobuf-javanano:3.0.0-alpha-7'
    compile project(':common')
    compile project(':commonwidget')
    compile project(':panowidget')
  • 1.4.准备全景图片用来测试代码 放在assets目录下面,例assets/a.jpg
  • 1.5.对当前应用进行内存设置,希望应用可使用最大内存 避免OOM
 <application android:largeHeap="true"

2.加载全景图片到内存中成为Bitmap(bitmap是图片在内存中的表示对象),展示在全景图片控件

public class MainActivity extends AppCompatActivity {
   

    private VrPanoramaView vrPanoramaView;
    private ImageTask imageTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //步骤二。加载全景图片到内存中成为Bitmap(bitmap是图片在内存中的表示对象),展示在全景图片控件
        //2.1.布局全景图片控件
        //2.2.查找控件
        vrPanoramaView = (VrPanoramaView) findViewById(R.id.vr_pano);
        //2.3.因为图片比较大,希望在异步线程里面加载,防止占用主线程
        imageTask = new ImageTask();
        imageTask.execute("a.jpg");
    }
    private class ImageTask extends AsyncTask<String,Void,Bitmap>
    {
   
        //2.4.读取资产目录下面的a.jpg图片
        @Override
        protected Bitmap doInBackground(String... params) {
            try {
                //2.4.1.获取文件流
                InputStream inputStream = getAssets().open(params[0]);
                //2.4.2.使用BitmapFactory可以将inputStream,file,byte[]-->Bitmap
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        //2.5.在主线程展示位图
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if(bitmap!=null)
            {
                //加载bitmap到vr图片控件  参1.Bitmap
                VrPanoramaView.Options options=new VrPanoramaView.Options() ;
                //TYPE_STEREO_OVER_UNDER :立体图片:上半画面显示在左眼,下半画面显示在右眼
                //TYPE_MONO :普通图片
                options.inputType= VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER;
               //3.1.监听加载过程
                VrPanoramaEventListener listener=new VrPanoramaEventListener(){
                    //3.1.1.加载bitmap异常
                    @Override
                    public void onLoadError(String errorMessage) {
                        super.onLoadError(errorMessage);
                        Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
                    }
                    //3.1.2.加载bitmap成功
                    @Override
                    public void onLoadSuccess() {
                        super.onLoadSuccess();
                        Toast.makeText(MainActivity.this, "进入VR图片显示...", Toast.LENGTH_SHORT).show();
                    }
                };
                vrPanoramaView.setEventListener(listener);
                vrPanoramaView.loadImageFromBitmap(bitmap,options);
            }
        }
    }

3.处理全景控件展示细节


    //步骤三。处理全景控件展示细节
    //3.2.页面停到后台,暂停画面显示
    @Override
    protected void onPause() {
        super.onPause();
        if (vrPanoramaView != null) {
            vrPanoramaView.pauseRendering();
        }
    }
    //3.3.页面回到屏幕,再继续显示
    @Override
    protected void onResume() {
        super.onResume();
        if (vrPanoramaView != 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值