【问题标题】:What is the right way to manage image assets for J2ME in NetBeans在 NetBeans 中管理 J2ME 图像资产的正确方法是什么
【发布时间】:2013-11-18 00:38:55
【问题描述】:

我正在使用 NetBeans 开发一个跨许多不同设备运行的 J2ME 应用程序。该应用程序使用了许多不同的图像资产。由于设备具有不同的屏幕尺寸,这意味着我需要编译多个二进制文件,每个文件具有不同的资产大小。

到目前为止,我一直在使用手动流程来控制资产。我有一个由一堆子目录组成的目录,每个子目录对应于特定类别的设备所需的资产。例如,我有一个目录“320_240”,其资产大小适用于 320x240 屏幕,另一个目录“480_360”,其资产大小适用于 480x360 屏幕。文件名与加载它们的代码完全相同。在编译之前,我只是将正确的文件复制到默认包中(在 src 下)。

这显然可以改进。我已经有代表不同屏幕尺寸的不同项目配置,所以我也想让资产自动切换。作为 NetBeans 的新手,我不确定最好的方法是什么。

FWIW,这是我想出的最好的:

  1. 创建资产。 src下的包,其中LABEL对应设备类(例如“320_240”,“480_360”)
  2. 将每个类的图片放到合适的 src/asset/ 目录中
  3. 根据当前选择的项目配置创建一个静态的最终字符串assetDir,将其设置为“/asset//”
  4. 使用 Image.creatImage(assetDir + "image.png") 加载图像
  5. 对于每个配置,仅在 Project->Build->Sources Filtering 中包含必要的资产目录(我认为这是避免将未使用的图像存储在已编译的应用程序中的必要条件,对吗?)

不过,这仍然让人觉得有点做作。这必须是一个普遍的问题。谁有更好的解决方案?

谢谢!

【问题讨论】:

    标签: netbeans java-me


    【解决方案1】:

    如果您使用大量图像,则 jar 文件的大小会增加。你不能在一些低端设备上安装那个 jar。

    只使用一张图片,并根据屏幕宽度和屏幕高度调整图片大小。

    要调整图像大小,请使用以下方法。

    public Image resizeImage(Image src, int screenHeight, int screenWidth) {
            int srcWidth = src.getWidth();
    
            int srcHeight = src.getHeight();
            Image tmp = Image.createImage(screenWidth, srcHeight);
            Graphics g = tmp.getGraphics();
            int ratio = (srcWidth << 16) / screenWidth;
            int pos = ratio / 2;
    
            //Horizontal Resize        
    
            for (int index = 0; index < screenWidth; index++) {
                g.setClip(index, 0, 1, srcHeight);
                g.drawImage(src, index - (pos >> 16), 0);
                pos += ratio;
            }
    
            Image resizedImage = Image.createImage(screenWidth, screenHeight);
            g = resizedImage.getGraphics();
            ratio = (srcHeight << 16) / screenHeight;
            pos = ratio / 2;
    
            //Vertical resize
    
            for (int index = 0; index < screenHeight; index++) {
                g.setClip(0, index, screenWidth, 1);
                g.drawImage(tmp, 0, index - (pos >> 16));
                pos += ratio;
            }
            return resizedImage;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 2019-05-26
      • 2011-06-13
      • 1970-01-01
      相关资源
      最近更新 更多