1

我有一张画布,我在中心显示一张图像。此图像是通过 url 下载的。实际上还有更多的图片要下载,这意味着如果用户点击右我必须显示下一张图片如果左,我必须从背面显示另一张图片。我有存储url图像的字符串数组。我想在后台下载上一张和下一张图片。怎么做?

4

2 回答 2

1

以下是您需要考虑的一些问题,以使此要求适用于各种设备

  1. 如果图像比设备画布尺寸大怎么办?也就是说,与画布 w * h 相比,图像尺寸 w * h 非常高?
  2. 低内存设备可以加载这么大的图像吗?
  3. 跨设备的多线程模型不一致。这是一些设备,它将允许您创建 10 个线程,而在其他设备中,它将停止在 2 个。
  4. 我的应用程序可以支持获取图像类型吗?那就是我正在下载 .JPG / .GIF 但我的设备不支持它。
  5. 如何缓存图像?当您导航到该图像时,您想一次又一次地加载相同的图像吗?
  6. 我是否使用 TCP/HTTP/RMI 来下载图像内容?
  7. 这段代码是否可以跨不同风格的 JVM for JavaME 移植?

名单还在继续......

牢记上述一组问题,您将对您的问题提出建设性的答案是创建一个网络 IO 管理器和一个缓存管理器。

interface NetworkIoItem {
     Object sourceComponent;

     public void onImageDownload(Image image) {
           //Trigger the source 
     }
}

.

class NetworkIoManager extends Threads {
     Vector pendingRequestQueue;

     :
     :

     public void run() {
           //Wait on the pending request queue

           //Process all the elements in pending request queue
           //and again wait on this queue
     }
}

.

class CacheManager {
     Vector cachedContent;

     public boolean checkIfCached() {
          //Check in the cachedContent whether
          //this image exists if so return true;
     }

     public Image fetchImage() {
          //Check if the image is cached
          //if so return this cached image

          //else push a NetworkIoItem object to 
          //NetworkIOManager pending queue
     }
}

现在对于每个图像(当前、左侧或右侧)调用CacheManager.fetchImage(),此方法将负责为您提供缓存或从服务器下载的图像。在相同的方法中,如果图像没有被缓存,它会将对象添加NetworkIoItemNetworkIoManager pendingQueue并下载它。下载完成后会触发NetworkIoItem.onImageDownload(...)方法。

您可以使用J2ME Polish 的 Touch功能在NetworkIoManager

通过使用这种方法,您将对请求 URL 进行异步图像提取。

于 2011-12-08T18:10:47.903 回答
0

做类似..

Thread t = new Thread(new Runnable(){
  public void run() {
    // Your code to download image here as you were doing earlier,
    // for previous and next image
  }
});

t.start();
于 2011-12-08T17:43:42.953 回答