如何动态加载VrPanoramaView中的不同全景图像

时间:2018-03-01 05:08:19

标签: android android-studio virtual-reality google-cardboard

可以在VrPanoramaView中加载不同的全景图像,例如像这张图片中的“Google Cardboad相机应用程序”enter image description here。我正在使用VR制作一个旅游景点,并且很难使用非用户友好的应用程序,因为您需要在VR BOX中再次移除手机以选择另一个全景图像。

TW1

package com.example.computer.vratdragii;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.vr.sdk.widgets.pano.VrPanoramaView;

public class TW1 extends Fragment {

    private VrPanoramaView panoWidgetView;
    private ImageLoaderTask backgroundImageLoaderTask;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.activity_tw1, container,false);
        panoWidgetView = (VrPanoramaView) v.findViewById(R.id.pano_view);
        return v;

    }

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

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

    @Override
    public void onDestroy() {

        panoWidgetView.shutdown();
        super.onDestroy();
    }

    private synchronized void loadPanoImage() {
        ImageLoaderTask task = backgroundImageLoaderTask;
        if (task != null && !task.isCancelled()) {
            task.cancel(true);
        }

        VrPanoramaView.Options viewOptions = new VrPanoramaView.Options();
        viewOptions.inputType = VrPanoramaView.Options.TYPE_STEREO_OVER_UNDER;


        String panoImageName = "tw1.jpg";


        task = new ImageLoaderTask(panoWidgetView, viewOptions, panoImageName);
        task.execute(getActivity().getAssets());
        backgroundImageLoaderTask = task;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadPanoImage();

    }



}

TW1.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:background="#ffffff">

        <TextView
            android:id="@+id/welcome_title"
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:layout_marginStart="@dimen/activity_horizontal_margin"
            android:layout_weight=".2"
            android:text="@string/chocolate_hills"
            android:textAlignment="center"
            android:textStyle="bold"
            android:textAllCaps="true"
            android:textAppearance="?android:attr/textAppearanceLarge" />

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:layout_marginStart="@dimen/activity_horizontal_margin"
            android:layout_weight=".2"
            android:text="@string/chocolate_hills_location"
            android:textStyle="bold"
            android:textSize="12sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/activity_horizontal_margin"
            android:layout_weight="1"
            android:text="@string/chocolate_hills_desc"
            android:textColor="#000000"/>

    </LinearLayout>

ImageLoaderTask

package com.example.computer.vratdragii;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.os.AsyncTask;

import com.google.vr.sdk.widgets.pano.VrPanoramaView;
import java.lang.ref.WeakReference;

import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

public class ImageLoaderTask extends AsyncTask<AssetManager, Void, Bitmap> {

    private static final String TAG = "ImageLoaderTask";
    private final String assetName;
    private final WeakReference<VrPanoramaView> viewReference;
    private final VrPanoramaView.Options viewOptions;

    private static WeakReference<Bitmap> lastBitmap = new WeakReference<>(null);
    private static String lastName;

    @Override
    protected Bitmap doInBackground(AssetManager... params) {
        AssetManager assetManager = params[0];

        if (assetName.equals(lastName) && lastBitmap.get() != null) {
            return lastBitmap.get();
        }

        try(InputStream istr = assetManager.open(assetName)) {
            Bitmap b = BitmapFactory.decodeStream(istr);
            lastBitmap = new WeakReference<>(b);
            lastName = assetName;
            return b;
        } catch (IOException e) {
            Log.e(TAG, "Could not decode default bitmap: " + e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        final VrPanoramaView vw = viewReference.get();
        if (vw != null && bitmap != null) {
            vw.loadImageFromBitmap(bitmap, viewOptions);
        }
    }

    public ImageLoaderTask(VrPanoramaView view, VrPanoramaView.Options viewOptions, String assetName) {
        viewReference = new WeakReference<>(view);
        this.viewOptions = viewOptions;
        this.assetName = assetName;
    }

}

0 个答案:

没有答案