Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw multiple lines with finger? (Android)

I have tried to draw multiple lines like this:

`

l1 = new Path();

l2 = new Path();
l3 = new Path();
l4 = new Path();`
---
`mPathList.add(l1...l4);`
---

    `public void onDraw(Canvas canvas) {
    ...

for (Path path : mPathList) {
canvas.drawPath(path, mOverlayPaint);

    }
    ...
    }`

    ---
    `case MotionEvent.ACTION_MOVE:
    int X = (int)me.getRawX();
    int Y = (int)me.getRawY();

    l1.moveTo(X, Y);
    l2.moveTo(X + 5, Y);
    l3.moveTo(X + 10, Y);
    l4.moveTo(X + 15, Y);
    break;`

But when I'm trying to draw something, FPS will slowly decrease. Any ideas how to make it works fine? P.S. Sorry for my English, please

like image 436
Helisia Avatar asked Sep 15 '13 08:09

Helisia


1 Answers

What you need to do is store the lines inside an arraylist and then read the arraylist onDraw(). try this code for your View Class:

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, 
  float joinX, joinY = 0;
  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) {
      if (joinX <= 0 || joinY <= 0) lines.add(new Line(event.getX(), event.getY()));
      else lines.add(new Line(joinX, joinY);
      return true;
    }
    else if ((event.getAction() == MotionEvent.ACTION_MOVE) &&
        lines.size() > 0) {
      Line current = lines.get(lines.size() - 1);
      current.stopX = event.getX();
      current.stopY = event.getY();
      Invalidate();
      return true;
    }
    else if ((event.getAction() == MotionEvent.ACTION_UP) &&
        lines.size() > 0 {
      Line current = lines.get(lines.size() - 1);
      current.stopX = event.getX();
      current.stopY = event.getY();
      joinX = event.getX();
      joinY = event.getY();
      Invalidate();
      return true;
    }
    else {
      return false;
    }
  }
}
like image 177
Caleb Bramwell Avatar answered Sep 19 '22 17:09

Caleb Bramwell