1
我試圖畫一些東西在自定義視圖中進行測試,該視圖添加到GoogleMaps FragmentActivity中。但不知何故,當我在模擬器中運行它時,它看起來像我的CustomView不是繪圖。我嘗試了一個新的RelativeLayout和一個MapLayout。有任何想法嗎 ?我做錯了什麼 ?Android/Java - 自定義在Google地圖上查看FragmentActivity未顯示
public class GoogleMaps extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@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);
//RelativeLayout relativeLayout = new RelativeLayout(this);
//relativeLayout.addView(new SpriteLayer(this));
MapView map = new MapView(this);
map.addView(new SpriteLayer(this));
}
@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));
}
}
這就是我的CustomView代碼:
繼承人我主要的GoogleMaps FragmentActivity碼,從Android的模板自動生成
public class SpriteLayer extends View {
Paint paint = new Paint();
public SpriteLayer(Context context) {
super(context);
setWillNotDraw(false);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int desiredWidth = 100;
int desiredHeight = 100;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
//Measure Width
if (widthMode == MeasureSpec.EXACTLY) {
//Must be this size
width = widthSize;
} else if (widthMode == MeasureSpec.AT_MOST) {
//Can't be bigger than...
width = Math.min(desiredWidth, widthSize);
} else {
//Be whatever you want
width = desiredWidth;
}
//Measure Height
if (heightMode == MeasureSpec.EXACTLY) {
//Must be this size
height = heightSize;
} else if (heightMode == MeasureSpec.AT_MOST) {
//Can't be bigger than...
height = Math.min(desiredHeight, heightSize);
} else {
//Be whatever you want
height = desiredHeight;
}
//MUST CALL THIS
setMeasuredDimension(width, height);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
Rect rect = new Rect(20, 56, 200, 112);
canvas.drawRect(rect, paint);
Log.println(Log.ERROR,"Test ","One");
}
}
目前我只看到了標準地圖,上面有一個標記,還沒有矩形:(
謝謝你的快速答案!我看了一下,做了幾次嘗試。也沒有工作。我要添加自定義視圖嗎?或者是有邏輯錯誤。我沒有發現很多關於它的文章。另外我編輯了我的問題:) – genaray
您仍然使用收到的參數調用super.onMeasure。你不需要調用它。 – jonathanrz
所以我刪除它並再次測試,仍然沒有: - /。任何其他想法? – genaray