当用户移动手指时如何在android中绘制矩形?

时间:2014-11-11 16:36:53

标签: java android android-canvas android-drawable

我想绘制一个矩形。第一个角应该是用户首次触摸屏幕的点。当用户移动他的手指时,它应该绘制矩形。这里显示了我要做的视频a link。{{3}}但我不明白,也许你可以帮助我。我只是想在不在图像上的白色背景上绘制该矩形。

我的代码:

package com.example.androiddrawing;

import java.util.ArrayList;
import java.util.List;

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

public class CanvasView extends View {

    private Canvas canvas;
    private Paint paint = new Paint();
    private Paint paint2 = new Paint();
    private Paint paint3 = new Paint();
    private Path path = new Path();
    private Point point = new Point();
    private static List<Path> lines = new ArrayList<Path>();
    private static List<Point> points = new ArrayList<Point>();
    private float x, x2, xc, xd, x3, x4;
    private float y, y2, yc, yd, y3, y4;
    private boolean touchStarted = false;

    public enum DrawMode {
        FreeDrawMode, RectDrawMode
    };

    public static DrawMode currentDrawMode;

    public void setDrawMode(DrawMode newDrawMode) {
        this.currentDrawMode = newDrawMode;
    }

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

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

        paint2.setAntiAlias(true);
        paint2.setStrokeWidth(5);
        paint2.setColor(Color.RED);
        paint2.setStyle(Paint.Style.STROKE);
        paint2.setStrokeJoin(Paint.Join.ROUND);

        paint3.setAntiAlias(true);
        paint3.setColor(Color.BLACK);
        paint3.setStrokeWidth(10);
        paint3.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        for (Path p : lines)
            canvas.drawPath(p, paint);
        canvas.drawPath(path, paint2);

        for (Point point : points)
            canvas.drawCircle(point.x, point.y, 0, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getX();
        y = event.getY();

        System.out.println(currentDrawMode);
        if (currentDrawMode == DrawMode.FreeDrawMode) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Set a new starting point
                paint2.setColor(Color.RED);
                path = new Path();
                path.moveTo(x, y);
                touchStarted = true;


                break;
            // return true;
            case MotionEvent.ACTION_MOVE:
                // Connect the points
                touchStarted = false;
                path.lineTo(x, y);

                break;
            case MotionEvent.ACTION_UP:
                if (touchStarted) {
                    point = new Point();
                    point.x = (int) x;
                    point.y = (int) y;
                    paint2.setColor(Color.BLACK);
                    points.add(point);
                    touchStarted = false;
                    System.out.println("siin");
                } else {
                    System.out.println("seal");
                    paint2.setColor(Color.BLACK);
                    lines.add(path);
                }
                break;
            default:
                return false;
            }
        } else if (currentDrawMode == DrawMode.RectDrawMode) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Set a new starting point
                paint3.setColor(Color.RED);
            //CODE HERE

                break;
            // return true;
            case MotionEvent.ACTION_MOVE:

                //CODE HERE


                break;
            case MotionEvent.ACTION_UP:

                    //CODE HERE


                break;
            default:
                return false;
            }
        }
        // Makes our view repaint and call onDraw
        invalidate();
        return true;
    }

}

我应该把代码写在我放置注释的地方// CODE HERE,但我真的不明白,我怎么画一个矩形。

2 个答案:

答案 0 :(得分:5)

您可以使用以下代码。希望这会对你有所帮助。

public class DrawSample extends View {

int mStartX;
int mStartY;
int mEndX;
int mEndY;

Paint mPaint = new Paint();

int mSelectedColor = Color.BLACK;

public DrawSample(Context context, AttributeSet attrs, int defStyle) {

    super(context, attrs, defStyle);

    mPaint.setColor(mSelectedColor);
    mPaint.setStrokeWidth(5);
    mPaint.setStyle(Paint.Style.STROKE);

    setFocusable(true);
}

public DrawSample(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getActionMasked()) {

        case MotionEvent.ACTION_DOWN:

            mStartX = (int) event.getX();
            mStartY = (int) event.getY();

            break;

        case MotionEvent.ACTION_MOVE:

            mEndX = (int) event.getX();
            mEndY = (int) event.getY();

            invalidate();

            break;

        case MotionEvent.ACTION_UP:


            mEndX = (int) event.getX();
            mEndY = (int) event.getY();

            invalidate();

            break;

        default:

            super.onTouchEvent(event);

            break;
    }

    return true;
}

@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    canvas.drawRect(mStartX, mStartY, mEndX, mEndY, mPaint);
}
}

答案 1 :(得分:0)

你需要保留起点X和Y点,然后,当用户在MotionEvent.ACTION_UP中停止绘制时,得到结束的X和Y来得到矩形的4个角:

canvas.drawRect(startX, startY, x, y, paint3);

您可以找到有关drawRect herehere的更多信息。

编辑我没有很好的环境来测试它...但我在你的答案中发现了一些错误:

else if (currentDrawMode == DrawMode.RectDrawMode) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Set a new starting point
            paint3.setColor(Color.RED);
            startX = event.getX();
            startY = event.getY();

            break;
        // return true;
        case MotionEvent.ACTION_MOVE:

            endX = event.getX();
            endY = event.getY();

            canvas.drawRect(startX, startY, endX, endY, paint3);

            //invalidate(); // Tell View that the canvas needs to be redrawn
            break;
        case MotionEvent.ACTION_UP:

            paint3.setColor(Color.BLACK);
            canvas.drawRect(startX, startY, endX, endY, paint3);

            break;
        default:
            return false;
        }

PS:如果你想在你的问题中添加信息,请编辑它,但不要发布新的答案!!!