![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
j2me中怎样更具不同的屏幕来放大或缩小一个图片的显示
比如在200*180的手机屏幕中,有一张180*160的图片,我想让他按不同的比例缩小(放大)显示在大小不同的手机屏幕中,改怎么做...
比如在200*180的手机屏幕中,有一张180*160的图片,我想让他按不同的比例缩小(放大)显示在大小不同的手机屏幕中,改怎么做
展开
2个回答
展开全部
需要一个函数来改变图片大小,我给你提供一个:
public static Image scaleImage(Image src, int dstW, int dstH) {
int srcW = src.getWidth();
int srcH = src.getHeight();
Image tmp = Image.createImage(dstW, srcH);
Graphics g = tmp.getGraphics();
int delta = (srcW << 16) / dstW;
int pos = delta / 2;
for (int x = 0; x < dstW; x++) {
g.setClip(x, 0, 1, srcH);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += delta;
}
Image dst = Image.createImage(dstW, dstH);
g = dst.getGraphics();
delta = (srcH << 16) / dstH;
pos = delta / 2;
for (int y = 0; y < dstH; y++) {
g.setClip(0, y, dstW, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += delta;
}
return dst;
}
使用很简单,比如你原来的图片为 img1 :
Image img2 = scaleImage(img1, 200, 180);
img2 即成为一张把 img1 放大到 200*180 的图片。
public static Image scaleImage(Image src, int dstW, int dstH) {
int srcW = src.getWidth();
int srcH = src.getHeight();
Image tmp = Image.createImage(dstW, srcH);
Graphics g = tmp.getGraphics();
int delta = (srcW << 16) / dstW;
int pos = delta / 2;
for (int x = 0; x < dstW; x++) {
g.setClip(x, 0, 1, srcH);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += delta;
}
Image dst = Image.createImage(dstW, dstH);
g = dst.getGraphics();
delta = (srcH << 16) / dstH;
pos = delta / 2;
for (int y = 0; y < dstH; y++) {
g.setClip(0, y, dstW, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += delta;
}
return dst;
}
使用很简单,比如你原来的图片为 img1 :
Image img2 = scaleImage(img1, 200, 180);
img2 即成为一张把 img1 放大到 200*180 的图片。
![](https://ecmc.bdimg.com/public03/b4cb859ca634443212c22993b0c87088.png)
2024-11-14 广告
标定板认准大凡光学科技,专业生产研发厂家,专业从事光学影像测量仪,光学投影测量仪.光学三维测量仪,光学二维测量仪,光学二维测量仪,光学三维测量仪,光学二维测量仪.的研发生产销售。东莞市大凡光学科技有限公司创立于 2018 年,公司总部坐落于...
点击进入详情页
本回答由东莞大凡提供
展开全部
因为 me中api没有带图片缩放算法 如果你想程序自适屏那么图片缩放很重要 所以……
自己写的……
public static Image ZoomImage(Image image ,int newWidth, int newHeight){
int imageWidth,imageHeight;
int[] temp ,RGB;
float x,y;
imageWidth = image.getWidth();
imageHeight = image.getHeight();
temp = new int[imageWidth*imageHeight];
RGB = new int[newWidth*newHeight];
try{
image.getRGB(temp, 0, imageWidth, 0, 0, imageWidth, imageHeight);
x = imageWidth/(float)newWidth; //计算倍率
y = imageHeight/(float)newHeight;
for(int i = 0;i<newHeight;i++){
for(int j=0;j<newWidth;j++){
RGB[i*newWidth+j] = temp[imageWidth*(int)((i*y))+(int)(j*x)];
}
}
}catch(Exception e){
e.printStackTrace();
}
return Image.createRGBImage(RGB, newWidth, newHeight, true);
}
其实这个很简单……
这个算法采用数据计算,比调用系统绘制来得跟根本跟高效!
速度远比绘制图片的速度快!
自己写的……
public static Image ZoomImage(Image image ,int newWidth, int newHeight){
int imageWidth,imageHeight;
int[] temp ,RGB;
float x,y;
imageWidth = image.getWidth();
imageHeight = image.getHeight();
temp = new int[imageWidth*imageHeight];
RGB = new int[newWidth*newHeight];
try{
image.getRGB(temp, 0, imageWidth, 0, 0, imageWidth, imageHeight);
x = imageWidth/(float)newWidth; //计算倍率
y = imageHeight/(float)newHeight;
for(int i = 0;i<newHeight;i++){
for(int j=0;j<newWidth;j++){
RGB[i*newWidth+j] = temp[imageWidth*(int)((i*y))+(int)(j*x)];
}
}
}catch(Exception e){
e.printStackTrace();
}
return Image.createRGBImage(RGB, newWidth, newHeight, true);
}
其实这个很简单……
这个算法采用数据计算,比调用系统绘制来得跟根本跟高效!
速度远比绘制图片的速度快!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询