Android:如何实现连续缩放功能
我正在寻找一种实现连续缩放的方法。连续我的意思是我想缩放到一个位置并从那里缩放到另一个位置,依此类推。就像在浏览器中一样。
到目前为止,我的方法只能进行一次缩放,并在下一次缩放之前重置。
首先,我计算手指与手指之间的中点之间的距离。
public boolean onTouchEvent(MotionEvent event) {
if(mode == ZOOM) {
float newDist = spacing(event);
scale = newDist / oldDist;
midPoint(midPoint, event);
invalidate();
}
invalidate 方法调用 onDraw,我告诉矩阵进行缩放。
protected void onDraw(Canvas canvas) {
matrix = canvas.getMatrix();
matrix.postScale(scale, scale, midPoint.x, midPoint.y);
canvas.setMatrix(matrix);
// drawing code
}
有没有更好的累积方法?
I am looking for a way to implement a continously zoom. With continously I mean that I want to zoom to one position and zoom from there to another and so on. Like in the browser.
My aproach so far is only capable of a one time zoom and resets before the next.
First I calculate the distance between the fingers and the mid point inbetween.
public boolean onTouchEvent(MotionEvent event) {
if(mode == ZOOM) {
float newDist = spacing(event);
scale = newDist / oldDist;
midPoint(midPoint, event);
invalidate();
}
The method invalidate calls the onDraw where I tell the matrix to do the scaling.
protected void onDraw(Canvas canvas) {
matrix = canvas.getMatrix();
matrix.postScale(scale, scale, midPoint.x, midPoint.y);
canvas.setMatrix(matrix);
// drawing code
}
Is there a better cumulating aproach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要动画缩放,则必须在计时器中计算动画。这意味着随着时间的推移,您可以从一个矩阵转换为另一个矩阵,使视图无效,并在 onDraw 中应用生成的矩阵。
这样您就可以从一个缩放级别平滑过渡到另一个缩放级别。
If you want animated zooming, you must compute the animation in a Timer. This means that over time you transform from one matrix to another, invalidate view, and apply resulting matrix in onDraw.
This way ou get smooth transition from one zoom level to another.