2016-02-25 144 views
0

我目前正在尝试通过URL下载图片并将其放入TextView中,使用我的Android应用程序中的“fromHtml”。由于滑动面板,我使用了TextView。我需要同时拥有文字和图片,试图建立一个好的模板。从url获取小尺寸图片Android

我得到了正确的图像,但我不明白为什么它如此之小。 我试图下载的图片是在一台我已经做出了400x300图片的wamp服务器上。

我在StackOverflow上使用了一段代码,从实现ImageGetter和getIntrinsicWidth()和getIntrinsicHeight()方法的url中获取图像,但它们将图片大小除以4!

我已经打印出这两个结果,IntrinsicWidth返回100,IntrinsicHeight返回75 ...所以它在我的应用程序中显示一个非常小的图片,我真的不知道为什么,我无法弄清楚它..

我试过使用7360x4912图片,并且在这个尺寸下,我能够在TextView中获得适当的图片尺寸(但仍然被4除以获得1840x1228图片)。

我也试图通过4乘这些值,但它只是放大的容器,而不是内容...

这里是我的Java代码示例:

@Override 
    protected void onPostExecute(Drawable result) { 
     // set the correct bound according to the result from HTTP call 
     Log.d("IMAGE DEBUG", ""+result.getIntrinsicWidth()); 
     Log.d("IMAGE DEBUG", ""+result.getIntrinsicHeight()); 
     int width = result.getIntrinsicWidth(); 
     int height = result.getIntrinsicHeight(); 

     urlDrawable.setBounds(0, 0, width, height); 

     // change the reference of the current drawable to the result 
     // from the HTTP call 
     urlDrawable.drawable = result; 

     URLImageParser.this.container.setMinimumHeight(result.getIntrinsicHeight()); 
     URLImageParser.this.container.requestLayout(); 

     // redraw the image by invalidating the container 
     URLImageParser.this.container.invalidate(); 
    } 

    /*** 
    * Get the Drawable from URL 
    * @param urlString 
    * @return 
    */ 
    public Drawable fetchDrawable(String urlString) { 
     try { 
      Log.d("IMAGE DEBUG", "" + urlString); 
      InputStream is = fetch(urlString); 
      Drawable drawable = Drawable.createFromStream(is, "src"); 
      drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 
        + drawable.getIntrinsicHeight()); 
      return drawable; 
     } catch (Exception e) { 
      return null; 
     } 
    } 

这里是哪里使用的图片,我得到:

TextView panel = (TextView) findViewById(R.id.slidingpanelview); 
      URLImageParser p = new URLImageParser(panel, getApplicationContext()); 

      String name = queryResults.get(mHashMap.get(marker)).getName(); 
      String address = queryResults.get(mHashMap.get(marker)).getAddress(); 
      String phonenumber = queryResults.get(mHashMap.get(marker)).getPhoneNumber(); 

      panel.setText(""); 
      String titleDisplay = "<table><tr><h2 align=\"center\">"+name+"</h2></tr>"; 
      String addressDisplay = "<tr><h3 align=\"left\">Address</h3><p>"+address+"</p></tr>"; 
      String phonenumberDisplay = "<tr><h3 align=\"left\">Phone number</h3><p>"+phonenumber+"</p></tr>"; 
      String space ="<tr></tr>"; 
      String imageDisplay = "<tr><img src=\"http://ip_address_from_server/img/"+name+".jpg\"></tr></table>"; 
      Spanned htmlSpan = Html.fromHtml(titleDisplay + addressDisplay + phonenumberDisplay + space + imageDisplay, p, null); 
      //Spanned htmlSpan = Html.fromHtml(titleDisplay + addressDisplay + phonenumberDisplay + space + imageDisplay); 
      panel.setText(htmlSpan); 

这里是什么样子,在我的Android应用程序的400x300的图像: 400x300 result

这里是什么样子与7360x4912的图像: 7360x4912 result

所以基本上我想有在一个滑动屏幕,同时文字和图片,因此,如果您有关于如何一些想法修补这个,或者如果你知道另一种方法来做到这一点,那将是很棒的。

提前致谢!

回答

0

getIntrinsicWidthgetIntrinsicHeight方法返回的大小绘制应该是在Android后缩放drawable。以编程方式创建drawable将创建一个默认的mdpi drawable,因此,如果您正在xxxhdpi设备上运行此应用程序,这将解释您的4x比例因子。

Here is a better explanation of how getIntrinsic functions actually work

+0

哦,我明白了!非常感谢你的解释! 其实这是真的,我直接在我的三星Galaxy S6有2560 x 1440分辨率开发这个应用程序。 所以我必须找到我可以如何改变我得到的drawable的指标? – Lucien

+0

是的,你可以通过调用getResources()。getDisplayMetrics()。density –