给定旋转角度如何手动绘制箭头 - android

时间:2012-10-18 11:05:39

标签: android math drawing android-mapview

我想在地图上手动绘制箭头。我没有使用drawables的旋转来解决性能问题,因为如果使用这种技术绘制的话,我有许多覆盖会减慢应用程序的速度。

我将手动绘制箭头,给定gps点和旋转角度,我需要在该点上绘制箭头。 我将扩展overlay类,在draw方法上,我将完成绘图工作。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我在我的应用中遇到了同样的问题。代码is on github

基本上我覆盖draw并在绘制箭头之前旋转画布。

@Override
public void draw(Canvas canvas) {

    //NOTE: 0, 0 is the bottom center point of the bus icon

    //first draw the bus
    vehicle.draw(canvas);

    //then draw arrow
    if (arrow != null && heading != BusLocation.NO_HEADING)
    {
        //put the arrow in the bus window

        int arrowLeft = -(arrow.getIntrinsicWidth() / 4);
        int arrowTop = -vehicle.getIntrinsicHeight() + arrowTopDiff;

        //NOTE: use integer division when possible. This code is frequently executed
        int arrowWidth = (arrow.getIntrinsicWidth() * 6) / 10;  
        int arrowHeight = (arrow.getIntrinsicHeight() * 6) / 10;
        int arrowRight = arrowLeft + arrowWidth;
        int arrowBottom = arrowTop + arrowHeight;

        arrow.setBounds(arrowLeft, arrowTop, arrowRight, arrowBottom);

        canvas.save();
        //set rotation pivot at the center of the arrow image
        canvas.rotate(heading, arrowLeft + arrowWidth/2, arrowTop + arrowHeight / 2);

        Rect rect = arrow.getBounds();
        arrow.draw(canvas);
        arrow.setBounds(rect);

        canvas.restore();

    }

}