Example usage for android.webkit WebView setInitialScale

List of usage examples for android.webkit WebView setInitialScale

Introduction

In this page you can find the example usage for android.webkit WebView setInitialScale.

Prototype

public void setInitialScale(int scaleInPercent) 

Source Link

Document

Sets the initial scale for this WebView.

Usage

From source file:com.sonnychen.aviationhk.views.MetarFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.v("METAR-UI", "Starting");
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_metar, container, false);
    //        ((TextView) view.findViewById(R.id.metar)).setText(BaseApplication.Data.METAR_Code);
    WebView metar = ((WebView) view.findViewById(R.id.metar_html));
    metar.setInitialScale(1);
    metar.getSettings().setJavaScriptEnabled(false);
    metar.getSettings().setLoadWithOverviewMode(true);
    metar.getSettings().setUseWideViewPort(true);
    metar.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    metar.setScrollbarFadingEnabled(false);
    metar.loadDataWithBaseURL("", BaseApplication.Data.METAR_HTML + "<p><p>" + BaseApplication.Data.TAF_HTML
            + "<p><pre>" + BaseApplication.Data.SIGMET_Code + "</pre>", "text/html", "UTF-8", "");
    //        ((TextView) view.findViewById(R.id.taf)).setText(BaseApplication.Data.TAF_Code);
    //        ((TextView) view.findViewById(R.id.sigmet)).setText(BaseApplication.Data.SIGMET_Code);
    return view;//from ww  w.  j  a va 2  s  .  c o m
}

From source file:org.noise_planet.noisecapture.MapActivity.java

public void loadWebView() {
    WebView leaflet = (WebView) findViewById(R.id.webmapview);
    WebSettings webSettings = leaflet.getSettings();
    webSettings.setJavaScriptEnabled(true);
    leaflet.clearCache(true);//w  w  w .j av  a  2 s .  co  m
    leaflet.setInitialScale(200);
    String location = "";
    if (builder != null && validBoundingBox) {
        LatLng latLng = builder.build().getCenter();
        location = "/#18/" + latLng.latitude + "/" + latLng.longitude;
    }
    leaflet.loadUrl("http://onomap.noise-planet.org" + location);
}

From source file:com.nttec.everychan.ui.gallery.GalleryActivity.java

private void setWebView(final GalleryItemViewTag tag, final File file) {
    runOnUiThread(new Runnable() {
        private boolean oomFlag = false;

        private final ViewGroup.LayoutParams MATCH_PARAMS = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        private void prepareWebView(WebView webView) {
            webView.setBackgroundColor(Color.TRANSPARENT);
            webView.setInitialScale(100);
            webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
                CompatibilityImpl.setScrollbarFadingEnabled(webView, true);
            }/*from   www .  j  ava  2  s .co  m*/

            WebSettings settings = webView.getSettings();
            settings.setBuiltInZoomControls(true);
            settings.setSupportZoom(true);
            settings.setAllowFileAccess(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
                CompatibilityImpl.setDefaultZoomFAR(settings);
                CompatibilityImpl.setLoadWithOverviewMode(settings, true);
            }
            settings.setUseWideViewPort(true);
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                CompatibilityImpl.setBlockNetworkLoads(settings, true);
            }

            setScaleWebView(webView);
        }

        private void setScaleWebView(final WebView webView) {
            Runnable callSetScaleWebView = new Runnable() {
                @Override
                public void run() {
                    setPrivateScaleWebView(webView);
                }
            };

            Point resolution = new Point(tag.layout.getWidth(), tag.layout.getHeight());
            if (resolution.equals(0, 0)) {
                // wait until the view is measured and its size is known
                AppearanceUtils.callWhenLoaded(tag.layout, callSetScaleWebView);
            } else {
                callSetScaleWebView.run();
            }
        }

        private void setPrivateScaleWebView(WebView webView) {
            Point imageSize = getImageSize(file);
            Point resolution = new Point(tag.layout.getWidth(), tag.layout.getHeight());

            //Logger.d(TAG, "Resolution: "+resolution.x+"x"+resolution.y);
            double scaleX = (double) resolution.x / (double) imageSize.x;
            double scaleY = (double) resolution.y / (double) imageSize.y;
            int scale = (int) Math.round(Math.min(scaleX, scaleY) * 100d);
            scale = Math.max(scale, 1);
            //Logger.d(TAG, "Scale: "+(Math.min(scaleX, scaleY) * 100d));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
                double picdpi = (getResources().getDisplayMetrics().density * 160d) / scaleX;
                if (picdpi >= 240) {
                    CompatibilityImpl.setDefaultZoomFAR(webView.getSettings());
                } else if (picdpi <= 120) {
                    CompatibilityImpl.setDefaultZoomCLOSE(webView.getSettings());
                } else {
                    CompatibilityImpl.setDefaultZoomMEDIUM(webView.getSettings());
                }
            }

            webView.setInitialScale(scale);
            webView.setPadding(0, 0, 0, 0);
        }

        private Point getImageSize(File file) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            return new Point(options.outWidth, options.outHeight);
        }

        private boolean useFallback(File file) {
            String path = file.getPath().toLowerCase(Locale.US);
            if (path.endsWith(".png"))
                return false;
            if (path.endsWith(".jpg"))
                return false;
            if (path.endsWith(".gif"))
                return false;
            if (path.endsWith(".jpeg"))
                return false;
            if (path.endsWith(".webp") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
                return false;
            return true;
        }

        @Override
        public void run() {
            try {
                recycleTag(tag, false);
                WebView webView = new WebViewFixed(GalleryActivity.this);
                webView.setLayoutParams(MATCH_PARAMS);
                tag.layout.addView(webView);
                if (settings.fallbackWebView() || useFallback(file)) {
                    prepareWebView(webView);
                    webView.loadUrl(Uri.fromFile(file).toString());
                } else {
                    JSWebView.setImage(webView, file);
                }
                tag.thumbnailView.setVisibility(View.GONE);
                tag.loadingView.setVisibility(View.GONE);
                tag.layout.setVisibility(View.VISIBLE);
            } catch (OutOfMemoryError oom) {
                System.gc();
                Logger.e(TAG, oom);
                if (!oomFlag) {
                    oomFlag = true;
                    run();
                } else
                    showError(tag, getString(R.string.error_out_of_memory));
            }
        }

    });
}

From source file:nya.miku.wishmaster.ui.GalleryActivity.java

private void setWebView(final GalleryItemViewTag tag, final File file) {
    runOnUiThread(new Runnable() {
        private boolean oomFlag = false;

        private final ViewGroup.LayoutParams MATCH_PARAMS = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        private void prepareWebView(WebView webView) {
            webView.setBackgroundColor(Color.TRANSPARENT);
            webView.setInitialScale(100);
            webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
                CompatibilityImpl.setScrollbarFadingEnabled(webView, true);
            }//ww  w  .j  av  a  2s. c  o m

            WebSettings settings = webView.getSettings();
            settings.setBuiltInZoomControls(true);
            settings.setSupportZoom(true);
            settings.setAllowFileAccess(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
                CompatibilityImpl.setDefaultZoomFAR(settings);
                CompatibilityImpl.setLoadWithOverviewMode(settings, true);
            }
            settings.setUseWideViewPort(true);
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                CompatibilityImpl.setBlockNetworkLoads(settings, true);
            }

            setScaleWebView(webView);
        }

        private void setScaleWebView(final WebView webView) {
            Runnable callSetScaleWebView = new Runnable() {
                @Override
                public void run() {
                    setPrivateScaleWebView(webView);
                }
            };

            Point resolution = new Point(tag.layout.getWidth(), tag.layout.getHeight());
            if (resolution.equals(0, 0)) {
                // wait until the view is measured and its size is known
                AppearanceUtils.callWhenLoaded(tag.layout, callSetScaleWebView);
            } else {
                callSetScaleWebView.run();
            }
        }

        private void setPrivateScaleWebView(WebView webView) {
            Point imageSize = getImageSize(file);
            Point resolution = new Point(tag.layout.getWidth(), tag.layout.getHeight());

            //Logger.d(TAG, "Resolution: "+resolution.x+"x"+resolution.y);
            double scaleX = (double) resolution.x / (double) imageSize.x;
            double scaleY = (double) resolution.y / (double) imageSize.y;
            int scale = (int) Math.round(Math.min(scaleX, scaleY) * 100d);
            scale = Math.max(scale, 1);
            //Logger.d(TAG, "Scale: "+(Math.min(scaleX, scaleY) * 100d));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
                double picdpi = (getResources().getDisplayMetrics().density * 160d) / scaleX;
                if (picdpi >= 240) {
                    CompatibilityImpl.setDefaultZoomFAR(webView.getSettings());
                } else if (picdpi <= 120) {
                    CompatibilityImpl.setDefaultZoomCLOSE(webView.getSettings());
                } else {
                    CompatibilityImpl.setDefaultZoomMEDIUM(webView.getSettings());
                }
            }

            webView.setInitialScale(scale);
            webView.setPadding(0, 0, 0, 0);
        }

        private Point getImageSize(File file) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            return new Point(options.outWidth, options.outHeight);
        }

        private boolean useFallback(File file) {
            String path = file.getPath().toLowerCase(Locale.US);
            if (path.endsWith(".png"))
                return false;
            if (path.endsWith(".jpg"))
                return false;
            if (path.endsWith(".gif"))
                return false;
            if (path.endsWith(".jpeg"))
                return false;
            if (path.endsWith(".webp") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
                return false;
            return true;
        }

        @Override
        public void run() {
            try {
                recycleTag(tag, false);
                WebView webView = new WebViewFixed(GalleryActivity.this);
                webView.setLayoutParams(MATCH_PARAMS);
                tag.layout.addView(webView);
                if (settings.fallbackWebView() || useFallback(file)) {
                    prepareWebView(webView);
                    webView.loadUrl(Uri.fromFile(file).toString());
                } else {
                    JSWebView.setImage(webView, file);
                }
                tag.thumbnailView.setVisibility(View.GONE);
                tag.loadingView.setVisibility(View.GONE);
                tag.layout.setVisibility(View.VISIBLE);
            } catch (OutOfMemoryError oom) {
                MainApplication.freeMemory();
                Logger.e(TAG, oom);
                if (!oomFlag) {
                    oomFlag = true;
                    run();
                } else
                    showError(tag, getString(R.string.error_out_of_memory));
            }
        }

    });
}