如何在Canvas上使用Touch事件移动ShapeDrawable

4

我正在尝试在Android中实现一款绘图应用程序。用户应该能够选择和移动所绘制的形状。
目前,我已经静态地在我的绘图画布上绘制了一些矩形和文本:

        View mDrawingCanvas = new View(mContext) 
        {
            ShapeDrawable rectangle;
            @Override
            public boolean isFocused() {
                // TODO Auto-generated method stub
                Log.d(TAG, "View's On focused is called !");
                return super.isFocused();
            }

            @Override
            public boolean onTouchEvent(MotionEvent event) {
                // TODO Auto-generated method stub

                return super.onTouchEvent(event);
            }

            @Override
            protected void onDraw(final Canvas canvas) {
                super.onDraw(canvas);
                // Work out current total scale factor
                // from source to view

                final float scale = mSourceScale*(float)getWidth()/(float)mSize.x;

                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(Color.WHITE);

                //Custom View
                rectangle = new ShapeDrawable(new RectShape());
                rectangle.getPaint().setColor(Color.GRAY);
                rectangle.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
                rectangle.getPaint().setStrokeWidth(3);
                rectangle.setBounds((int)(50*scale), (int)(30*scale), (int)(200*scale), (int)(150*scale));
                rectangle.draw(canvas);

                rectangle.getPaint().setColor(Color.BLUE);
                rectangle.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
                rectangle.getPaint().setStrokeWidth(3);
                rectangle.setBounds((int)(200*scale), (int)(200*scale), (int)(400*scale), (int)(350*scale));
                rectangle.draw(canvas);
            }
        };

我希望在绘图画布的onTouch事件中,能够选择(在所选形状上画边框)并移动所绘制的形状。
请问有人能指导我如何实现吗?非常感谢任何帮助。

2个回答

4

1

在触摸事件中应保存X和Y位置,并在绘制形状时使用它们。以下是一个非常基本的示例,但您需要改进它(检查触摸是否在对象内并仅更改该对象的值)

示例:

public class DrawTest extends View {

    private static final String TAG = "Desenho";

    private ShapeDrawable rectangle;
    private Paint paint;
    private float currX, currY;
    private Rect blue, gray;

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

        currX = 1;
        currY = 1;

        gray = new Rect(50,30,200,150);
        blue = new Rect(200,200,400,350);

        paint = new Paint();
        rectangle = new ShapeDrawable(new RectShape());
    }

    @Override
    public boolean isFocused() {
        Log.d(TAG, "View's On focused is called !");
        return super.isFocused();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        currX = event.getX();
        currY = event.getY();
        invalidate();
        Log.d(TAG, "View's On touch is called! X= "+currX + ", Y= "+currY);
        return super.onTouchEvent(event);
    }

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

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);

        //Custom View
        rectangle.getPaint().setColor(Color.GRAY);
        rectangle.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        rectangle.getPaint().setStrokeWidth(3);
        gray.set((int)(50+currX), (int)(30+currY), (int)(200+currX), (int)(150+currY));
        rectangle.setBounds(gray);
        gray = rectangle.getBounds();
        rectangle.draw(canvas);

        rectangle.getPaint().setColor(Color.BLUE);
        rectangle.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        rectangle.getPaint().setStrokeWidth(3);
        blue.set((int)(200+currX), (int)(200+currY), (int)(400+currX), (int)(350+currY));
        rectangle.setBounds(blue);
        blue = rectangle.getBounds();
        rectangle.draw(canvas);

    }

}

1
我想移动所选的形状,例如如果我点击灰色矩形,则应为其绘制边框,并且它只应相对于当前onTouch坐标移动。 - Salman Khakwani
同样的问题,在我的情况下,画布上有5个圆圈,我需要选择其中一个并移动该特定形状? - Anjuka Koralage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接