将路径对象添加到arraylist

时间:2014-01-01 18:04:45

标签: android arraylist path android-canvas draw

我创建了一个如下所示的路径对象,它绘制了不同的形状。不同的按钮响应在画布上绘制不同的形状。每次调用按钮进入arraylist时我都会尝试保存路径,并在每次增加arraylist时重绘画布上的所有路径。

        public class DrawImage extends SurfaceView {
            ArrayList<Path> paths = new ArrayList<Path>();
        Path path = new Path();

                 @Override
                protected void onDraw(Canvas canvas) {
                    // TODO Auto-generated method stub
                    super.onDraw(canvas);


            if (MainActivity.isRectangle) {


                        path.moveTo(1, 1);
                        path.lineTo(90, 1);
                        path.lineTo(90, 60);
                        path.lineTo(1, 60);

                        path.close();


                        paintColor.setColor(Color.RED);
                        paintColor.setStrokeWidth(2);
                        paintColor.setStyle(Paint.Style.STROKE);

                        paths.add(path);

             for (Path p : paths) {
                    canvas.drawPath(p, paintColor);
                }

                    }
if (MainActivity.isTriangle) {

                  path.moveTo(0, 40);
                  path.lineTo(50, 0);
                  path.lineTo(50, 50);
                  path.lineTo(50, 50);
                  path.lineTo(0, 40);

                        path.close();


                        paintColor.setColor(Color.RED);
                        paintColor.setStrokeWidth(2);
                        paintColor.setStyle(Paint.Style.STROKE);

                        paths.add(path);

             for (Path p : paths) {
                    canvas.drawPath(p, paintColor);
                }

                    }



            }

这是我的mainActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dI = new DrawImage(this);

        rectbutton = (Button) findViewById(R.id.pointerbutton);


        RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        // add here other layout params rules to make your
        // custom view stay below the buttons

        lp.addRule(RelativeLayout.BELOW, R.id.rectbutton);

        dI.setLayoutParams(lp);

        mainLayout.addView(dI);

rectbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // TODO Auto-generated method stub
                isTriangle = false;
                isRectangle = true;
                isCircle= false;
                isParallelogram = false;
                isTapezium = false;


                dv.invalidate();


            }// onclick

        });

trianglebutton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    // TODO Auto-generated method stub
                    isTriangle = true;
                    isRectangle = false;
                    isCircle= false;
                    isParallelogram = false;
                    isTapezium = false;


                    dv.invalidate();


                }// onclick

            });

问题:两个路径对象重叠,当第二次调用rect按钮或三角形按钮时,新的路径对象不会出现。

请指教谢谢。

DrawImage类的修正

 public class DrawImage extends SurfaceView  {


    Paint paintColor = new Paint();

    Path path = new Path();

    Canvas canvas;

    float x = 0, y = 0;


    public static ArrayList<Path> paths = new ArrayList<Path>();


    public DrawImage(Context context) {

        super(context);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        Paint background = new Paint();
        background.setColor(Color.WHITE);
        canvas.drawARGB(255, 255, 255, 255);

        paintColor.setColor(Color.GRAY);
        paintColor.setStrokeWidth(2);
        paintColor.setStyle(Paint.Style.STROKE);

        for (Path p : paths) {
            canvas.drawPath(p, paintColor);
        }

    }


    public void addRectangle() {

        Path path = new Path();

          path.moveTo(x+1, y+1);
              path.lineTo(x+90, y+1);
              path.lineTo(x+90, y+60);
              path.lineTo(x+1, y+60);

        path.close();
        paths.add(path);
    }


}

对mainActivity的修正

layout.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            x = event.getX();

            y = event.getY();

            et1.setText(String.valueOf(x));
            et2.setText(String.valueOf(x));

            break;

        }
        dv.invalidate();
        return true;

    }
});

}

2 个答案:

答案 0 :(得分:1)

这是一个在您的代码中出现的简单问题。

查看您何时执行Paths.add(path) - 它将路径的地址存储为其对象。

而不是这样做Paths.add(new Path(path)) - 现在这将创建一个新对象,它将存储在arraylist中。

现在你为什么要走空路?

当你执行path.reset()时,原因很简单,路径变空,arraylist指向现在已重置的同一地址。

答案 1 :(得分:0)

我会尝试这样的事情:

addRectangle()课程添加方法DrawImage(三角形类似的方法......)

public void addRectangle() {
     Path path = new Path();
     path.moveTo(1, 1);
     path.lineTo(90, 1);
     path.lineTo(90, 60);
     path.lineTo(1, 60);
     path.close();
     paths.add(path);
}

draw方法减少到此(实际上,为了提高性能,您还应该只初始化Paint个对象,而不是每次都调用onDraw

protected void onDraw(Canvas canvas) {
     paintColor.setColor(Color.RED);
     paintColor.setStrokeWidth(2);
     paintColor.setStyle(Paint.Style.STROKE);
     for (Path p : paths) {
         canvas.drawPath(p, paintColor);
     }
}

MainActivity中删除所有静态isRectangle ...变量,而不是onClick方法。你的矩形按钮执行此操作(我假设dvDrawImage实例:

public void onClick(View arg0) {
        dv.addRectangle();
        dv.invalidate();
}

请注意,所有矩形都绘制在相同的位置,因此添加的数量没有区别。但您可以轻松扩展addRectangle()方法以传递起始位置,矩形大小等。

另请注意,我认为代码中的主要错误是,您已将path定义为DrawImage中的全局变量,因为您始终将相同的对象添加到列表,因此列表将始终只包含该单个对象。