从 url Android 获取小尺寸图片
Getting small size pictures from url Android
我目前正在尝试通过 URL 下载图片,并在我的 Android 应用程序中使用 "fromHtml" 将其放入 TextView 中。由于滑动面板,我正在使用 TextView。我需要同时有文字和图片,求一个好的模板。
我实际上得到了正确的图像,但我不明白为什么它这么小。
我要下载的图片在我制作的 wamp 服务器上,它是一张 400x300 图片。
我正在使用我在 Whosebug 上找到的一段代码从 url 获取图像,它实现了 ImageGetter 和 getIntrinsicWidth() 和 getIntrinsicHeight() 方法,但它们将图片大小除以4 !
我打印了这两个结果,IntrinsicWidth returns 100 和 IntrinsicHeight returns 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
所以基本上我试图在幻灯片面板中同时显示文本和图像,所以如果您对如何修补它有一些想法,或者如果您知道另一种方法来做到这一点,那就太好了。
提前致谢!
getIntrinsicWidth
和 getIntrinsicHeight
方法 return 可绘制对象的大小 应该 在 Android 缩放可绘制对象之后。以编程方式创建可绘制对象会创建一个默认的 mdpi
可绘制对象,因此,如果您随后 运行 在 xxxhdpi 设备上使用此应用程序,这就解释了您的 4 倍比例因子。
Here is a better explanation of how getIntrinsic
functions actually work
我目前正在尝试通过 URL 下载图片,并在我的 Android 应用程序中使用 "fromHtml" 将其放入 TextView 中。由于滑动面板,我正在使用 TextView。我需要同时有文字和图片,求一个好的模板。
我实际上得到了正确的图像,但我不明白为什么它这么小。 我要下载的图片在我制作的 wamp 服务器上,它是一张 400x300 图片。
我正在使用我在 Whosebug 上找到的一段代码从 url 获取图像,它实现了 ImageGetter 和 getIntrinsicWidth() 和 getIntrinsicHeight() 方法,但它们将图片大小除以4 !
我打印了这两个结果,IntrinsicWidth returns 100 和 IntrinsicHeight returns 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
所以基本上我试图在幻灯片面板中同时显示文本和图像,所以如果您对如何修补它有一些想法,或者如果您知道另一种方法来做到这一点,那就太好了。
提前致谢!
getIntrinsicWidth
和 getIntrinsicHeight
方法 return 可绘制对象的大小 应该 在 Android 缩放可绘制对象之后。以编程方式创建可绘制对象会创建一个默认的 mdpi
可绘制对象,因此,如果您随后 运行 在 xxxhdpi 设备上使用此应用程序,这就解释了您的 4 倍比例因子。
Here is a better explanation of how getIntrinsic
functions actually work