如何使用多点触控绘制圆圈 [英] how to draw circle using Multitouch

查看:74
本文介绍了如何使用多点触控绘制圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了有关Multitouch&的信息.我有很多与之相关的例子

i had searched about Multitouch & i got lots of example related to

放大&缩小&

pich zoom in & zoom out &

获取多个触摸坐标,但是

getting multiple touch coordinates but

我想使用多点触控在视图上绘制一个圆..我尝试了此操作,但是当我绘制它时,坐标的变化并没有在我触摸屏幕的确切位置绘制圆.

i want to use multitouch to draw a circle on a view.. i tried this but when i draw it the coordinates changes the circle didn't draw at the exact place where i touch the screen..

这是代码....

public class TestMultiTouchActivity extends Activity {
public class MultiTouchView extends View {

    // In this test, handle maximum of 2 pointer
    final int MAX_POINT_CNT = 2;

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    float[] x = new float[MAX_POINT_CNT];
    float[] y = new float[MAX_POINT_CNT];
    boolean[] isTouch = new boolean[MAX_POINT_CNT];

    public MultiTouchView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        if (isTouch[0]) {
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(1);
            paint.setColor(Color.RED);
            // canvas.drawCircle(x[0], y[0], 50f, paint);
        }
        if (isTouch[1]) {
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(1);
            paint.setColor(Color.BLUE);
            // canvas.drawCircle(x[1], y[1], 50f, paint);
        }
        float x1 = (Math.abs((x[0] - x[1]))) / 2;
        float y1 = (Math.abs((y[0] - y[1]))) / 2;

        float r = Math.abs(x1 - x[0]);
        canvas.drawCircle(x1, y1, r, paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
                MeasureSpec.getSize(heightMeasureSpec));
    }

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

        int pointerIndex = ((motionEvent.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT);
        int pointerId = motionEvent.getPointerId(pointerIndex);
        int action = (motionEvent.getAction() & MotionEvent.ACTION_MASK);
        int pointCnt = motionEvent.getPointerCount();

        if (pointCnt <= MAX_POINT_CNT) {
            if (pointerIndex <= MAX_POINT_CNT - 1) {

                for (int i = 0; i < pointCnt; i++) {
                    int id = motionEvent.getPointerId(i);
                    x[id] = (int) motionEvent.getX(i);
                    y[id] = (int) motionEvent.getY(i);
                }

                switch (action) {
                case MotionEvent.ACTION_DOWN:
                    isTouch[pointerId] = true;
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    isTouch[pointerId] = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    isTouch[pointerId] = true;
                    break;
                case MotionEvent.ACTION_UP:
                    isTouch[pointerId] = false;
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    isTouch[pointerId] = false;
                    break;
                case MotionEvent.ACTION_CANCEL:
                    isTouch[pointerId] = false;
                    break;
                default:
                    isTouch[pointerId] = false;
                }
            }
        }

        invalidate();
        return true;
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MultiTouchView(this));
}

推荐答案

尝试此操作以计算圆的大小/位置:

Try this for calculating the size/position of the circle:

// Calculate average x and y.
float x1 = (x[0] + x[1]) / 2.0f;
float y1 = (y[0] + y[1]) / 2.0f;

// Calculate the distance between the points
float r = Math.sqrt(Math.pow(x[1] - x[0], 2.0f) + Math.pow(y[1] - y[0], 2.0f) / 2.0f;

canvas.drawCircle(x1, y1, r, paint);

这篇关于如何使用多点触控绘制圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆