2012-05-03 27 views
10

におけるWebViewの負荷画像/内容私はWebViewで画像をロードしているが、それは私のデバイスの左上隅に画像の場所そのもの最大のズームアウトされたとき。とにかく画像をロードして中央に表示されますか?アンドロイド:中央

乾杯!

答えて

3

に役立ちます願っています:

//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を使用していましたタグを使用してイメージの上端を指定します。 WebViewの中で垂直方向にもセンタリング - これは私が同じ問題を抱えていた

+1

有望に見えて、病気でそれをチェックし、私の答えを変えてください!ありがとう! – AndroidXTr3meN

0

イメージを埋め込むHTMLファイルを読み込みます。すべてのレイアウトは標準的なCSSスタイルで行うことができます。

+0

、画像はあなたがHTMLコードをドロップすることができ、私のSDカード – AndroidXTr3meN

+0

からイメージをロードするHTMLコードが中央にテキストを使用してのWebViewをロードしますか?そうでなければ問題を推測することは困難です。 – alex

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

これは、SDCardの画像をwebViewの中央に配置します。私はあなたがWebViewの中に、画面の中央に画像を中央に、このコードを使用することができ、これは

+0

wowowは、半分の道をworkds、今までのおかげで、それだけ水平と垂直ではない中心です。それを垂直にセンタリングすることは可能ですか? – AndroidXTr3meN

0

を助けscreenVierticalHeight/2-ImageVerticalHeight/2

希望:今すぐマージンをして算出されます。あなたはWebViewのを拡張し、保護を使用することができます

:このソリューションは、多分それは私はこの答えは少し遅れだが、ここではJavaScriptなしのオプションを知っているあなたに少し

   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"); 

       } 
+0

そして、このコードはどこに行きますか?onCreate? – AndroidXTr3meN

0

を助けることができる...最も時間の作品方法computeHorizontalScrollRange()computeVerticalScrollRange()最大スクロール範囲のアイデアを取得し、あなたが垂直方向についても同様にcomputeHorizontalScrollRange()/2 - getWidth()/2 andscrollToすることを計算に基づいてする:computeVerticalScrollRange()/2 - getHeight()/2

私の唯一のアドバイスはロードがそうする前に完了していることを確認するだろう、私物事が読み込まれている間にこれが動作するかどうかはテストされていませんが、webviewはまだスクロール範囲が正しく設定されていないと思います(コンテンツがまだ読み込まれているので)。

参照:Android webview : detect scrollからの私の多くの情報。

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; 
     } 
} 
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", ""); 
0
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);

だから、これは私が