2012-05-03 19 views

Odpowiedz

3

Można użyć tego kodu, aby wyśrodkować obraz na środku ekranu w 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      

    } 

Użyłem znacznik środka do środka obrazu w pionie, a następnie użył vSpace tag, aby nadać najwyższy margines obrazu. Teraz margines jest obliczana przez: screenVierticalHeight/2/2-ImageVerticalHeight

nadzieję, że to pomaga

+1

Wygląda obiecująco, źle to sprawdź i zmień moją odpowiedź! Dzięki! – AndroidXTr3meN

0

Spróbuj załadować plik HTML, w którym osadzony jest obraz. Cały układ może być wykonany przy użyciu standardowych stylów CSS.

+0

zrobić, obraz jest kod HTML, który ładuje obraz z mojej karty SD – AndroidXTr3meN

+0

można upuścić kod HTML? w przeciwnym razie trudno jest odgadnąć problem. – alex

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

Powoduje umieszczenie obrazu z karty SDCard w środku widoku webView. Mam nadzieję, że to pomoże

+0

Wowow, które pracują w połowie drogi, dzięki do tej pory, ale to tylko centrum poziomej, a nie pionowej. Czy jest możliwe wyśrodkowanie go w pionie? – AndroidXTr3meN

0

miałem ten sam problem - centrowanie w pionie WebView. To rozwiązanie działa przez większość czasu ... być może to pomoże wam trochę

   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

Gdzie powinien znajdować się ten kod> onCreate? – AndroidXTr3meN

0

wiem, ta odpowiedź jest trochę późno, ale tutaj jest to opcja bez javascript:

można rozszerzyć WebView i użyć chroniony metody computeHorizontalScrollRange() i computeVerticalScrollRange() aby zorientować się w maksymalnej odległości przewijania i na jej podstawie oblicz którym chcesz scrollTo z computeHorizontalScrollRange()/2 - getWidth()/2 and podobnie dla pionie: computeVerticalScrollRange()/2 - getHeight()/2

moim jedynym rozwiązaniem byłoby, aby upewnić się, że ładowanie jest zakończone przed tym, ja nie testowałem, jeśli to działa, gdy rzeczy ładują się, ale nie sądzę, aby tak się stało, ponieważ przeglądarka nie ma jeszcze prawidłowo ustawionego zakresu przewijania (ponieważ treść jest nadal wczytywana)

patrz: Android webview : detect scroll gdzie mam dużo moich informacji od.

6

Zrobiłem to z połączeniem xml i kodu. Mój plik gif jest przechowywany w folderze zasobów.

<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

Dla mnie to jest praca:

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

Więc to będzie załadować webview z tekstem w centrum