GLSurfaceView not appearing over MapView

别说谁变了你拦得住时间么 提交于 2019-12-02 04:22:24

I have found a solution to my problem. Just not sure if there is a better way of doing it.

Basically, all I did was split up the two views (MapView and GLSurfaceView) into two different Fragments. Then used an Activity class to call the two fragments. The XML file had the fragments cover the same area. Below is my updated code.

GoogleMapsActivity:

public class GoogleMapsActivity extends FragmentActivity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    final Context context = this;

    return super.onOptionsItemSelected(item);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.DarkTheme);

    if(SettingsActivity.mUnits){
        setContentView(R.layout.google_maps_tracker_metric);
    } else {
        setContentView(R.layout.google_maps_tracker);
    }
}

@Override
protected void onResume(){
    super.onResume();

    // Get both fragments shown on the same screen
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    GoogleMapsTracker fragment;
    fragment = GoogleMapsTracker.newInstance();

    GoogleMapsOGLES fragmentOGL;
    fragmentOGL = GoogleMapsOGLES.newInstance();

    fragmentTransaction.add(R.id.map, fragment);
    fragmentTransaction.add(R.id.gl, fragmentOGL);
    fragmentTransaction.commit();
}

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

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

}

GoogleMapsTracker:

public class GoogleMapsTracker extends Fragment implements OnMapReadyCallback {

@Override
public void onMapReady(GoogleMap map){

}

MapView mapView;
GoogleMap map;

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 */
public static GoogleMapsTracker newInstance() {
    GoogleMapsTracker fragment = new GoogleMapsTracker();
    fragment.setRetainInstance(true);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mapView = (MapView) getActivity().findViewById(R.id.maps);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v;

    v = inflater.inflate(R.layout.fragment_google_maps, container, false);

    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) v.findViewById(R.id.maps);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();

    map.getUiSettings().setMyLocationButtonEnabled(true);
    map.getUiSettings().setZoomControlsEnabled(true);
    map.getUiSettings().setRotateGesturesEnabled(true);

    map.setMyLocationEnabled(true);

    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
    MapsInitializer.initialize(this.getActivity());

    // Updates the location and zoom of the MapView
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(49.025656, -125.035473), 18);
    map.animateCamera(cameraUpdate);

    return v;
}

GoogleMapsOGLES:

public class GoogleMapsOGLES extends Fragment {
GLSurfaceView mGLSurfaceView;

private boolean rendererSet = false;

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 */
public static GoogleMapsOGLES newInstance() {
    GoogleMapsOGLES fragment = new GoogleMapsOGLES();
    fragment.setRetainInstance(true);
    return fragment;
}

public GoogleMapsOGLES() {
    // Do nothing
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGLSurfaceView = new GLSurfaceView(getActivity());

    // Check if the system supports OpenGL ES 2.0
    final ActivityManager am = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    final ConfigurationInfo cInfo = am.getDeviceConfigurationInfo();
    final boolean supportsEs2 = cInfo.reqGlEsVersion >= 0x20000;

    if(supportsEs2){
        // Request an OpenGL ES 2.0 compatible context
        mGLSurfaceView.setEGLContextClientVersion(2);

        // Set the renderer to our demo renderer, defined below
        mGLSurfaceView.setRenderer(new GoogleMapsRenderer(getActivity()));
        rendererSet = true;
    } else {
        // This is where you could create an OpenGL ES1.x compatible
        // renderer if you wanted to support both ES 1 and ES 2
        Log.d("Does Not Support ES 2.0", "Does NOT Support ES 2.0");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v;

    v = inflater.inflate(R.layout.fragment_maps_ogl, container, false);

    mGLSurfaceView = (GLSurfaceView) v.findViewById(R.id.ogl);
    mGLSurfaceView.setEGLContextClientVersion(2);
    GoogleMapsRenderer renderer = new GoogleMapsRenderer(getActivity());

    mGLSurfaceView.setZOrderOnTop(true);
    mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    mGLSurfaceView.setRenderer(renderer);
    rendererSet = true;
    mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

    return v;
}

google_maps_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.tracker.GoogleMapsTracker"
    class="com.example.tracker.GoogleMapsTracker"/>

<fragment
    android:id="@+id/gl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.tracker.GoogleMapsOGLES"
    class="com.example.tracker.GoogleMapsOGLES"/>

</RelativeLayout>

The fragments within the xml file had their own xml files that just initialized the MapView and GLSurfaceView.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!