Android:WebView在中心加载图片/内容

时间:2012-05-03 16:09:08

标签: java android webview

我已经加载了WebView的图像,但是当它缩小时,图像最多会放置在我设备的左上角。无论如何加载图像使其显示在中心?

干杯!

8 个答案:

答案 0 :(得分:6)

我是用xml和代码组合完成的。我将我的gif文件存储在assets文件夹中。

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<WebView
    android:id="@+id/webView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true" />

</RelativeLayout>

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    WebView webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewController());
    webView.loadDataWithBaseURL("file:///android_asset/","<html><center><img src=\"animation.gif\"></html>","text/html","utf-8","");
}

private class WebViewController extends WebViewClient {
     @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
}

答案 1 :(得分:4)

您可以使用此代码将图像置于WebView中的屏幕中央:

  //first you will need to find the dimensions of the screen 
  float width;
  float height;
  float currentHeight;
  WebView mWebView;

  //this function will set the current height according to screen orientation
  @Override
  public void onConfigurationChanged(Configuration newConfig){
          if(newConfig.equals(Configuration.ORIENTATION_LANDSCAPE)){

                currentHeight=width; 
                loadImage();                 

         }if(newConfig.equals(Configuration.ORIENTATION_PORTRAIT)){

                currentHeight=height;
                loadImage();

        }
    } 


  //call this function and it will place the image at the center
  public void load and center the image on screen(){

    mWebView=(WebView)findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setBuiltInZoomControls(true);       
    mWebView.setBackgroundColor(0);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    height = displaymetrics.heightPixels;
    width = displaymetrics.widthPixels;
    currentHeight=height             //assuming that the phone
                                     //is held in portrait mode initially

         loadImage();        
  }
  public void loadImage(){
       Bitmap BitmapOfMyImage=BitmapFactory.decodeResource(Environment.getExternalStorgeDirectory().getAbsolutePath()+"yourFolder/myImageName.jpg");  

       mWebView.loadDataWithBaseURL("file:///"+Environment.getExternalStorgeDirectory().getAbsolutePath()
                                    +"yourFolder/","<html><center>
                                    <img src=\"myImageName.jpg\" vspace="
                                    +(currentHeight/2-(BitmapOfMyImage.getHeight()/2))+">
                                     </html>","text/html","utf-8","");
     //This loads the image at the center of thee screen                    

   }

我使用中心标签垂直居中,然后使用vspace标签给出图像上边距。现在保证金的计算方法是:screenVierticalHeight / 2-ImageVerticalHeight / 2

希望这有帮助

答案 2 :(得分:1)

    mWebView.loadDataWithBaseURL("file:///"+Environment.getExternalStorgeDirectory().getAbsolutePath()+"yourFolder/","<html><center><img src=\"myImage.jpg\"></html>","text/html","utf-8","");

这会将SDCard的图像放在webView的中心。我希望这有帮助

答案 3 :(得分:1)

String html = "<html><head><style type='text/css'>html,body {margin: 0;padding: 0;width: 100%;height: 100%;}html {display: table;}body {display: table-cell;vertical-align: middle;text-align: center;}</style></head><body><p style=\"color:white\">+"you message"+</p></body></html>";

webView.loadData(html,“text / html; charset = UTF-8”,null);

因此,这会将您的网页视图加载到中心的文本

答案 4 :(得分:0)

尝试加载嵌入图片的HTML文件。所有布局都可以使用标准的CSS样式完成。

答案 5 :(得分:0)

我遇到了同样的问题 - 在webview中垂直居中。这个解决方案大部分时间都可以工作......也许它可以帮到你一点点

                int pheight = picture.getHeight();
                int vheight = view.getHeight();

                float ratio = (float) vheight / (float) pheight;

                System.out.println("pheight: " + pheight + "px");
                System.out.println("vheight: " + vheight + "px");
                System.out.println("ratio: " + ratio + "px");

                if (ratio > 0.5)
                {
                    return;
                }

                int diff = pheight - vheight;
                int diff2 = (int) ((diff * ratio) / 4);

                if (pheight > vheight)
                {
                    // scroll to middle of webview

                    currentPhotoWebview.scrollTo(0, diff2);
                    System.out.println("scrolling webview by " + diff2 + "px");

                }

答案 6 :(得分:0)

我知道这个答案有点晚了,但这里有一个没有javascript的选项:

您可以扩展WebView并使用受保护的方法computeHorizontalScrollRange()computeVerticalScrollRange()来了解最大滚动范围,并根据此计算scrollTo {{1}的位置类似于vertical:computeHorizontalScrollRange()/2 - getWidth()/2 and

我唯一的建议是确保加载完成之前这样做,我没有测试这是否有效,但事情正在加载,但我不认为它会因为webview还没有它的滚动范围正确设置(当内容仍在加载时)

请参阅:Android webview : detect scroll我从中获得了很多信息。

答案 7 :(得分:0)

对我来说,这是工作:

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "PICTURE FILE NAME";
File file = new File(path);
String html = "<style>img{height: 100%;max-width: 100%;}</style> <html><head></head><body><center><img src=\"" + imagePath + "\"></center></body></html>";
webView.getSettings().setDefaultZoom(ZoomDensity.FAR);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true); 
webView.getSettings().setLoadWithOverviewMode(true);
webView.loadDataWithBaseURL( "" ,  html, "text/html", "UTF-8", "");