Android/Java - Custom View on GoogleMaps FragmentActivity isnt shown

[亡魂溺海] 提交于 2020-01-11 12:50:33

问题


Im trying to draw some Stuff for testing in an Custom View, which is added to the GoogleMaps FragmentActivity. But somehow, when i run it in the emulator, it seems like my CustomView isnt drawing.I tried it with a new RelativeLayout and an MapLayout. Any ideas ? What did i do wrong ?

Heres my Main GoogleMaps FragmentActivity code, auto generated from Android templates :

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));
}
}

Thats my CustomView Code :

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");
}
}

Currently i only see the standard map with an marker on it, still no Rectangle :(


回答1:


You need to measure the desired size of your view. You are just passing what you are receiving, which probably is 0.

Another option is to set a LayoutParams with MATCH_PARENT, but I'm not sure if this will work.

What you are trying to do is advanced, you should read some tutorials before trying it.

Here you have more info: https://developer.android.com/training/custom-views/custom-drawing.html



来源:https://stackoverflow.com/questions/42163321/android-java-custom-view-on-googlemaps-fragmentactivity-isnt-shown

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