2012-05-03 18 views
10

मैंने WebView के साथ एक छवि लोड की है, लेकिन जब यह ज़ूम आउट हो जाती है तो छवि को अपने डिवाइस के ऊपरी बाएं कोने में अधिकतम स्थान दिया जाता है। क्या छवि को लोड करने के लिए वैसे भी है, तो यह केंद्र में प्रदर्शित होता है?एंड्रॉइड: केंद्र में वेबव्यू लोड छवि/सामग्री

चीयर्स!

उत्तर

3

आप एक 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

आशा इस मदद करता है

+1

आशाजनक लग रहा है, बीमार इसे जांचें और मेरा जवाब बदलें! धन्यवाद! – AndroidXTr3meN

0

एक HTML फ़ाइल लोड करने का प्रयास करें जो इसके बजाय छवि को एम्बेड करता है। सभी लेआउट मानक सीएसएस शैलियों के साथ किया जा सकता है।

+0

मैं करता हूँ, छवियों एचटीएमएल कोड है कि मेरे एसडी कार्ड – AndroidXTr3meN

+0

से एक छवि को लोड करता है आप एचटीएमएल कोड को छोड़ कर सकते हैं? अन्यथा समस्या का अनुमान लगाना मुश्किल है। – alex

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

यह वेबव्यू के केंद्र में एसडीकार्ड से छवि रखता है। मुझे आशा है कि इस मदद करता है

+0

वाह जो आधा रास्ते काम करता है, अब तक धन्यवाद, लेकिन यह केवल क्षैतिज केंद्र और ऊर्ध्वाधर नहीं है। इसे लंबवत केंद्र में संभव है? – AndroidXTr3meN

0

मैं एक ही समस्या थी - वेबव्यू में खड़ी केंद्रित। यह समाधान समय के सबसे अधिक काम करता है ... शायद यह आपको थोड़ा

   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

और यह कोड कहां जाना चाहिए> चालू करें? – AndroidXTr3meN

0

मदद कर सकते हैं मैं जानता हूँ कि क्या यह उत्तर एक छोटे से देर हो चुकी है, लेकिन यहाँ जावास्क्रिप्ट के बिना एक विकल्प है:

आप WebView का विस्तार करने और संरक्षित उपयोग कर सकते हैं तरीकों computeHorizontalScrollRange() और computeVerticalScrollRange() अधिकतम पुस्तक श्रृंखला के एक विचार प्राप्त और कहा कि calculate जहां ऊर्ध्वाधर के लिए इसी तरह computeHorizontalScrollRange()/2 - getWidth()/2 and साथ scrollTo करना चाहते हैं के आधार पर करने के लिए: computeVerticalScrollRange()/2 - getHeight()/2

अपने ही सलाह लोड हो रहा है ऐसा करने से पहले पूरा हो गया है यह सुनिश्चित करें कि बनाने के लिए हो सकता है, मैं अगर यह लोड हो रहा है, तो यह काम नहीं करता है, लेकिन मुझे नहीं लगता कि यह वेबवेव में अभी तक स्क्रॉल रेंज सही ढंग से सेट नहीं होगा (क्योंकि सामग्री अभी भी लोड हो रही है)

देखें: Android webview : detect scroll जहां मुझे मिला से मेरी बहुत सारी जानकारी।

6

मैंने इसे एक्सएमएल और कोड के संयोजन के साथ किया। मेरे पास मेरी gif फ़ाइल संपत्ति फ़ोल्डर में संग्रहीत है।

<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 (एचटीएमएल, "text/html; charset = UTF-8", नल);

तो यह मध्य में पाठ के साथ अपने वेब दृश्य लोड होगा

संबंधित मुद्दे