首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >黑莓:在自定义字段中下载图像会降低VerticalFieldManager速度

问黑莓:在自定义字段中下载图像会降低VerticalFieldManager速度
EN

Stack Overflow用户
提问于 2011-12-27 08:35:35
回答 1查看 184关注 0票数 0

下面是我的自定义字段的paint方法:

代码语言:javascript
复制
   protected void paint(Graphics graphics) {

            new downloadImage();
            Bitmap img =  downloadImage.connectServerForImage(this.poster);


            graphics.drawBitmap(0, 0, 100, 150, img, LEFT, TOP);
     }

下面是connectServerForImage方法:

代码语言:javascript
复制
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方法,即使图像已经下载。

我在想,我需要在另一个线程中下载图像吗?谁来给我指引一个正确的方向。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-27 12:29:04

  1. 您必须在单独的线程中启动任何HTTP交互(无阻塞调用)。
  2. 为避免多次下载同一图像,请缓存已下载的图像。您可以使用图片下载URL作为图片标签进行存储,
  3. 每次屏幕上有事件时,都会调用paint方法。因此,如果图像已经下载,请不要开始下载。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8640190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档