Java me 如何在j2me(java)中设置图像高度和宽度
我已从图像url(lcdui图像)创建了一个图像Java me 如何在j2me(java)中设置图像高度和宽度,java-me,lcdui,Java Me,Lcdui,我已从图像url(lcdui图像)创建了一个图像 我想将高度和宽度设置为此?我想显示您不需要设置宽度和高度,因为在图像加载过程中会加载和设置此信息。因此,如果映像是320x100,那么代码将创建一个320x100映像。 img.getWidth()将返回320img.getHeight()将返回100 无法更改图像对象的宽度和高度。您可以只查询其宽度和高度 您的图像已准备好显示在画布中的ImageItem对象中。您无法设置图像的宽度和高度。但是,您可以使用以下方法调整图像的大小 public I
我想将高度和宽度设置为此?我想显示您不需要设置宽度和高度,因为在图像加载过程中会加载和设置此信息。因此,如果映像是320x100,那么代码将创建一个320x100映像。
img.getWidth()
将返回320img.getHeight()
将返回100
无法更改图像
对象的宽度和高度。您可以只查询其宽度和高度
您的图像已准备好显示在画布中的
ImageItem
对象中。您无法设置图像的宽度和高度。但是,您可以使用以下方法调整图像的大小
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;
}
public Image resizeImage(Image src、int-screenHeight、int-screenWidth){
int srcWidth=src.getWidth();
int srchheight=src.getHeight();
Image tmp=Image.createImage(屏幕宽度、高度);
Graphics g=tmp.getGraphics();
整数比=(srcWidth>16),0);
pos+=比率;
}
Image resizedImage=Image.createImage(屏幕宽度、屏幕高度);
g=resizedImage.getGraphics();
比率=(SRC高度>16));
pos+=比率;
}
返回resizedImage;
}
接受的答案对我来说不起作用(因为缩小图像尺寸时,它在图像底部留下了一条白色带,尽管保持了相同的纵横比)。我发现了一个可以从
以下是已清理的片段:
protected static Image resizeImage(Image image, int resizedWidth, int resizedHeight) {
int width = image.getWidth();
int height = image.getHeight();
int[] in = new int[width];
int[] out = new int[resizedWidth * resizedHeight];
int dy, dx;
for (int y = 0; y < resizedHeight; y++) {
dy = y * height / resizedHeight;
image.getRGB(in, 0, width, 0, dy, width, 1);
for (int x = 0; x < resizedWidth; x++) {
dx = x * width / resizedWidth;
out[(resizedWidth * y) + x] = in[dx];
}
}
return Image.createRGBImage(out, resizedWidth, resizedHeight, true);
}
受保护的静态图像大小图像(图像图像、int-resizedWidth、int-resizedHeight){
int width=image.getWidth();
int height=image.getHeight();
int[]in=新的int[宽度];
int[]out=new int[resizedWidth*resizedhight];
int-dy,dx;
对于(int y=0;y
上面的代码(未编译)可能取自本页的示例,该示例确实编译,但在我的测试中,没有正确调整图像大小,因为它在图像底部留下了一条白带。
protected static Image resizeImage(Image image, int resizedWidth, int resizedHeight) {
int width = image.getWidth();
int height = image.getHeight();
int[] in = new int[width];
int[] out = new int[resizedWidth * resizedHeight];
int dy, dx;
for (int y = 0; y < resizedHeight; y++) {
dy = y * height / resizedHeight;
image.getRGB(in, 0, width, 0, dy, width, 1);
for (int x = 0; x < resizedWidth; x++) {
dx = x * width / resizedWidth;
out[(resizedWidth * y) + x] = in[dx];
}
}
return Image.createRGBImage(out, resizedWidth, resizedHeight, true);
}