如何在按下清除按钮时禁用谷歌地图上的免费手绘图

时间:2017-08-16 09:47:14

标签: android canvas paint

我正在研究需要在谷歌地图上添加免费手绘的项目。我已经在谷歌地图上成功绘图了。但我的问题是当我点击清除按钮它删除了我在地图上绘制的所有东西,但我没有禁用编辑模式,这意味着当我再次触摸屏幕然后它再次开始绘制。

我希望当我点击清除按钮时,它会清除所有内容并再次映射处于初始模式之前。

这里有一些截图

1。this screenshot after clicking clear button and still i am able to draw on map

这里有一些相关的代码......

这是我的地图活动

private void drawOnMap() {
        mcontent = (FrameLayout) findViewById(signature);
        save_button = (ImageButton) findViewById(R.id.save_button);
        save_button.setEnabled(false);
        exit_edit_mode = (ImageButton) findViewById(R.id.exit_edit_mode);
        mSignature = new signature(this, null);
        mcontent.addView(mSignature);

        save_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSignature.save();
            }
        });
        exit_edit_mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSignature.clear();
                mGoogleMap.getUiSettings().setScrollGesturesEnabled(true);
            }
        });

这是我在Google地图上执行绘图的课程

public class signature extends View {
    static final float STROKE_WIDTH = 10f;
    static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;

    Paint paint = new Paint();
    Path path = new Path();

    float lastTouchX;
    float lastTouchY;
    final RectF dirtyRect = new RectF();

    public signature(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
    }

    public void clear() {
        path.reset();
        invalidate();
        save_button.setEnabled(false);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
        //save.setEnabled(true);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                lastTouchX = eventX;
                lastTouchY = eventY;
                return true;

            case MotionEvent.ACTION_MOVE:

            case MotionEvent.ACTION_UP:

                resetDirtyRect(eventX, eventY);
                int historySize = event.getHistorySize();
                for (int i = 0; i < historySize; i++) {
                    float historicalX = event.getHistoricalX(i);
                    float historicalY = event.getHistoricalY(i);
                    path.lineTo(historicalX, historicalY);
                }
                path.lineTo(eventX, eventY);
                break;
        }

        invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
                (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
    }

    private void resetDirtyRect(float eventX, float eventY) {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
    }
}

提前致谢

1 个答案:

答案 0 :(得分:0)

我自己得到了它 在

之后添加此代码
msignature.clear()
mcontent.removeView(mSignature);