Pink Jazz
Pink Jazz

Reputation: 802

Android - How to center Path on Canvas to screen with a given X and Y coordinate as the center

I have the following code to draw a path to a Canvas:

if (mPointArray.size() > 1) {
    mPath.moveTo(mPointArray.get(0).x * scaleX, mPointArray.get(0).y * scaleY);
    for (int x = 1; x < mPointArray.size(); x++) {
        mPath.lineTo(mPointArray.get(x).x * scaleX, mPointArray.get(x).y * scaleY);
    }
    canvas.drawPath(mPath, mPaint);    
    canvas.drawPoint(mPointArray.get(mPointArray.size() - 1).x * scaleX, mPointArray.get(mPointArray.size() - 1).y * scaleY, pointPaint);
}

Say I were to have an arbitrary X and Y coordinate as a designated center, how would I apply that X and Y coordinate to the center of the screen? The reason why I am asking is because I am developing an app where you stand on a sensor where you stand on a specific location, and I would like my path to be centered around that initial point before drawing the path. I have two float values, centerX and centerY that I would somehow like to apply to my path. Unfortunately, if I apply them before multiplying by scaleX and scaleY, the translation is too large, while if I do it afterwards, the translation is too small.

Here is the declaration for scaleX:

scaleX = ((screenW / sensorWidth) * 1.1f) - ((screenW / sensorWidth) * 0.1f);

scaleY is definied similarly in terms of screenH and sensorLength.

How would I apply the new center coordinates of the sensor, with the new center being centered on the screen?

Upvotes: 0

Views: 1366

Answers (2)

Pink Jazz
Pink Jazz

Reputation: 802

I finally got it: mPath.offset((centerX - (sensorWidth / 2)) * -scaleX, (centerY - (sensorLength / 2)) * -scaleY);

Upvotes: 0

j3App
j3App

Reputation: 1582

Not sure I got your question, but this formula looks sick:

scaleX = ((screenW / sensorWidth) * 1.1f) - ((screenW / sensorWidth) * 0.1f);

A simpler version of this would be:

scaleX = screenW / sensorWidth;

Upvotes: 1

Related Questions