使用偏移的画布路径制作多个副本(onDraw运行两次)

时间:2014-11-01 19:16:19

标签: android canvas path offset

似乎我有一个问题需要理解偏移是如何工作的。当我使用下面的代码时,它会创建5个箭头,但为什么呢?我创建一个并使用两次偏移。所以我认为我得到3个箭头。

public class legende extends View {

    private Paint paint;
    private Path arrowPath;

    public legende(Context context) {
        super(context);
        init();
    }

    public void init(){
        paint = new Paint();
        arrowPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 1500;
        int height = 5000;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        arrowPath.moveTo(420, 300);
        arrowPath.lineTo(430, 300);
        arrowPath.lineTo(420, 310);
        arrowPath.lineTo(410, 300);
        arrowPath.close();
        canvas.drawPath(arrowPath, paint);


        arrowPath.offset(0,200);
        canvas.drawPath(arrowPath, paint);

        arrowPath.offset(0,380);
        canvas.drawPath(arrowPath, paint);
    }
}

修改

看起来,问题是,onDraw被调用了两次,但我不知道为什么。 这就是我在片段活动中使用它的方式:

            hScrollView.addView(new legende(getActivity()));
            scrollView.addView(hScrollView);
            relativeLayout.addView(scrollView);

1 个答案:

答案 0 :(得分:0)

Looks like you´ve got a non thread safe implementation of the onDraw method.

This guy worked it out by using a thread safe implementation: Custom View not drawing properly

Maybe you should copy the path to a local object inside the onDraw and then alter and paint.