2012-05-03 48 views

Trả lời

3

Bạn có thể sử dụng mã này để tập trung hình ảnh ở trung tâm của màn hình trong một 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      

    } 

Tôi đã sử dụng thẻ trung tâm để tập trung hình ảnh theo chiều dọc và sau đó sử dụng các vspace để tạo hình ảnh trên cùng. Bây giờ bên lề được tính bằng cách: screenVierticalHeight/2-ImageVerticalHeight/2

Hope this helps

+1

Có vẻ đầy hứa hẹn, hãy kiểm tra nó và thay đổi câu trả lời của tôi! Cảm ơn! – AndroidXTr3meN

0

Hãy thử tải tệp HTML nhúng hình ảnh thay thế. Tất cả các bố trí có thể được thực hiện với các phong cách tiêu chuẩn css sau đó.

+0

tôi làm, những hình ảnh là mã html mà tải một hình ảnh từ thẻ SD của tôi – AndroidXTr3meN

+0

bạn có thể thả mã html? thật khó để đoán được vấn đề khác. – alex

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

Hình ảnh địa điểm này từ SDCard ở giữa webView. Tôi hy vọng điều này sẽ giúp

+0

wowow rằng workd một nửa cách, cảm ơn cho đến nay nhưng nó chỉ trung tâm ngang và không thẳng đứng. Có thể căn giữa nó theo chiều dọc không? – AndroidXTr3meN

0

tôi đã cùng một vấn đề - tập trung theo chiều dọc trong webview. Giải pháp này làm việc hầu hết thời gian ... có lẽ nó có thể giúp bạn một chút

   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

Và mã này nên ở đâu> onCreate? – AndroidXTr3meN

0

Tôi biết câu trả lời này là hơi muộn nhưng đây là một lựa chọn mà không cần javascript:

bạn có thể mở rộng WebView và sử dụng được bảo vệ phương pháp computeHorizontalScrollRange()computeVerticalScrollRange() để có được một ý tưởng về phạm vi cuộn tối đa và trên cơ sở đó tính toán nơi bạn muốn scrollTo với computeHorizontalScrollRange()/2 - getWidth()/2 and tương tự cho dọc: computeVerticalScrollRange()/2 - getHeight()/2

lời khuyên duy nhất của tôi sẽ là để đảm bảo rằng tải hoàn tất trước khi làm như vậy, tôi chưa thử nghiệm nếu điều này làm việc trong khi mọi thứ đang tải nhưng tôi không nghĩ rằng nó sẽ là webview sẽ không có phạm vi di chuyển của nó một cách chính xác thiết lập (như nội dung vẫn đang được nạp)

xem: Android webview : detect scroll nơi tôi có rất nhiều thông tin của tôi từ.

6

Tôi đã làm điều đó với sự kết hợp của xml và mã. Tôi có tập tin gif của tôi được lưu trữ trong thư mục tài sản.

<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

Đối với tôi đó là công việc:

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

Vì vậy, đây sẽ được tải webview của bạn với các văn bản trong trung tâm

Các vấn đề liên quan