我在这里扩展一个包含谷歌地图的SupportMapFragment,我根据How to handle onTouch event for map in Google Map API v2?将我的谷歌地图放在一个frameLayout上。调用Ondraw()方法,但问题是矩形从未显示过,即使我使用setWillNotDraw(false);这里。
我已经得到了矩形的正确位置,唯一的问题是它没有显示出来。以下是我的大部分代码:
加载项:当我删除谷歌地图时,它按预期工作,实际上矩形绘制在背景frameLayout上,所以它将被地图覆盖,现在我解决问题的方式是添加一个图像视图为矩形,动态在触摸移动时改变其大小。我不知道如何直接在此视图组(framelayout)的顶部绘制
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
//stash reference to google map
mGoogleMap = getMap();
//show the location
mGoogleMap.setMyLocationEnabled(true);
mTouchView = new TouchableWrapper(getActivity());
mTouchView.addView(view);
return mTouchView;
}
private class TouchableWrapper extends FrameLayout {
//box is used to illustrate the drawing box, the painter is used to set the color of the box.
private Box mCurrentBox = null;
private Paint mBoxPaint;
public TouchableWrapper(Context context) {
super(context);
mBoxPaint = new Paint();
mBoxPaint.setColor(0xffff0000);
// mBoxPaint.setStrokeMiter(20);
mBoxPaint.setStrokeWidth(8);
this.setWillNotDraw(false);
setWillNotDraw(false);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// decide when the child should hold the motion event.
return true; //always let the relative layout to hold the motion event.
}
@Override
public boolean onTouchEvent(MotionEvent event) {
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mMapIsTouched = true;
mCurrentBox = new Box(curr);//original code for draw Rectangle.
Log.d(TAG, "action.down box is"+mCurrentBox);
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "action_move");
if (mCurrentBox != null) {
mCurrentBox.setmCurrent(curr);
Log.d(TAG, "action.move box is"+mCurrentBox);
this.invalidate(); //TODO
}
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "action_up");
mMapIsTouched = false;
mCurrentBox = null;
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
float left = 0;
float right = 0;
float top = 0;
float bottom = 0;
if (mCurrentBox!=null) {
left = Math.min(mCurrentBox.getmOrigin().x, mCurrentBox.getCurrent().x) ;
right = Math.max(mCurrentBox.getmOrigin().x, mCurrentBox.getCurrent().x) ;
top = Math.min(mCurrentBox.getmOrigin().y, mCurrentBox.getCurrent().y) ;
bottom = Math.max(mCurrentBox.getmOrigin().y, mCurrentBox.getCurrent().y) ;
canvas.drawRect(left, top, right, bottom, mBoxPaint);
}
Log.i(TAG, "value is"+left+right+top+bottom);
}
答案 0 :(得分:0)
您可以将Map包装在FrameLayout中,该FrameLayout也包含Drawable(您的矩形)。这样,一旦找到矩形的位置,就可以隐藏并显示它,而不必重绘它。
您可以查看here作为示例。