代码之家  ›  专栏  ›  技术社区  ›  jbabey

重新缩放PNG的自定义方法会丢失透明度

  •  5
  • jbabey  · 技术社区  · 16 年前

    嘿,伙计们,我正在为支持java的手机开发一个j2ME游戏。我尝试用以下方法缩放透明PNG:

    // method derived from a Snippet from http://snippets.dzone.com/posts/show/3257
    // scales an image according to the ratios given as parameters
    
    private Image rescaleImage(Image image, double XRatio, double YRatio)
    {
        // the old height and width
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();
    
        // what the new height and width should be
        int newWidth = (int)(XRatio * sourceWidth);
        int newHeight = (int)(YRatio * sourceHeight);
    
        Image newImage = Image.createImage(newWidth, newHeight);
        Graphics g = newImage.getGraphics();
    
        for (int y = 0; y < newHeight; y++)
        {
            for (int x = 0; x < newWidth; x++)
            {
                g.setClip(x, y, 1, 1);
                int dx = (x * sourceWidth) / newWidth;
                int dy = (y * sourceHeight) / newHeight;
                g.drawImage(image, (x - dx), (y - dy), Graphics.LEFT | Graphics.TOP);
            }
        }
        return Image.createImage(newImage);
    }
    

    任何 支持java的移动设备,重缩放需要在代码中完成,而不是在任何类型的图像编辑器中。

    3 回复  |  直到 16 年前
        1
  •  4
  •   jbabey    16 年前

    感谢每一位一直在寻找解决这一看似广泛且尚未解决的问题的方法的人。我设法找到了一个很好的解决办法 http://willperone.net/Code/codescaling.php

    初始化画布时,XRatio和YRatio声明为常量,其中XRatio=this.getWidth()(当前手机的屏幕宽度)除以原始背景图像宽度,YRatio使用getHeight()/原始背景图像高度。

    // RESCALEIMAGE
    // scales an image according to the ratios given as parameters
    // derived from http://willperone.net/Code/codescaling.php
    
    public static Image rescaleImage(Image original, double XRatio, double YRatio)
    {
        // the original width and height
        int originalWidth = original.getWidth();
        int originalHeight = original.getHeight();
    
        // the target width and height
        int newWidth = (int)(XRatio * originalWidth);
        int newHeight = (int)(YRatio * originalHeight);
    
        // create and fill the pixel array from the original image
        int[] rawInput = new int[originalHeight * originalWidth];
        original.getRGB(rawInput, 0, originalWidth, 0, 0, originalWidth, originalHeight);
    
        // pixel array for the target image
        int[] rawOutput = new int[newWidth*newHeight];
    
        // YD compensates for the x loop by subtracting the width back out
        int YD = (originalHeight / newHeight) * originalWidth - originalWidth;
        int YR = originalHeight % newHeight;
        int XD = originalWidth / newWidth;
        int XR = originalWidth % newWidth;
        int outOffset= 0;
        int inOffset=  0;
    
        for (int y = newHeight, YE = 0; y > 0; y--)
        {
            for (int x = newWidth, XE = 0; x > 0; x--)
            {
                rawOutput[outOffset++] = rawInput[inOffset];
    
                inOffset += XD;
                XE += XR;
    
                if (XE >= newWidth)
                {
                    XE -= newWidth;
                    inOffset++;
                }
            }
    
            inOffset += YD;
            YE += YR;
    
            if (YE >= newHeight)
            {
                YE -= newHeight;
                inOffset += originalWidth;
            }
        }
        return Image.createRGBImage(rawOutput, newWidth, newHeight, true);
    }
    
        2
  •  1
  •   Yonathan    16 年前

    可能是因为你没有区分像素值中的alpha。你能做的就是添加一个额外的例程来处理那些带有alpha的,这样它们就可以保持它们可能的alpha值。

        3
  •  0
  •   Mike Tunnicliffe    16 年前

    可能是图像类的方法:

    Image   getScaledInstance(int width, int height, int hints) 
    

    适合你的需要吗。。?

    http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/awt/Image.html

    推荐文章