排队缩放事件时遇到问题:
ScaleGestureDetector.OnScaleGestureListener fingerScaleGestureListener = new ScaleGestureDetector.OnScaleGestureListener() {
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
//GESTURE finger scale-end
Log.d(TAG+".scale.end", Float.toString(detector.getScaleFactor()));
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
//GESTURE finger scale-begin
Log.d(TAG+".scale.begin", Float.toString(detector.getScaleFactor()));
return true;
}
@Override
public boolean onScale(final ScaleGestureDetector detector) {
//GESTURE finger onscale
Log.d(TAG+".scale.on", Float.toString(detector.getScaleFactor()));
glSurfaceView.queueEvent(new Runnable() {
@Override
public void run() {
renderer.zoom(detector.getScaleFactor());
}
});
return true;
}
};
根据LogCat输出,它工作正常。
renderer.zoom()的实现:
public void zoom(float ratio)
{
Log.d(TAG+".zoom", Float.toString(ratio));
float dx = eyeX - centerX;
float dy = eyeY - centerY;
float dz = eyeZ - centerZ;
dx*=ratio;
dy*=ratio;
dz*=ratio;
eyeX = centerX + dx;
eyeY = centerY + dy;
eyeZ = centerZ + dz;
Matrix.perspectiveM(projectionMatrix, 0, fovy, aspect, zNear, zFar);
Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
Matrix.multiplyMM(MVPMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
}
缩放手势期间的LogCat:
04-09 19:57:47.057: D/MainActivity.scale.begin(21916): 1.0
04-09 19:57:47.072: D/MainActivity.scale.on(21916): 1.0305684
04-09 19:57:47.072: D/GLRenderer.zoom(21916): 1.0
04-09 19:57:47.087: D/MainActivity.scale.on(21916): 1.0367821
04-09 19:57:47.092: D/GLRenderer.zoom(21916): 1.0
04-09 19:57:47.102: D/MainActivity.scale.on(21916): 1.0497419
04-09 19:57:47.107: D/GLRenderer.zoom(21916): 1.0
04-09 19:57:47.122: D/MainActivity.scale.on(21916): 1.0489701
04-09 19:57:47.122: D/GLRenderer.zoom(21916): 1.0
04-09 19:57:47.137: D/MainActivity.scale.on(21916): 1.035543
04-09 19:57:47.137: D/GLRenderer.zoom(21916): 1.0
04-09 19:57:47.147: D/MainActivity.scale.end(21916): 1.0
为什么缩放功能始终为1.0f?
答案 0 :(得分:0)
您依靠detector.getScaleFactor()
获取在您的情况下不正确的比例因子:)
getScaleFactor()
返回相对于上次调用onScale()
解决方案就像维护float
成员变量(初始化为1.0f)一样简单,只需要像
public void run() {
mScale *= detector.getScaleFactor();
renderer.zoom(mScale);
}