Android how to draw paint in free hand in MapView using overlay?

前端 未结 1 1110
南笙
南笙 2021-02-04 20:12

In my app Draw paint in free hand on Map view but searching lot of information finally got from rectangle shape draw on mapview but i want in place of rectangle draw free hand l

相关标签:
1条回答
  • 2021-02-04 20:29

    You can free hand draw a line using the code bellow:

    Code

    public class HandDrawOverlay extends Overlay { 
    
    private boolean editMode = false;
    private boolean isTouched = false;
    private Paint paint = new Paint(); 
    private Point screenPt1 = new Point(); 
    private Point screenPt2 = new Point(); 
    private ArrayList<GeoPoint> points = null;
    
    public HandDrawOverlay(){ 
        paint.setStrokeWidth(2.0f); 
        paint.setStyle(Style.STROKE); 
        paint.setColor(Color.BLUE); 
    } 
    
    @Override 
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        if(points != null && points.size() > 1){
            mapView.getProjection().toPixels(points.get(0), screenPt1); 
            for(int i=1; i<points.size();i++){
                mapView.getProjection().toPixels(points.get(i), screenPt2);
                canvas.drawLine(screenPt1.x, screenPt1.y, screenPt2.x, screenPt2.y, paint);
                screenPt1.set(screenPt2.x, screenPt2.y);
            }
        }
    }     
    
    @Override 
    public boolean onTouchEvent(MotionEvent e, MapView mapView) { 
        if(editMode){ 
            int x = (int)e.getX();
            int y = (int)e.getY();
            GeoPoint geoP = mapView.getProjection().fromPixels(x,y);
    
            switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouched = true;
                points = new ArrayList<GeoPoint>();
                points.add(geoP);
                break;
            case MotionEvent.ACTION_MOVE:
                if(isTouched)
                    points.add(geoP);
                break;
            case MotionEvent.ACTION_UP:
                if(isTouched)
                    points.add(geoP);
                isTouched = false;
                break;
            }
            mapView.invalidate();
            return true; 
        } 
        return false; 
    }
    
    /**
     * @return the editMode
     */
    public boolean isEditMode() {
        return editMode;
    }
    
    /**
     * @param editMode the editMode to set
     */
    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    } 
    }
    

    to use

    HandDrawOverlay handDrawOverlay;
    handDrawOverlay = new HandDrawOverlay();
    mapView.getOverlays().add(handDrawOverlay);
    
    //Set edit mode to true to start drwaing
    handDrawOverlay.setEditMode(true);
    
    //Set edit mode to true to stop drwaing
    handDrawOverlay.setEditMode(false);
    

    Note

    This is a full functioning example to help you starting. However, you should optimize the code to make it more efficient (i.e. using Path to store the drawing path in onDraw(), reducing the number of points recorded in onTouch(), etc.).

    Enjoy it.

    0 讨论(0)
提交回复
热议问题