Android使用画布绘图(而不是地图)计算多边形的面积

时间:2014-06-20 19:00:05

标签: android canvas polygon area

我使用此代码在画布上绘制多边形。但我想计算多边形的面积。当然要给出每条线的测量值。我在地图上看到了很多例子但是我不在画布上转换/改编。任何人都可以展示方式或方法吗?

提前致谢。

import java.util.ArrayList;
import java.util.List;    
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(new DrawingView(MainActivity.this));
    }

    class DrawingView extends SurfaceView 
    {
        private SurfaceHolder surfaceHolder;
        private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        private List<Point> pointsList = new ArrayList<Point>();

        public DrawingView(Context context)
        {
            super(context);
            surfaceHolder = getHolder();
            paint.setColor(Color.BLACK);
            paint.setStyle(Style.FILL);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event)
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                if (surfaceHolder.getSurface().isValid()) 
                {
                    // Add current touch position to the list of points
                    pointsList.add(new Point((int) event.getX(), (int) event.getY()));

                    // Get canvas from surface
                    Canvas canvas = surfaceHolder.lockCanvas();

                    // Clear screen
                    canvas.drawColor(Color.WHITE);

                    // Iterate on the list
                    for (int i = 0; i < pointsList.size(); i++)
                    {
                        Point current = pointsList.get(i);
                        Point first = pointsList.get(0);

                        // Draw points
                        canvas.drawCircle(current.x, current.y, 5, paint);                  

                        // Draw line with next point (if it exists)
                        if (i + 1 < pointsList.size()) 
                        {
                            Point next = pointsList.get(i + 1);
                            canvas.drawLine(current.x, current.y, next.x, next.y, paint);
                            canvas.drawLine(next.x, next.y, first.x, first.y, paint);
                            c
                        }

                    }

                    // Release canvas
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这是一种计算多边形面积的方法。

for (int i = 0; i < points.size(); i++) {
    float addX = points.get(i).x;
    float addY = points.get(i == points.size() - 1 ? 0 : i + 1).y;
    float subX = points.get(i == points.size() - 1 ? 0 : i + 1).x;
    float subY = points.get(i).y;
    total += (addX * addY * 0.5);
    total -= (subX * subY * 0.5);
}
return Math.abs(total);