如何根据屏幕密度缩放webview

时间:2014-02-06 12:15:14

标签: android webview

在我的webview应用程序中,我使用stInitialScale()进行缩放。我使它适合于screendensity 240 * 400,我认为当使用屏幕密度超过此值的设备时,它会自动进行缩放。但是webview仍然是相同的缩放级别。我可以使webview适合具有不同屏幕密度的任何设备。 User_reg.java

package com.example.samworkshops;

import android.os.Bundle;
import android.os.SystemClock;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class User_reg extends Activity {
 private WebView webView;
 final Activity activity = this;


@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.activity_user);
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


        webView = (WebView)findViewById(R.id.webView1);
        webView.requestFocusFromTouch();
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);                                                         
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.setHorizontalScrollBarEnabled(false);

        webView.setInitialScale(90);

        webView.loadUrl("http://app.samworkshops.org/User_Reg.aspx");

        webView.setVisibility(View.INVISIBLE);
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.title_activity_user_reg);
            }
        });

        webView.setBackgroundColor(Color.parseColor("black"));


        webView.setWebViewClient(new WebViewClient() {


            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                StringBuilder builder = new StringBuilder("");


                    builder.append("javascript:document.getElementById('Image3').style.visibility= 'hidden';");
                    builder.append("javascript:document.getElementById('Image3').style.display   = 'none'  ;");
view.loadUrl(builder.toString());

                webView.setVisibility(View.VISIBLE);
                SystemClock.sleep(2000);

            }


        });

    }

}

3 个答案:

答案 0 :(得分:1)

将这些行添加到Web视图以启用缩放缩放

     webview.getSettings().setSupportZoom(true);
     webview.getSettings().setBuiltInZoomControls(true);

OR

您必须为每个结构设计布局。

参考链接 http://developer.android.com/guide/practices/screens_support.html

答案 1 :(得分:0)

Try this may be it will solve you problem

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        int width = display.getWidth();
        int height = display.getHeight();
        if (height > 800) {  //Put value as per your requirements 

                   //Set Zooming value
        } else {
                       //Set Zooming value

        }

答案 2 :(得分:0)

switch(this.getResources().getDisplayMetrics().densityDpi)
        {
        case DisplayMetrics.DENSITY_LOW:

            MUL_RATIO = 0.75f;
            break;

        case DisplayMetrics.DENSITY_MEDIUM:

            MUL_RATIO = 1.0f;
            break;

        case DisplayMetrics.DENSITY_HIGH:

            MUL_RATIO = 1.5f;
            break;

        case DisplayMetrics.DENSITY_XHIGH:

            MUL_RATIO = 2.0f;
            break;
        }

放置你想要的缩放级别因素。它可以帮助你