How to enable pinch zoom on a web view loading a url with pdf(Android)?

前端 未结 3 1206
闹比i
闹比i 2020-12-15 02:30

Pinch zoom works fine on a webView that loads a regular url, if I enable the settings as below,

myWebView.getSettings().setBuiltInZoomControls(true); //this          


        
相关标签:
3条回答
  • 2020-12-15 02:37

    Well instead of using a WebView to show your PDF to the user, maybe using a PDF-View would make more sense. Here's a library I found after googling for it, but I never worked with it so I can't tell how well it works: https://code.google.com/p/apv/

    Edit: Here's another answer with a list of PDF-libs: https://stackoverflow.com/a/6729135/1219113

    0 讨论(0)
  • 2020-12-15 02:43

    Old question, but here's a solution I found.

            WebView mWebView = (WebView) findViewById(R.id.webView);
            mWebView.getSettings().setJavaScriptEnabled(true);
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setBuiltInZoomControls(true);
            webSettings.setSupportZoom(true);
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-15 02:55

    PDFs are notoriously difficult to interact with in Android. You could:

    1.) Roll your own implementation of WebView that supports PDF

    I don't recommend doing it this way. My typical logic is that if Google hasn't done it for a sufficiently long period of time, then it's either very difficult to do, or not very battery efficient. In any case, you may have to get your hands dirty with the Android NDK in order to make your implementation fast and efficient.

    http://gurushya.com/customizing-android-webview/

    2.) Convert your PDF to a series of bitmaps

    For this option, you would need to parse the content of the webpage to find the necessary pdf, convert each slide of your PDF to a bitmap, and display each bitmap in a simple ImageSlider. I haven't had the time to try this method, but it seems like it may work (probably not very quickly though). This stackoverflow answer may help:

    Need help to convert a Pdf page into Bitmap in Android Java

    3.) Follow Convention - Recommended

    This is likely your best option. Using Google Docs is somewhat of a hack. When you pass it a PDF, you're essentially opening up a javascript PDF reader with lousy mobile support. The typical way of handling PDF's in Android is by checking if the user has a PDF reader installed and opening the PDF with that if they do. That PDF reader will more than likely have a pinch to zoom functionality. Android is likely implemented this way for performance reasons and to work across a broader scope of devices.

    Keep in mind that while your average iOS user may not be as used to this; Android users are quite comfortable opening files in other apps. Opening your PDF in a PDF reader gives the user the ability to choose which PDF reader to use. By doing so, you give the reader even more options than just pinch to zoom (such as indexed search, highlighting, quick navigation UI) depending on which app they have installed. If they don't have a PDF reader? Suggest a free one and link them to your Android store of choice!

    Render a PDF file using Java on Android

    0 讨论(0)
提交回复
热议问题