大家好,我正在为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的移动设备上正确显示,重新缩放必须在代码中完成,而不是在任何图像编辑器中进行。 提前感谢!