以下是我的自定义字段的绘制方法:
protected void paint(Graphics graphics) {
new downloadImage();
Bitmap img = downloadImage.connectServerForImage(this.poster);
graphics.drawBitmap(0, 0, 100, 150, img, LEFT, TOP);
}
这是connectServerForImage方法:
public static Bitmap connectServerForImage(String url) {
HttpConnection httpConnection = null;
DataOutputStream httpDataOutput = null;
InputStream httpInput = null;
int rc;
Bitmap bitmp = null;
try {
httpConnection = (HttpConnection) Connector.open(url);
rc = httpConnection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
httpInput = httpConnection.openInputStream();
InputStream inp = httpInput;
byte[] b = IOUtilities.streamToBytes(inp);
EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
return hai.getBitmap();
} catch (Exception ex) {
System.out.println("URL Bitmap Error........" + ex.getMessage());
} finally {
try {
if (httpInput != null)
httpInput.close();
if (httpDataOutput != null)
httpDataOutput.close();
if (httpConnection != null)
httpConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return bitmp;
}
我将自定义字段的多个实例放入verticalfieldmanager中,但图像会减慢向下滚动的速度。好像每次滚动它都会再次运行paint方法,即使图像已经下载了。
我想我需要在另一个帖子中下载图片?有人引导我朝着正确的方向前进。
答案 0 :(得分:3)
- 您必须在单独的线程中启动任何HTTP交互(无阻塞调用)。
- 避免多次下载同一图像缓存已下载的图像。您可以使用图像下载URL作为图像标记进行存储 它们。
- 每次在屏幕上显示事件时,都会调用paint方法。因此,如果图像已经下载,请不要开始下载。
醇>