正常&虚线问题

时间:2013-03-29 15:52:41

标签: java android paint lines

我编辑了MyLine和CanvasView,但是现在我绘制了法线和虚线,但之后我点击了法线并绘制了虚线。你已经编辑了代码。在MyLine中创建私有绘制后,onDraw中的line.paint在getPaint

中进行了转换
Here you have MyLine.java

    public class MyLine { //  line
    public float x,y;
    public float xStart,yStart,xEnd,yEnd;
    private boolean drawDashed;
    private Paint paint = new Paint();

public MyLine(boolean drawDashed)
{
    if(drawDashed){
    getPaint().setColor(Color.BLUE);
    getPaint().setStyle(Paint.Style.STROKE);
    getPaint().setPathEffect(new DashPathEffect(new float[]{20,30}, 0));
    getPaint().setAntiAlias(true);
    getPaint().setStrokeJoin(Paint.Join.ROUND);
    getPaint().setStrokeWidth(5f);
    } else{ 
        getPaint().setColor(Color.RED);
        getPaint().setStyle(Paint.Style.STROKE);
        getPaint().setPathEffect(null);
        getPaint().setAntiAlias(true);
        getPaint().setStrokeJoin(Paint.Join.ROUND);
        getPaint().setStrokeWidth(5f);
    }
}

public void mouseDown(Path path,float xDown,float yDown){
    //path.moveTo(xDown, yDown);
//  path.lineTo(xDown, yDown);

    xStart = xDown;
    yStart = yDown;
}

public void mouseUp(Path path,float xUp,float yUp){
    //path.lineTo(xUp, yUp);
    xEnd = xUp;
    yEnd = yUp;
}

public void draw(Canvas c){
    getPaint().setColor(Color.GREEN);
    //paint.setStyle(Paint.Style.STROKE);
    getPaint().setPathEffect(new DashPathEffect(new float[]{10,20}, 0));
    getPaint().setAntiAlias(true);
    getPaint().setStrokeJoin(Paint.Join.ROUND);
    getPaint().setStrokeWidth(5f);

    c.drawLine(xStart,yStart,xEnd,yEnd,getPaint());
}

public Paint getPaint() {
    return paint;
}

public void setPaint(Paint paint) {
    this.paint = paint;
}

这是来自CanvasView.java

protected void onDraw(Canvas canvas) {
    paint.setPathEffect(null);
    if(bitmap!=null){

        canvas.drawBitmap(bitmap, 0, 0, paint);
         for(MyCircle circle:circleList){// draw circles

             myCanvas.drawCircle(getCircleMidPointX(circle.firstX, circle.lastX),getCircleMidPointY(circle.firstY, circle.lastY),circle.radius,myPaint);
         }

    }

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


     for(MyLine line:lineList){ //draw lines
         canvas.drawLine(line.xStart, line.yStart, line.xEnd, line.yEnd, line.getPaint());
      }
final OnTouchListener drawLineListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            FirstActivity.ll.setVisibility(LinearLayout.GONE);

            switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  myLine = new MyLine(dashedLine);
                  myLine.xStart = event.getX();
                  myLine.yStart = event.getY();

                return true;

              case MotionEvent.ACTION_MOVE:
              case MotionEvent.ACTION_UP:
                myLine.xEnd = event.getX();
                myLine.yEnd = event.getY();
                invalidate();
                lineList.add(myLine); 
                break;

              default:
                Log.d("mock it up", "Unknown touch event  " + event.toString());
                return false;
            }
            return true;

        }
    };

    final OnTouchListener drawDashedLineListener = new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            FirstActivity.ll.setVisibility(LinearLayout.GONE);

            switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                 return true;

              case MotionEvent.ACTION_MOVE:
              case MotionEvent.ACTION_UP:

                break;

              default:
                Log.d("mock it up", "Unknown touch event  " + event.toString());
                return false;
            }
            return true;

        }
    };

2 个答案:

答案 0 :(得分:1)

b on onDraw()

 if(dashedLine)
     line.paint.setPathEffect(dashEffect);
 else
     line.paint.setPathEffect(null);

您正在重置所有线条的效果,因此正在绘制的内容:全部虚线或全部正常

您可能想要做的是为每个MyLine提供自己的属性:

boolean drawDashed =true/false;

让它相应地绘制自己(用正确的效果选择正确的颜料)

答案 1 :(得分:0)

public class MyLine { //  line

public float x,y;
    public float xStart,yStart,xEnd,yEnd;
    private boolean drawDashed;
    private Paint paint = new Paint();
    public MyLine(boolean drawDashed)
    {
      if(drawDashed){
        ...
        paint.setPathEffect(new DashPathEffect(new float[]{10,20}, 0));
        ....
      } else{  
         ....
         paint.setPathEffect(null);
         ...
      }
    }

public void mouseDown(Path path,float xDown,float yDown){
    //path.moveTo(xDown, yDown);
//  path.lineTo(xDown, yDown);

    xStart = xDown;
    yStart = yDown;
}

public void mouseUp(Path path,float xUp,float yUp){
    //path.lineTo(xUp, yUp);
    xEnd = xUp;
    yEnd = yUp;
}

public void draw(Canvas c){

    c.drawLine(xStart,yStart,xEnd,yEnd,paint);
}

}

因此,向Myline添加两个私有属性,以便它自己拥有如何绘制自己的知识:

private boolean drawDashed;
private Paint paint = new Paint();

然后绘制方法变得非常简单:

public void draw(Canvas c){    
    c.drawLine(xStart,yStart,xEnd,yEnd,paint);
}