所以我有一个问题......我想在我的GoogleMaps FragmentActivity中添加一个SpriteLayer,用于绘制和显示Menus,Enemys等类似内容。
首先,我创建我的GoogleMaps FragmentActivity:
public class GoogleMaps extends FragmentActivity implements OnMapReadyCallback,AndroidFragmentApplication.Callbacks,GoogleMap.OnMapLoadedCallback {
private GoogleMap mMap;
View mapView;
SupportMapFragment mapFragment;
FutureTask<PlayFabErrors.PlayFabResult<PlayFabClientModels.LoginResult>> loginTask = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
GameFragment fragment = new GameFragment();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.add(R.id.map,fragment);
trans.commit();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.getUiSettings().setZoomGesturesEnabled(false);
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// Move the camera instantly to location with a zoom of 15.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 20));
// Zoom in, animating the camera.
mMap.animateCamera(CameraUpdateFactory.zoomTo(17.5f), 2000, null);
mapView = mapFragment.getView();
mapView.post(new Runnable() {
@Override
public void run() {
int width = mapView.getMeasuredWidth();
int height = mapView.getMeasuredHeight();
String widthAndHeight = width + " " + height;
Log.println(Log.ERROR,"Width and height :",widthAndHeight);
}
});
}
@Override
public void onMapLoaded() {
}
public static class GameFragment extends AndroidFragmentApplication
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return initializeForView(new Main());
}
}
@Override
public void exit() {
}
}
正如你所看到的,我称之为在onCreate方法中添加片段:
GameFragment fragment = new GameFragment();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.add(R.id.map,fragment);
trans.commit();
这应创建一个新的静态GameFragment,它在GoogleMaps FragmentActivity中定义:
public static class GameFragment extends AndroidFragmentApplication
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return initializeForView(new Main());
}
}
此时它将从Libgdx返回Main ApplicationAdapter类作为片段。哪个已添加到R.id.map
,GoogleMaps的地图布局。
以下是Main类的内容:
public class Main extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
}
@Override
public void render () {
Gdx.gl.glClearColor( 0, 0, 0, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
batch.begin();
batch.draw(img, 0, 0);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
}
}
它只是画一幅画。
当我现在启动应用程序时,行为非常奇怪。首先,Libgdx碎片弹出1秒钟。在那之后它就“消失了”。然后谷歌地图被加载,但触摸事件似乎被打破,因为我不能再缩放/移动地图。
那我做错了什么?
感谢您的帮助!我很高兴能得到的一切帮助:D!