问题
In my app whenever I place my second finger on the Screen. I should be able to show the center of the view shown in this post or below image at the point of touch of second finger.
I am using the code shown below. The problem I face is the center of the mainarea is not at the second finger touch.
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]);
}
How can I construct the image shown above. so that it centre appears at the point of second finger?
来源:https://stackoverflow.com/questions/38536295/using-rect-with-touch-coordinates