在android中制作可调整大小的矩形

时间:2015-06-01 12:22:39

标签: java android canvas ontouchevent

我正在研究android中的一个案例。我需要绘制一个可调整大小的矩形然后扭曲它。我试图通过4个圆圈和连接这些圆圈绘制它。但我无法单独移动它们,因为在我的代码中它们都是相互关联的。我找不到解决方案。我的代码如图所示

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {

    private float x;
    private float y;
    private int x1;
    private int y1;
    private int x2;
    private int y2;

    public MyView(Context context) {
        super(context);
    }

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            x = event.getX();
            y = event.getY();
            x1 = 0;
            y1 = 0;
            x2 = 0;
            y2 = 0;
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            x = event.getX();
            y = event.getY();
            x1 = 0;
            y1 = 0;
            invalidate();
            break;
        }
        return true;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint1 = new Paint();
        paint1.setStyle(Paint.Style.FILL);
        paint1.setColor(Color.WHITE);
        canvas.drawPaint(paint1);
        paint1.setColor(Color.parseColor("#CD5C5C"));
        canvas.drawCircle(x1 + x, y1 + y, 50, paint1);
        canvas.drawCircle(700, 400, 50, paint1);
        canvas.drawLine(x1 + x, y1 + y, 700, 400, paint1);
        canvas.drawCircle(300, 800, 50, paint1);
        canvas.drawCircle(700, 800, 50, paint1);
        canvas.drawLine(x1 + x, y1 + y, 300, 800, paint1);
        canvas.drawLine(300, 800, 700, 800, paint1);
        canvas.drawLine(700, 400, 700, 800, paint1);
    }
}

有人可以帮我解决这个案子吗?

0 个答案:

没有答案