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

Java Android:WebView在中心加载图像/内容,java,android,webview,Java,Android,Webview,我已经用WebView加载了一幅图像,但是当它被最大化缩小时,图像会自动放在我的设备的左上角。是否仍要加载图像以使其显示在中心 干杯 尝试加载嵌入图像的HTML文件。然后可以使用标准css样式完成所有布局。mWebView.loadDataWithBaseURL(“文件://”+Environment.getExternalStorgeDirectory().getAbsolutePath()+“yourFolder/”,“,”文本/html”,“utf-8“,”); mWebView.

我已经用
WebView
加载了一幅图像,但是当它被最大化缩小时,图像会自动放在我的设备的左上角。是否仍要加载图像以使其显示在中心


干杯

尝试加载嵌入图像的HTML文件。然后可以使用标准css样式完成所有布局。

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

这会将SD卡中的图像放置在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                    

   }
//首先需要找到屏幕的尺寸
浮动宽度;
浮动高度;
浮动高度;
网络视图;
//此功能将根据屏幕方向设置当前高度
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
if(newConfig.equals(Configuration.ORIENTATION\u横向)){
当前高度=宽度;
loadImage();
}if(newConfig.equals(Configuration.ORIENTATION\u纵向)){
当前高度=高度;
loadImage();
}
} 
//调用此函数,它会将图像置于中心
公共无效加载屏幕上的图像并将其居中(){
mWebView=(WebView)findViewById(R.id.WebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().SetBuilTinZoomControl(true);
mWebView.setBackgroundColor(0);
DisplayMetrics DisplayMetrics=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
高度=displaymetrics.heightPixels;
宽度=displaymetrics.widthPixels;
currentHeight=高度//假设手机
//最初以纵向模式保持
loadImage();
}
public void loadImage(){
位图BitmapOfMyImage=BitmapFactory.decodeResource(Environment.getExternalStorgeDirectory().getAbsolutePath()+“yourFolder/myImageName.jpg”);
mWebView.loadDataWithBaseURL(“文件://“+Environment.getExternalStorgeDirectory().getAbsolutePath())
+“您的文件夹/,”
“,”文本/html“,”utf-8“,”);
//这将在屏幕中央加载图像
}
我使用了中心标记垂直居中图像,然后使用vspace标记给出图像的上边距。现在,边距的计算公式为:screenVierticalHeight/2-ImageVerticalHeight/2


希望这有帮助

我也有同样的问题-在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");

                }

我知道这个答案有点晚,但这里有一个不使用javascript的选项:

您可以扩展WebView,并使用受保护的方法
computeHorizontalScrollRange()
computeVerticalScrollRange()
来了解最大滚动范围,并基于该范围,使用
computeHorizontalScrollRange()/2-getWidth()计算要滚动到的位置/2和
与垂直方向类似:
computeVerticalScrollRange()/2-getHeight()/2

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


请看:我从中获得了很多信息。

我是通过xml和代码的组合来实现的。我的gif文件存储在资产文件夹中

以下是xml布局代码:

<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>

以下是java代码:

@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;
        }
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u网络视图);
WebView WebView=(WebView)findViewById(R.id.WebView);
setWebViewClient(新的WebViewController());
webView.loadDataWithBaseURL(“file:///android_asset/“,”,“文本/html”,“utf-8”和“);
}
私有类WebViewController扩展WebViewClient{
@凌驾
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
view.loadUrl(url);
返回true;
}
}
对我来说,这就是工作:

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", "");
String path=Environment.getExternalStorageDirectory().getAbsolutePath()+“图片文件名”;
文件=新文件(路径);
字符串html=“img{高度:100%;最大宽度:100%;}”;
webView.getSettings().setDefaultZoom(ZoomDensity.FAR);
webView.getSettings().setBuilTinZoomControl(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverview模式(true);
loadDataWithBaseURL(“,html,“text/html”,“UTF-8”,等等);
String html=“html,正文{margin:0;padding:0;宽度:100%;高度:100%;}html{display:table;}正文{display:table单元格;垂直对齐:中间;文本对齐:中间;}

+“您的消息”+

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


因此,这将用中间的文本加载您的Web视图

I do,图像是从我的SD卡加载图像的html代码您可以删除html代码吗?否则很难猜出问题所在。哇哦,这是成功的一半,谢谢你,但它只是水平居中,而不是垂直居中。有可能垂直居中吗?看起来很有希望,我会检查一下并更改我的答案!谢谢
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>";