0

ユーザーが「描画」できるビューを作成しようとしています。今私がやっていることはパスで、それを線で結びますが、正しく動作しません。動作が遅く、物事が「奇妙」になります。このビデオでそれを見ることができますhttp://youtu.be/PUSUTFhDPrM、申し訳ありませんが、少し速くなりますが、私が話していることがわかります.

私の実際のコードは次のとおりです。

public class DrawView extends View implements OnTouchListener {

private static final String TAG = "DrawView";

private List<List<Point>> _paths = new ArrayList<List<Point>>();
private List<Point> _lastPath;
private Paint _paint = new Paint();
private Path _path = new Path();

public DrawView(Context context) {
    super(context);

    setFocusable(true);
    setFocusableInTouchMode(true);
    setOnTouchListener(this);

    _paint.setColor(Color.BLACK);
    _paint.setStyle(Paint.Style.STROKE);
    _paint.setStrokeWidth(5);
    _paint.setAntiAlias(true);
}
public DrawView(Context context, AttributeSet attrs) {
    super( context, attrs );

    setFocusable(true);
    setFocusableInTouchMode(true);
    setOnTouchListener(this);

    _paint.setColor(Color.BLACK);
    _paint.setStyle(Paint.Style.STROKE);
    _paint.setStrokeWidth(5);
    _paint.setAntiAlias(true);
}

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

    setFocusable(true);
    setFocusableInTouchMode(true);
    setOnTouchListener(this);

    _paint.setColor(Color.BLACK);
    _paint.setStyle(Paint.Style.STROKE);
    _paint.setStrokeWidth(5);
    _paint.setAntiAlias(true);
}

@Override
protected void onDraw(Canvas canvas) {

    for (List<Point> pointsPath : _paths) {
        _path.reset();
        boolean first = true;

        for (int i = 0; i < pointsPath.size(); i += 2) {
            Point point = pointsPath.get(i);

            if (first) {
                first = false;
                _path.moveTo(point.x, point.y);
            } else if (i < pointsPath.size() - 1) {
                Point next = pointsPath.get(i + 1);
                _path.quadTo(point.x, point.y, next.x, next.y);
            } else {
                _path.lineTo(point.x, point.y);
            }
        }
        canvas.drawPath(_path, _paint);
    }
}

public boolean onTouch(View view, MotionEvent event) {
    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();
    Log.d(TAG, "point: " + point);

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        _lastPath = new ArrayList<Point>();
        _lastPath.add(point);
        _paths.add(_lastPath);
        break;
    case MotionEvent.ACTION_MOVE:
        _lastPath.add(point);
        break;
    }
    invalidate();
    return true;
}

private class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}
public void changePaint(int Stroke, int color){

    _path.reset();

    _paint.setColor(color);
    _paint.setStyle(Paint.Style.STROKE);
    _paint.setStrokeWidth(Stroke);
    _paint.setAntiAlias(true);

}

}

私が欲しいのは、ユーザーが指で「描画」できるようにするためのより良い方法があるかどうか、またはこのコードの最も遅い部分を削除するために何を改善できるかを知ることです.

4

3 に答える 3