矩形线和短划线

时间:2013-03-29 10:31:48

标签: java android paint

如果我使用矩形线绘制,之后我想用虚线绘制,我的矩形线将以虚线变换。我使用这段代码:

这是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);
}

}

以下是我认为存在问题的CanvasView.java中的一些代码。

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;

    }
};

如果你能看到出错的地方,请告诉我如何才能有用。

0 个答案:

没有答案