我只是想知道是否有某种类可以使用 android 2.1 的多点触控功能。具体来说,我正在尝试实现捏缩放,并且想知道我是否总是必须测量两个触摸事件之间的距离并自己计算缩放级别?
谢谢,克里斯
我只是想知道是否有某种类可以使用 android 2.1 的多点触控功能。具体来说,我正在尝试实现捏缩放,并且想知道我是否总是必须测量两个触摸事件之间的距离并自己计算缩放级别?
谢谢,克里斯
我正在尝试做同样的事情,像往常一样,我的第一直觉是查看 Android 源代码本身。有趣的部分似乎在ScaleGestureDetector类中,它不是公开的,但它的 javadoc 说
@hide 等待 API 批准
所以希望它会在某个时候公开。
更新:ScaleGestureDetector现在是 Android 2.2 API 的一部分。
我相信您需要自己计算缩放级别。这篇文章看起来像是一个很好的资源来帮助你入门: http: //blogs.zdnet.com/Burnette/ ?p=1847
这取决于您要定位的 Android 版本。
为了避免在 2.0 之前的设备上使用多点触控 API,您需要为 ScaleGestureDetector 创建一个接口(Eclipse 可以通过 Refactor 菜单执行此操作),以及一个 1.x 设备将使用的虚拟实现。我们将调用我们的接口ScaleGestureDetectorInterface
和我们的虚拟实现FakeScaleGestureDetector
。
这是一个支持 2.0 之前的设备的示例:
// If you don't care about pre-2.0 devices, just make this a
// ScaleGestureDetector and skip the API check in the constructor.
private final ScaleGestureDetectorInterface mScaleDetector;
public MyView {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ECLAIR) {
// Use the fake version which won't call any multitouch APIs
mScaleDetector = new FakeScaleGestureDetector();
} else {
// We are using SDK 2.0+, use the real implementation.
mScaleDetector = new ScaleGestureDetector(context,
new MyScaleListener());
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// On pre-2.0, the implementation does nothing.
return mScaleDetector.onTouchEvent(event);
}
private class MyScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureInterface detector) {
float scaleFactor = detector.getScaleFactor();
// If you were using a matrix to zoom an ImageView, you would do
// something like this:
mMatrix.postScale(scaleFactor, scaleFactor, detector.getFocusX(),
detector.getFocusY());
return true;
}
}
开发人员已经创建了在浏览器和 Dolphin 浏览器上启用多点触控的黑客。这些来自自定义 rom,我相信它们是可下载的。
此外,谷歌在 Nexus One 和摩托罗拉的里程碑上正式发布了多点触控。这意味着您应该能够为它获得一个官方课程,但我敢打赌它适用于 Android 2.1 版。
此外,我认为可以安全地假设您希望它在有根手机上工作。这意味着您可能会被困在使用 Android 2.1 并且可能一直使用到 2.0。
你想放大图片吗?作为一个简单的解决方案,您可以使用 WebView 来显示您的图像,它具有内置的缩放功能。