-1

MyLine と CanvasView を編集しましたが、現在は通常の線と破線を描画していますが、その後通常の線をクリックすると破線が描画されます。下に mt 編集されたコードがあります。MyLine で専用ペイントを作成した後、getPaint で onDraw の line.paint を変換しました。

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;

        }
    };
4

2 に答える 2

1

b in onDraw()

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

すべての線の効果をリセットしているので、それが描かれています: すべて破線またはすべて通常

おそらくやりたいことは、各 MyLine に独自のプロパティを与えることです。

boolean drawDashed =true/false;

そして、それに応じて自分自身を描画させます(適切なダッシュ効果で適切なペイントを選択します)

于 2013-03-29T18:04:01.790 に答える
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 に 2 つのプライベート プロパティを追加して、ライン自体が描画方法を認識できるようにします。

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

draw メソッドは非常に単純になります。

public void draw(Canvas c){    
    c.drawLine(xStart,yStart,xEnd,yEnd,paint);
}
于 2013-03-29T19:41:54.653 に答える