我有点绝望=)......
我正在Android中的画布上绘制点和线。我正在显示的点应该在数学系统中显示。所以我做了canvas.getHeight() - point.y以正确的方式显示点。
但是如果我想放大绘制的对象,y坐标会从我的视图中缩小。 这是因为x = 10且y = 700.如果我用比例因子10来缩放它,则y使对象消失。 我希望你能得到我所说的......
如何在不移动y坐标的情况下以正确的(数学)方式显示坐标?
以下是我的工作:
canvas.drawPoint(startPoints[i][0], height-startPoints[i][1], pointColor);
canvas.drawPoint(endPoints[i][0], height-endPoints[i][1], pointColor);
然后我的观点(x = 10,y = 10)将显示为x = 10,y = 714。
这是我的完整例程
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
final float[][] startPoints;
final float[][] endPoints;
int count = 0;
height = canvas.getHeight();
canvas.save();
canvas.scale(scaleFactor, scaleFactor);
canvas.translate(translateX / scaleFactor, translateY / scaleFactor);
Paint lineColor = new Paint();
lineColor.setColor(Color.BLACK);
Paint pointColor = new Paint();
pointColor.setColor(Color.RED);
pointColor.setStrokeWidth(5f);
startPoints = data.getStartPoints();
endPoints = data.getEndPoints();
count = data.getSize();
for (int i = 0; i < count; i++) {
canvas.drawLine(startPoints[i][0], height-startPoints[i][1],
endPoints[i][0], height-endPoints[i][1], lineColor);
canvas.drawPoint(startPoints[i][0], height-startPoints[i][1], pointColor);
canvas.drawPoint(endPoints[i][0], height-endPoints[i][1], pointColor);
}
canvas.restore();
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
return true;
}
}
答案 0 :(得分:0)
缩放系数x =以像素为单位的屏幕宽度/以像素为单位的目标宽度
缩放系数y =屏幕高度(以像素为单位)/目标高度(以像素为单位)
要么
在调整大小的画布上缩放x点= x *(调整宽度/原始宽度)
在调整大小的画布上缩放y point = y *(调整大小/原始高度)