使用 ScaleDetector 进行 Android WebView 缩放

发布于 2024-09-08 15:56:53 字数 228 浏览 3 评论 0原文

您好,

当用户捏合手指并将手指移近/移远时,是否可以支持 WebView 的缩放(类似于默认浏览器)。我使用 2.2 并认为使用 ScaleGestureDetector 可以实现这一点。问题是我似乎找不到设置比例的方法。 WebView.getScale() 返回通过 ZoomIn() 和 ZoomOut() 更改的当前比例,但我找不到 setScale() 或默认设置缩放并让操作系统处理它的选项。

安迪

HI,

Is it possible to support zooming of a WebView (similar to the default browser) when the user pinches and moves their fingers closer / further away. Im using 2.2 and tought this would be possible using the ScaleGestureDetector. THe problem is that I can't seem to find a method to set the scale. WebView.getScale() returns the current scale which changes through zoomIn() and ZoomOut() but I can't find setScale() or an option to set zooming by default and let the OS handle it.

Andy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妳是的陽光 2024-09-15 15:56:53

用途:

webview.getSettings().setBuiltInZoomControls(true);

进行捏合缩放并将默认的放大/缩小按钮放在屏幕上。

Use:

webview.getSettings().setBuiltInZoomControls(true);

to do pinch zooming and put the default zoom in/out buttons on the screen.

梅倚清风 2024-09-15 15:56:53

您需要在 WebView 中实现 onTouchEvent(MotionEvent)。

然后在该事件中,将 MotionEvent 传递给 ScaleGestureDetector 以便它可以处理它,然后调用 super.

class ScalableWebView extends WebView implements ScaleGestureDetector.OnScaleGestureListener
{
    ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(this, this);

    public boolean onTouchEvent(MotionEvent event)
    {
        if(mScaleDetector.onTouchEvent(event))
             return true;

        return super.onTouchEvent(event);
    }

    boolean onScaleBegin(ScaleGestureDetector detector) 
    {
     //handle scale event begin
    }

    boolean onScale(ScaleGestureDetector detector) 
    {
     //handle scale event
    }

    boolean onScaleEnd(ScaleGestureDetector detector) 
    {
     //handle scale event end
    }

}

You need to implement the onTouchEvent(MotionEvent) in your WebView.

Then in that event, pass the MotionEvent to the ScaleGestureDetector so that it can process it, then call the super.

class ScalableWebView extends WebView implements ScaleGestureDetector.OnScaleGestureListener
{
    ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(this, this);

    public boolean onTouchEvent(MotionEvent event)
    {
        if(mScaleDetector.onTouchEvent(event))
             return true;

        return super.onTouchEvent(event);
    }

    boolean onScaleBegin(ScaleGestureDetector detector) 
    {
     //handle scale event begin
    }

    boolean onScale(ScaleGestureDetector detector) 
    {
     //handle scale event
    }

    boolean onScaleEnd(ScaleGestureDetector detector) 
    {
     //handle scale event end
    }

}
秋风の叶未落 2024-09-15 15:56:53

有一个 setScale ,试试这个:

webView.setInitialScale((int)(100 * webView.getScale());

There is a setScale , try this :

webView.setInitialScale((int)(100 * webView.getScale());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文