2012-10-19 57 views
5

Trong ứng dụng của tôi Vẽ sơn bằng tay miễn phí trên Chế độ xem bản đồ nhưng tìm kiếm rất nhiều thông tin cuối cùng có được từ hình chữ nhật vẽ trên mapview nhưng tôi muốn thay thế hình chữ nhật vẽ tay miễn phí như ngoằn ngoèo làm thế nào để thay đổi mã của tôi Bất kỳ trợ giúp xin vui lòng ..Android làm thế nào để vẽ sơn trong tay miễn phí trong MapView bằng cách sử dụng lớp phủ?

MapOverlay.java

public class MapOverlay extends Overlay { 

private float x1,y1,x2,y2; 
private GeoPoint p1=null,p2=null; 
private MapExampleActivity mv = null; 
private Paint paint = new Paint(); 
private Path path = new Path(); 
private boolean isUp = false; 

//constructor receiving the initial point 
    public MapOverlay(MapExampleActivity mapV,float x,float y){ 
    paint.setStrokeWidth(2.0f); 
    x1 = x; 
    y1 = y; 
    mv = mapV; 
    p1 = mapV.getMapView().getProjection().fromPixels((int)x1,(int)y1); 
} 
//override draw method to add our custom drawings 
@Override 
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 

    if(p1 != null && p2 != null){ 
     //get the 2 geopoints defining the area and transform them to pixels 
     //this way if we move or zoom the map rectangle will follow accordingly 
     Point screenPts1 = new Point(); 
     mapView.getProjection().toPixels(p1, screenPts1); 
     Point screenPts2 = new Point(); 
     mapView.getProjection().toPixels(p2, screenPts2);     

     //draw inner rectangle 
     paint.setColor(Color.BLUE); 
    // paint.setStyle(Style.FILL); 
     canvas.drawPath(path, paint);  
     canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint); 
     //draw outline rectangle 

    // paint.setColor(Color.YELLOW); 
     paint.setStyle(Style.STROKE); 
    // canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint); 
     canvas.drawPath(path, paint); 
    } 
    return true; 
}  

@Override 
public boolean onTouchEvent(MotionEvent e, MapView mapView) { 
    if(mv.isEditMode() && !isUp){ 

     if(e.getAction() == MotionEvent.ACTION_DOWN){ 
      x1 = y1 = 0; 
      x1 = e.getX(); 
      y1 = e.getY(); 
      p1 = mapView.getProjection().fromPixels((int)x1,(int)y1);    
     } 

     //here we constantly change geopoint p2 as we move out finger 
     if(e.getAction() == MotionEvent.ACTION_MOVE){ 
      x2 = e.getX(); 
      y2 = e.getY(); 
      p2 = mapView.getProjection().fromPixels((int)x2,(int)y2);    
     } 

     //---when user lifts his finger--- 
     if (e.getAction() == MotionEvent.ACTION_UP) {     
      isUp = true; 
     }  
     return true; 
    } 
    return false; 
} 

}

sử dụng i này có thể vẽ giống như hình dạng hình chữ nhật này và vẽ lên một lần nữa bạn nhấp vào nút chuyển đổi (có thể rút ra nhiều lần)

Rectangle shape

tôi muốn vẽ đường thay vì hình chữ nhật như hình bên dưới (vẽ nhiều lần).

enter image description here

cuối cùng tôi tìm thấy liên kết này liên kết này cung cấp hình chữ nhật vẽ http://n3vrax.wordpress.com/2011/08/13/drawing-overlays-on-android-map-view/

chỉ cần thay đổi hình chữ nhật để giải phóng rút ra bất kỳ ý tưởng xin vui lòng ....

Trả lời

5

Bạn có thể rảnh tay vẽ một đường sử dụng mã bên dưới:

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

sử dụng

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

Đây là một ví dụ chức năng đầy đủ để giúp bạn bắt đầu. Tuy nhiên, bạn nên tối ưu hóa mã để làm cho mã hiệu quả hơn (tức là sử dụng Path để lưu trữ đường vẽ trong onDraw(), giảm số điểm được ghi trong onTouch(), v.v.).

Hãy tận hưởng nó.

+0

chúng tôi sử dụng nút vẽ cho drawpaint trên bản đồ tiếp theo khi nút vẽ cuộc gọi sẽ hiển thị bản đồ có sơn ... sau đó chúng tôi sẽ phóng to và di chuyển ... nhưng từ khi nút vẽ cuộc gọi thứ hai cũng vẽ tranh. –

+0

@Luic Vẽ lần thứ hai vẽ lần đầu tiên vẽ không hiển thị và khi tôi nhấp vào nút không thể phóng to bản đồ ..... Chỉ cần nhìn vào liên kết ví dụ chúng tôi đã có ý tưởng rõ ràng ... – NagarjunaReddy

+0

Tôi không thể hiểu được bình luận của bạn, bạn cần để rephrase nó. Tôi có thể đảm bảo với bạn rằng mã ở trên hoạt động hoàn hảo, bởi vì tôi đã thử nghiệm nó trước khi đăng bài. Về zoom, ofcorse bạn không thể zoom trong khi bạn đang vẽ trên bản đồ. Nếu bạn muốn thu phóng hoạt động, bạn cần đặt 'editMode()' thành false hoặc bạn cần loại trừ khu vực nút thu phóng khỏi khu vực vẽ. – Luis

Các vấn đề liên quan