gpt4 book ai didi

java - Android/Java/向 GoogleMaps FragmentActivity 添加自定义 View

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:59 25 4
gpt4 key购买 nike

晚上好!

我目前正在尝试学习 Android 的 Java,并且我已经制作了一些“简单”的应用程序。这次我想创造一些更复杂的东西。我想尝试在谷歌地图上的额外图层中绘制 Sprite 或动画。我已经知道我需要自定义 View ,但我不知道如何将该自定义 View 添加到 GoogleMaps FragmentActivity。

这是我的 GoogleMaps Activity :D:

public class GoogleMaps extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
public Canvas c;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

}

/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// 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"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

这是我的自定义 View :

public class SpriteLayer extends View {

Paint paint = new Paint();

public SpriteLayer(Context context) {
super(context);


}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
public void onDraw(Canvas canvas) {

paint.setColor(Color.GREEN);
Rect rect = new Rect(20, 56, 200, 112);
canvas.drawRect(rect, paint );
}
}

那么如何将此 CustomView 添加到 GoogleMaps Fragment Activity 中以调用 onDraw 方法?

感谢您的帮助:)

最佳答案

Android SDK已引入API为屏幕的特定部分创建 Controller ,可以轻松地在多个屏幕上重复使用。

Libgdx 还可以用作 fragment 内更大屏幕的一部分。创建一个 Libgdx fragment ,子类化 AndroidFragmentApplication 并通过以下初始化实现 onCreateView():

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return initializeForView(new MyGdxGame());
}

该代码取决于 -android 项目的一些其他更改:

  1. 如果您尚未添加 Android V4 支持库,请将其添加到 -android 项目及其构建路径中。这是为了稍后扩展 FragmentActivity 所必需的
  2. 更改 AndroidLauncher Activity 以扩展 FragmentActivity,而不是 AndroidApplication
  3. 在 AndroidLauncher Activity 上实现 AndroidFragmentApplication.Callbacks
  4. 创建一个扩展 AndroidFragmentApplication 的类,它是 Libgdx 的 Fragment 实现。
  5. 在Fragment的onCreateView方法中添加initializeForView()代码。
  6. 最后,将 AndroidLauncher Activity 内容替换为 Libgdx fragment 。


了解有关 libgdx 中 fragment 的更多信息:

https://github.com/libgdx/libgdx/wiki/Starter-classes-%26-configuration#fragment-based-libgdx

关于java - Android/Java/向 GoogleMaps FragmentActivity 添加自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42144672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com