2017-02-10 76 views
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"); 
} 
} 

目前我只看到了标准地图,上面有一个标记,还没有矩形:(

GMaps

回答

1

您需要测量所需视图的大小。你只是通过你正在收到,可能是0.

另一种选择是设置一个LayoutParams与MATCH_PARENT,但我不知道这是否会工作。

你想要做的是先进的,你应该在尝试之前阅读一些教程。

这里您有更多的信息:https://developer.android.com/training/custom-views/custom-drawing.html

+0

谢谢你的快速答案!我看了一下,做了几次尝试。也没有工作。我要添加自定义视图吗?或者是有逻辑错误。我没有发现很多关于它的文章。另外我编辑了我的问题:) – genaray

+0

您仍然使用收到的参数调用super.onMeasure。你不需要调用它。 – jonathanrz

+0

所以我删除它并再次测试,仍然没有: - /。任何其他想法? – genaray