Android Canvas drawLine

时间:2013-05-25 09:23:18

标签: android

我有一个自定义布局来根据触摸输入绘制一条线。我有它绘制线条但是当用户触摸屏幕时,线条会消失并且它会绘制一条新线条。我想要它做的是绘制一条新线并将前一条线留在那里。 这是我的代码:

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;

    public class DrawView extends View {
      Paint paint = new Paint();
      float startX;
      float startY;
      float stopX;
      float stopY;

      public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
      }

      @Override
      protected void onDraw(Canvas canvas) {
          canvas.drawLine(startX, startY, stopX, stopY, paint);
      }

      @Override
              public boolean onTouchEvent(MotionEvent event) {

       switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startX = event.getX();
            startY = event.getY();
          return true;
        case MotionEvent.ACTION_MOVE:
          stopX = event.getX();
          stopY = event.getY();
         break;
        case MotionEvent.ACTION_UP:    
            stopX = event.getX();
            stopY = event.getY();
          break;
        default:
          return false;
        }
          Invalidate();
        return true;
      }
    } 

1 个答案:

答案 0 :(得分:14)

您需要存储所有行而不是最后一行 以下代码完全未经测试,但希望能为您提供一般性的想法。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;

class Line {
  float startX, startY, stopX, stopY;
  public Line(float startX, float startY, float stopX, float stopY) {
    this.startX = startX;
    this.startY = startY;
    this.stopX = stopX;
    this.stopY = stopY;
  }
  public Line(float startX, float startY) { // for convenience
    this(startX, startY, startX, startY);
  }
}

public class DrawView extends View {
  Paint paint = new Paint();
  ArrayList<Line> lines = new ArrayList<Line>();

  public DrawView(Context context, AttributeSet attrs) {
    super(context, attrs);

    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    for (Line l : lines) {
      canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, paint);
    }
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      lines.add(new Line(event.getX(), event.getY()));
      return true;
    }
    else if ((event.getAction() == MotionEvent.ACTION_MOVE ||
        event.getAction() == MotionEvent.ACTION_UP) &&
        lines.size() > 0) {
      Line current = lines.get(lines.size() - 1);
      current.stopX = event.getX();
      current.stopY = event.getY();
      Invalidate();
      return true;
    }
    else {
      return false;
    }
  }
}