每次在画布上触摸屏幕时如何绘制新图像?

时间:2019-05-21 17:34:57

标签: android canvas point

这是我第一次在Android项目中使用canvas。我的目标是制作一个可以在每次触摸屏幕时获取点坐标的应用程序。我已经完成了添加多点的每个触摸和点之间的线。但是,关键是canvas.drawCircle。我想用我的可绘制xml更改我的观点的外观。可以做到吗?

公共类PaintView扩展了视图{

float mX;
float mY;
TextView mTVCoordinates;
private Paint paint = new Paint();
List<Point> points = new ArrayList<Point>();
private Path path = new Path();

Context context;
float ratioX, ratioY;

public PaintView(Context context) {
    super(context);
    this.context = context;

}

public PaintView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    paint.setColor(getResources().getColor(R.color.blue));
    paint.setStrokeWidth(4);
    paint.setAntiAlias(true);

    for (Point p : points) {
        canvas.drawCircle(p.x, p.y, 15, paint);

    }

    if (points.size() > 1) {
        for (int i = 1; i < points.size(); i++) {
            int size = i;
            canvas.drawLine(points.get(size - 1).x, points.get(size - 1).y, points.get(size).x, points.get(size).y, paint);
        }

    }

    invalidate();
}

public void setTextView(TextView tv) {
    // Reference to TextView Object
    mTVCoordinates = tv;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            if (points.size() < 7) {
                Point p = new Point();
                p.x = (int) event.getX();
                p.y = (int) event.getY();
                points.add(p);
                invalidate();
                if (mTVCoordinates != null) {
                    float x = mX * ratioX;
                    float y = mY * ratioY;
                    mTVCoordinates.setText("X :" + points.get(points.size() - 1).x + " , " + "Y :" + points.get(points.size() - 1).y + " count: " + points.size());
                }
            } else {
                points.clear();
                path.reset();
                invalidate();
            }

        case MotionEvent.ACTION_MOVE:  // a pointer was moved
            final float x = event.getX();
            final float y = event.getY();

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL: {
            break;
        }
    }
    invalidate();
    return true;

}

public PointF getPoints() {

    return new PointF(mX, mY);
}

public void setRatio(float ratiox, float ratioy) {
    ratioX = ratiox;
    ratioY = ratioy;
}

}

我的下一个目标是在我放下它时使每个点都可以移动。这该怎么做?谢谢

0 个答案:

没有答案