How can i load image url to fit my webview in android?

倖福魔咒の 提交于 2019-12-05 03:16:34

问题


I am developing an android application.Here i am displaying a url image in webview. But the image is not fit to my webview. some one is suggested to use viewport concept. But i am not able to use that perfectly. I attached my issue in drawing form with explanation. I used

mWebView.getSettings().setUseWideViewPort (true);

but not get the good result. I need to show the fitted image in all resolution devices including 7inch, 10 inch tablets. So please look at the attached image and suggest me the right way to fit url image in my webview place. I would like to show my image same as in 3rd diagram.

Here is my code for loading image:

webView = (WebView) findViewById(R.id.webimage);
        WebSettings webSettings = imageView.getSettings(); 
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setSupportZoom(true) ;
        webSettings.setBuiltInZoomControls(true);
        webView.setBackgroundColor(Color.TRANSPARENT);
        webView.getSettings().setUseWideViewPort (true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setWebViewClient(new WebViewClient()
        {
        //some dialog implementation
        }
        webView.loadUrl(myurl);


回答1:


you can set width attribute of img to 100% and don't set height attribute

<body >
<img id="resizeImage" src="you_image.png" width="100%" alt="" />
</body>

Edited : Actually i also Having the Same Problem. However Still i am Waiting for the Correct Answer.

you can refere my Question HERE

though i have managed some how this Problem with a Trick by managing height and Width of Device. and Using wv.setInitialScale(185); ByDefault the Zoom will be in Top Left Corner.

i have put my code below.

WindowManager mWinMgr;
    public static int displayWidth = 0, displayHeight = 0;

    @Override
    protected void onCreate(Bundle icicle) {
        // TODO Auto-generated method stub
        super.onCreate(icicle);
        // this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        // this.setProgressBarVisibility(true);
        mWinMgr = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        displayWidth = mWinMgr.getDefaultDisplay().getWidth();
        displayHeight = mWinMgr.getDefaultDisplay().getHeight();

        setContentView(R.layout.view_my_poops);

        try {
            Map_Type = getIntent().getExtras().getString("MAP_TYPE");
        } catch (Exception e) {

        }
        Log.i(TAG, "MAP TYPE" + Map_Type);

    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        WebView wv;

        wv = (WebView) findViewById(R.id.webview_MyPoops);
        wv.getSettings().setJavaScriptEnabled(true);
        // this.setProgressBarVisibility(true);
        if (displayWidth >= 552 && displayHeight >= 976 || displayWidth >= 976
                && displayHeight >= 552) {
            wv.setInitialScale(185);
        } else {
            // wv.setInitialScale(150);
        }
        // wv.setInitialScale(30);


        URL = "YOUR_URL";


        Log.i(TAG, "Loading URL" + URL);

        final Activity activity = ViewMyPoop.this;

        wv.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int progress) {
                // TODO Auto-generated method stub
                // Log.i(TAG, "Inside OnProgress Changed" + URL);
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
                if (progress == 100)
                    activity.setTitle("My title");
            }
        });

        wv.loadUrl(URL);
        wv.setWebViewClient(new WebViewClient());
}

Hope it will Some How Help you.




回答2:


Are u try below code for above problem:-

Display display = getWindowManager().getDefaultDisplay();
int width=display.getWidth();

String data="<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
data=data+"<body><center><img width=\""+width+"\" src=\""+url+"\" /></center></body></html>";
webView.loadData(data, "text/html", null);



回答3:


You can try this:

webView.setInitialScale(30);
WebSettings webSettings = webView.getSettings();
webSettings.setUseWideViewPort(true);

Refer: link



来源:https://stackoverflow.com/questions/13511875/how-can-i-load-image-url-to-fit-my-webview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!