我目前正在尝试通过URL下载一张图片,并使用我的Android应用程序中的"fromHtml“将其放到一个fromHtml中。我之所以使用TextView是因为它是一种粘糊糊的东西。我需要同时有文字和图片,试图有一个好的模板。
我得到了正确的图像,但我不明白为什么它这么小。我正在尝试下载的图片是在一个wamp服务器上,我已经制作了一个400x300图片。
我正在使用在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);下面是我的安卓应用程序中的400x300图片的样子:400x300结果
下面是7360x4912图像的样子:7360x4912结果
基本上,我试着把文字和图像同时放在幻灯片上,所以如果你对如何修补这个问题有一些想法,或者如果你知道另一种方法的话,那就太棒了。
提前谢谢!
发布于 2016-02-25 22:29:42
getIntrinsicWidth和getIntrinsicHeight方法返回可绘制的大小,应该是在Android之后绘制的。以编程的方式创建一个可绘制的mdpi,因此,如果您要在xxxhdpi设备上运行这个应用程序,这就解释了您的4x缩放因子。
https://stackoverflow.com/questions/35639354
复制相似问题