在android画布中用箭头绘制一条曲线

时间:2013-03-13 18:10:34

标签: android android-canvas

用箭头从point1(x1,y1)到point2(x2,y2)绘制一条曲线,就像这样 enter image description here

我正在Android画布中开发一个应用程序。 像应用程序一样自动化,我在绘制带有箭头的曲线时遇到了麻烦。 指着下一个圈子。

你可以给我一个代码或建议吗?

2 个答案:

答案 0 :(得分:5)

我认为你需要的是在路径和线条画上画出的混合物。在你的onDraw中声明这个方法:

    private void drawOvalAndArrow(Canvas canvas){


    Paint circlePaint = new Paint();
    circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    circlePaint.setAntiAlias(true);
    circlePaint.setStrokeWidth(2);
    circlePaint.setColor(Color.CYAN);

    float centerWidth = canvas.getWidth()/2; //get center x of display
    float centerHeight = canvas.getHeight()/2; //get center y of display
    float circleRadius = 20; //set radius 
    float circleDistance = 200; //set distance between both circles

    //draw circles
    canvas.drawCircle(centerWidth, centerHeight, circleRadius, circlePaint);
    canvas.drawCircle(centerWidth+circleDistance, centerHeight, circleRadius, circlePaint);


    //to draw an arrow, just lines needed, so style is only STROKE
    circlePaint.setStyle(Paint.Style.STROKE);       
    circlePaint.setColor(Color.RED);

    //create a path to draw on
    Path arrowPath = new Path();

    //create an invisible oval. the oval is for "behind the scenes" ,to set the path´
    //area. Imagine this is an egg behind your circles. the circles are in the middle of this egg
    final RectF arrowOval = new RectF();
    arrowOval.set(centerWidth, 
            centerHeight-80, 
            centerWidth + circleDistance, 
            centerHeight+80);

    //add the oval to path
    arrowPath.addArc(arrowOval,-180,180);

    //draw path on canvas
    canvas.drawPath(arrowPath, circlePaint);


    //draw arrowhead on path start
     arrowPath.moveTo(centerWidth,centerHeight ); //move to the center of first circle
     arrowPath.lineTo(centerWidth-circleRadius, centerHeight-circleRadius);//draw the first arrowhead line to the left
     arrowPath.moveTo(centerWidth,centerHeight );//move back to the center
     arrowPath.lineTo(centerWidth+circleRadius, centerHeight-circleRadius);//draw the next arrowhead line to the right

     //same as above on path end
     arrowPath.moveTo(centerWidth+circleDistance,centerHeight );
     arrowPath.lineTo((centerWidth+circleDistance)-circleRadius, centerHeight-circleRadius);
     arrowPath.moveTo(centerWidth+circleDistance,centerHeight );
     arrowPath.lineTo((centerWidth+circleDistance)+circleRadius, centerHeight-circleRadius);

     //draw the path
     canvas.drawPath(arrowPath,circlePaint);

}

这只是一个很糟糕的例子,但它应该显示从哪里开始。

答案 1 :(得分:1)

我知道我应该发表评论,但评论中的代码难以阅读,所以我提出了另一个答案。 答案 Opiatefuchs 这是基本正确的。但是如果你想测试他的代码,你应该注意一件事。

float centerWidth = canvas.getWidth()/2; //get center x of display
float centerHeight = canvas.getHeight()/2; //get center y of display

中心宽度和中心高度应如下所示,否则不会在屏幕上绘制任何内容。 而且对于普通手机的屏幕,circleDistance = 200有点大(对于我的设备samsung i9300,200太大,第二个圆位于屏幕范围之外。例如,你将其更改为较小的值80。)< / p>

    @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    centerWidth = w / 2;
    centerHeight = h / 2;
}

截图。

enter image description here