Android的如何使用复用油漆画在放手图形页面? [英] Android how to draw paint in free hand in MapView using overlay?

查看:125
本文介绍了Android的如何使用复用油漆画在放手图形页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序绘制在地图视图放开手脚油漆,但在搜索很多资料终于在MapView的矩形战平了,但我想代替矩形画放手像锯齿如何改变我的$ C $(c)任何帮助,请..

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 like zigzag how to change my code Any help please..

MapOverlay.java

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

}

使用这个我能够得出这样的矩形形状和制定再点击切换按钮(可以得出多次)

using this i able to draw like this rectangle shapes and draw up to again you click the toggle button(possible to draw multiple times)

我要画线,而不是矩形像下图(画多次)。

i want draw lines instead of rectangle like below image(draw multiple times).

终于,我发现这个链接这个链接提供矩形平局的 HTTP://n3vrax.word$p$pss.com/2011/08/13/drawing-overlays-on-android-map-view/

finally i found this link this link provide rectangle shape draw http://n3vrax.wordpress.com/2011/08/13/drawing-overlays-on-android-map-view/

只是改变矩形释放得出任何想法,请....

just change rectangle to free draw any idea please....

推荐答案

您可以放开手脚使用code波纹管划清界限:

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

使用

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

注意

这是一个全功能的例子,以帮助您开始。然而,你应该优化code,使之更有效(即使用路径来存储的OnDraw绘制路径(),减少记录在 onTouch点的数量(),等等)。

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.).

享受它。

这篇关于Android的如何使用复用油漆画在放手图形页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆