当前位置:   article > 正文

Android实现VR查看图片_android com.google.vr:sdk-panowidget

android com.google.vr:sdk-panowidget

前言:

         最近没事逛懂车帝, 发现有VR全景图片看车这个功能, 于是查资料,自己也写一个。

方法一:利用谷歌提供的vr库

1、依赖vr库

implementation 'com.google.vr:sdk-panowidget:1.80.0'

2、布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".VrLocationActivity">
  7. <com.google.vr.sdk.widgets.pano.VrPanoramaView
  8. android:id="@+id/vr_view"
  9. android:layout_width="match_parent"
  10. android:layout_height="250dp"
  11. android:layout_centerInParent="true"/>
  12. </RelativeLayout>

3、找到控件,设置图片, 图片由美工提供

  1. public class VrLocationActivity extends AppCompatActivity {
  2. int type = -1;
  3. Bitmap mBitmap;
  4. VrPanoramaView panoWidgetView;
  5. VrPanoramaView.Options paNormalOptions;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_vr_location);
  10. type = getIntent().getIntExtra("type", 0);
  11. panoWidgetView = findViewById(R.id.vr_view);
  12. panoWidgetView.setEventListener(new ActivityEventListener());
  13. paNormalOptions = new VrPanoramaView.Options();
  14. // mVrPanoramaView.setFullscreenButtonEnabled (false); //隐藏全屏模式按钮
  15. panoWidgetView.setInfoButtonEnabled(false); //设置隐藏最左边信息的按钮
  16. panoWidgetView.setStereoModeButtonEnabled(false); //设置隐藏立体模型的按钮
  17. panoWidgetView.setEventListener(new ActivityEventListener()); //设置监听
  18. if (type == 1) {
  19. /**
  20. 包含两个大小相等的投影 全景图垂直叠加。顶部图像被显示给左眼、底部图像被显示给右眼。图像将覆盖沿水平轴360度,而垂直范围是根据图像的宽高比来计算
  21. */
  22. paNormalOptions.inputType = VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER;
  23. mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.andes);
  24. }
  25. else if (type == 2) {
  26. paNormalOptions.inputType = VrPanoramaView.Options.TYPE_MONO;
  27. mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.andes1);
  28. }
  29. else if (type == 3) {
  30. /**
  31. * 图像被预期以覆盖沿着其水平轴360度,而垂直范围是根据图像的宽高比来计算。例如,如果一个1000x250像素的图像,给出所述全景将覆盖360x90度与垂直范围是-45至+45度
  32. */
  33. paNormalOptions.inputType = VrPanoramaView.Options.TYPE_MONO;
  34. mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.andes2);
  35. }
  36. panoWidgetView.loadImageFromBitmap(mBitmap, paNormalOptions);
  37. }
  38. private class ActivityEventListener extends VrPanoramaEventListener {
  39. /**
  40. * Called by pano widget on the UI thread when it's done loading the image.
  41. */
  42. @Override
  43. public void onLoadSuccess() {
  44. Log.e("dongjie", "图片加载成功了 ");
  45. }
  46. /**
  47. * Called by pano widget on the UI thread on any asynchronous error.
  48. */
  49. @Override
  50. public void onLoadError(String errorMessage) {
  51. Log.d("dongjie", "图片加载: " + errorMessage);
  52. }
  53. @Override
  54. public void onDisplayModeChanged(int newDisplayMode) {
  55. //改变显示模式时候(全屏模式和纸板模式)
  56. super.onDisplayModeChanged(newDisplayMode);
  57. Log.i("dongjie", "onDisplayModeChanged------------>" + newDisplayMode);
  58. }
  59. @Override
  60. public void onClick() {
  61. super.onClick();
  62. //点击VrPanoramaView
  63. Log.i("dongjie", "onClick------------>");
  64. }
  65. }
  66. @Override
  67. protected void onPause() {
  68. panoWidgetView.pauseRendering();
  69. super.onPause();
  70. }
  71. @Override
  72. protected void onResume() {
  73. super.onResume();
  74. panoWidgetView.resumeRendering();
  75. }
  76. @Override
  77. protected void onDestroy() {
  78. // Destroy the widget and free memory.
  79. panoWidgetView.shutdown();
  80. super.onDestroy();
  81. }
  82. }

二、Three.js      WebView混合开发

1、集成x5内核, 这里建议使用x5内核, 加载效率会快好多,集成过程在此省略

2、xml文件

  1. <com.tencent.smtt.sdk.WebView
  2. android:id="@+id/web_view"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" />

3、下载Three.js

下载地址:https://threejs.org/

4、编写HTML代码: 这一步有困难的话可以交给web前端去做

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Photo Sphere Viewer</title>
  6. <meta name="viewport" content="initial-scale=1.0" />
  7. <script src="js/three.min.js"></script>
  8. <script src="js/photo-sphere-viewer.min.js"></script>
  9. <style>
  10. html, body {
  11. margin: 0;
  12. width: 100%;
  13. height: 100%;
  14. overflow: hidden;
  15. }
  16. #container {
  17. width: 100%;
  18. height: 100%;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="container"></div>
  24. <script>
  25. var div = document.getElementById('container');
  26. var PSV = new PhotoSphereViewer({
  27. panorama: 'https://gw.alicdn.com/tfs/TB1WSInRFXXXXXlXpXXXXXXXXXX-1200-600.jpg',
  28. container: div,
  29. time_anim: false,
  30. navbar: true,
  31. navbar_style: {
  32. backgroundColor: 'rgba(58, 67, 77, 0.7)'
  33. },
  34. });
  35. </script>
  36. </body>
  37. </html>

5、编写Activity代码, 用webview加载上面html代码, 一般是web前端提供一个链接

  1. public class WebViewActivity extends AppCompatActivity {
  2. private WebView webview;
  3. private String url = "file:///android_asset/vr.html";
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_web_view);
  8. webview = (WebView) findViewById(R.id.web_view);
  9. webview.loadUrl(url);
  10. WebSettings webSettings = tencent_webview.getSettings();
  11. webSettings.setJavaScriptEnabled(true);
  12. tencent_webview.setWebViewClient(new WebViewClient() {
  13. @Override
  14. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  15. return true;
  16. }
  17. });
  18. }
  19. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/530943
推荐阅读
相关标签