使用带触摸坐标的Rect

时间:2016-07-22 22:50:06

标签: android canvas coordinates draw multi-touch

每当我将第二根手指放在屏幕上时,在我的应用程序中。我应该能够在第二根手指的触摸点显示此post或以下图像中显示的视图中心。

enter image description here

我正在使用下面显示的代码。我面临的问题是主要区域的中心不是第二次触摸手指。

 public OnTouchListener mTouchlistener = new OnTouchListener() {


@TargetApi(Build.VERSION_CODES.M)
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int pointerIndex = event.getActionIndex();

                // get pointer ID
                int pointerId = event.getPointerId(pointerIndex);

                // get masked (not specific to a pointer) action
                int maskedAction = event.getActionMasked();

    //            Log.d("Multitouch","intialised");
                if (event.getPointerCount() > 2)
                    multi_touch = false;
                switch (maskedAction) {

                    case MotionEvent.ACTION_DOWN:


                        break;

                    case MotionEvent.ACTION_POINTER_DOWN:
                        multi_touch = true;

                        PointF f = new PointF();
                        f.x = event.getX(pointerIndex);
                        f.y = event.getY(pointerIndex);
                        setTouchDownPoint(f.x,  f.y);

                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;

                }
                invalidate();
                return true;
            }
       };
        public void setTouchDownPoint(float x1, float y1) {
            touchDownX = x1;
            touchDownY = y1;
        }
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (multi_touch == false)
        return;
    Rect textBounds = new Rect();
    Paint mInnerTextPaint = new Paint();
    mInnerTextPaint.setColor(Color.WHITE);
    mInnerTextPaint.setAntiAlias(true);
    mInnerTextPaint.setTextAlign(Paint.Align.CENTER);


    mInnerTextPaint.setTextSize(50);

    float squareSize = (float)0.3 * Math.min(getWidth(), getHeight()) / 7;
    float length = 6* squareSize;
    //draw main area
    canvas.drawRect(new RectF(touchDownX-length , touchDownY-length , touchDownX  + length, touchDownY + length), mainAreaPaint);

    //draw top squares
    canvas.drawRect(new RectF(2 * squareSize, 0, 3 * squareSize, squareSize), paints[0]);
    canvas.drawRect(new RectF(3 * squareSize, 0, 4 * squareSize, squareSize), paints[1]);
    canvas.drawRect(new RectF(4 * squareSize, 0, 5 * squareSize, squareSize), paints[2]);

    //draw right squares
    canvas.drawRect(new RectF(6 * squareSize, 2 * squareSize, 7 * squareSize, 3 * squareSize), paints[3]);
    canvas.drawRect(new RectF(6 * squareSize, 3 * squareSize, 7 * squareSize, 4 * squareSize), paints[4]);
    canvas.drawRect(new RectF(6 * squareSize, 4 * squareSize, 7 * squareSize, 5 * squareSize), paints[5]);

    //draw bottom squares
    canvas.drawRect(new RectF(4 * squareSize, 6 * squareSize, 5 * squareSize, 7 * squareSize), paints[6]);
    canvas.drawRect(new RectF(3 * squareSize, 6 * squareSize, 4 * squareSize, 7 * squareSize), paints[7]);
    canvas.drawRect(new RectF(2 * squareSize, 6 * squareSize, 3 * squareSize, 7 * squareSize), paints[8]);

    //draw left squares
    canvas.drawRect(new RectF(0, 4 * squareSize, squareSize, 5 * squareSize), paints[9]);
    canvas.drawRect(new RectF(0, 3 * squareSize, squareSize, 4 * squareSize), paints[10]);
    canvas.drawRect(new RectF(0, 2 * squareSize, squareSize, 3 * squareSize), paints[11]);

}

如何构建上面显示的图像。这样它的中心出现在秒针的位置?

0 个答案:

没有答案