在我的应用程序中使用Street View VR

时间:2018-08-21 16:45:46

标签: java android virtual-reality google-street-view

我正在使用Android Studio,并试图在VR中显示一些选定的街景路径。我已经可以很好地运行街景了,现在我正试图在VR中显示它。

我将com.google.vr.sdk.widgets.pano.VrPanoramaView放在了布局中,并在我的类的onCreate内,通过VrPanoramaView将其引用到findViewById变量中。现在,我试图显示一个图像,该图像调用我在此类中定义的方法loadPanoImage。此方法从存储中加载图像并通过loadImageFromBitmap显示。

问题在于,即使我已按照指南进行操作,但已完成显示的所有操作,它仍然无法显示任何内容。我什至尝试在代码的不同部分调用它(在执行任何其他操作之前,在单击按钮之前,在显示街景视图之前和之后),但我不明白为什么它不起作用以及如何能够用它来显示从StreetView拍摄的图像(我不知道我是否能够做到这一点,还是应该下载并将它们放入存储设备中。)

我将部分代码供参考:

public class VrExperience extends FragmentActivity {

    Button buttonCitta;
    Button buttonMare;
    Button buttonMontagna;
    TextView titleTextView;

    // George St, Sydney
    private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
    // LatLng with no panorama
    private static final LatLng INVALID = new LatLng(-45.125783, 151.276417);

    //VrPanoramaView is inserted in the layout
    private VrPanoramaView panoWidgetView;
    //StreetViewPanorama is another class in my project which shows Street View
    private StreetViewPanorama mStreetViewPanorama;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vrexperiences);

        panoWidgetView = (VrPanoramaView) findViewById(R.id.pano_view);
        panoWidgetView.setEventListener(new VrPanoramaEventListener());

        //download image and show it, but it doesn't show anything
        loadPanoImage();

        titleTextView = (TextView) findViewById(R.id.titleTextView);

        buttonCitta = (Button) findViewById(R.id.buttonCitta);
        buttonCitta.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (!checkReady()) {
                    return;
                }
                titleTextView.setVisibility(View.GONE);
                buttonCitta.setVisibility(View.GONE);
                buttonMare.setVisibility(View.GONE);
                buttonMontagna.setVisibility(View.GONE);
                loadPanoImage();    //it doesn't show anything
                mStreetViewPanorama.setPosition(SYDNEY);
                loadPanoImage();    //it doesn't show anything
            }
        }};

        //code for buttonMontagna and buttonMare as well, it's identical

        SupportStreetViewPanoramaFragment streetViewPanoramaFragment =
            (SupportStreetViewPanoramaFragment)
                    getSupportFragmentManager().findFragmentById(R.id.streetviewpanorama);

        streetViewPanoramaFragment.getStreetViewPanoramaAsync(
                new OnStreetViewPanoramaReadyCallback() {
                    @Override
                    public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
                        mStreetViewPanorama = panorama;
                        // Only set the panorama to INVALID on startup (when no panoramas have been
                        // loaded which is when the savedInstanceState is null).
                        if (savedInstanceState == null) {
                            mStreetViewPanorama.setPosition(INVALID);
                        }
                    }
                });

    }

    /**
     * When the panorama is not ready the PanoramaView cannot be used. This should be called on
     * all entry points that call methods on the Panorama API.
     */
    private boolean checkReady() {
        if (mStreetViewPanorama == null)
            return false;

        return true;
    }

    /**
     * Called when the Animate To Invalid button is clicked.
     */
    public void onGoToInvalid(View view) {
        if (!checkReady()) {
            return;
        }
        mStreetViewPanorama.setPosition(INVALID);
    }

    //retrieves image from the assets folder and loads it into the VrPanoramaView
    private void loadPanoImage() {
        VrPanoramaView.Options options = new VrPanoramaView.Options();
        InputStream inputStream = null;
        AssetManager assetManager = getAssets();

        try {
            inputStream = assetManager.open("demo2.jpg");
            options.inputType = VrPanoramaView.Options.TYPE_MONO;
            panoWidgetView.loadImageFromBitmap(
                BitmapFactory.decodeStream(inputStream), options
            );
            inputStream.close();
        } catch (IOException e) {
            Log.e("Fail", "Exception in loadPanoImage" + e.getMessage());
        }
    }

    @Override
    protected void onPause() {
        panoWidgetView.pauseRendering();
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        panoWidgetView.resumeRendering();
    }

    @Override
    protected void onDestroy() {
        panoWidgetView.shutdown();
        super.onDestroy();
    }

}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vrExperienceActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.vr.sdk.widgets.pano.VrPanoramaView
        android:id="@+id/pano_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:layout_weight="5"
        android:scrollbars="none" />

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:text="VR Experience"
        android:textAlignment="center"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:textColor="#0000F0"
        android:visibility="visible" />

    <Button
        android:id="@+id/buttonCitta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Città" />

    <fragment
        class="com.google.android.gms.maps.SupportStreetViewPanoramaFragment"
        android:id="@+id/streetviewpanorama"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

编辑:@LucioB
a)这些是我尝试调用loadPanoImage的地方,但它们均未显示任何内容。就像调用该方法没有发生任何事情一样,程序继续执行其他任务。我希望在单击按钮时直接在VR中显示图像,或者如果无法在街景视图模式下添加经典硬纸板按钮以传递到VR视图,则为
。 b)我的意思是代码没有按照我的预期做。我以为,一旦在布局中创建VrPanoramaView并将其用于通过.loadImageFromBitmap显示图像,它就会显示我从资产加载的图像(我有一个图像保存在虚拟SD上),能够针对单个图像做到这一点,我会找到一种针对整个路径进行成像的方法。
该代码没有给出任何异常,我认为我犯了逻辑错误,或者我不了解VR api的工作原理。

编辑:我发现Java代码正在运行,问题出在布局中,该布局不允许看到VrPanoramaView,因为它被StreetViewPanorama遮盖了

0 个答案:

没有答案