0

If I draw with Rectangle line and after that I want to draw with dash line my rectangle line will be transformed in dash line. I use this code:

This is MyLine.java

public class MyLine { //  line
public float x,y;
public float xStart,yStart,xEnd,yEnd;

public Paint paint = new Paint();
Path path = new Path();

public MyLine()
{

    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    paint.setPathEffect(new DashPathEffect(new float[]{20,30}, 0));
    paint.setAntiAlias(true);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(5f);
    path = new Path();
}

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){
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setPathEffect(new DashPathEffect(new float[]{10,20}, 0));
    paint.setAntiAlias(true);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(5f);
    path = new Path();

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

public void changeColor(int color){
    paint = new Paint(paint);
    paint.setColor(color);
    //myPaint.setColor(color);
}

}

Here is some code from CanvasView.java where I think is problem.

public void setDashLine(){

    dashedLine = true;
    paint = new Paint();
    paint.setPathEffect(dashEffect);
    paint.setStyle(Paint.Style.STROKE);
    paint.setAntiAlias(true);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(STROKE_WIDTH);
    myCanvas = new Canvas();
    path = new Path();
}

public void setNormalLine(){
    paint.setColor(Color.RED);
    paint = new Paint();
    paint.setPathEffect(null);
    paint.setStyle(Paint.Style.STROKE);
    paint.setPathEffect(null);
    paint.setAntiAlias(true);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(STROKE_WIDTH);
    myCanvas = new Canvas();
    path = new Path();
}

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(MyLine line:lineList){ //draw lines
     if(dashedLine)
         line.paint.setPathEffect(dashEffect);
     else
         line.paint.setPathEffect(null);
        canvas.drawLine(line.xStart, line.yStart, line.xEnd, line.yEnd, line.paint);
  }
final OnTouchListener drawLineListener = new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {

        FirstActivity.ll.setVisibility(LinearLayout.GONE);

        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              undonePaths.clear();
              path.reset();
              myLine = new MyLine();
              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:
              myLine = new MyLine();

             return true;

          case MotionEvent.ACTION_MOVE:
          case MotionEvent.ACTION_UP:

            invalidate();
            lineList.add(myLine); 
            break;

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

    }
};

If you can see something where is wrong please tell me how can make to be useful.

4

0 回答 0