?? imagemidlet.java
字號:
package com.j2medev.chapter3.splash;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ImageMidlet extends MIDlet implements CommandListener{
private Image img = null;
private Display display = null;
private List main = new List("Image",List.IMPLICIT);
private Command exitCommand = new Command("Exit",Command.EXIT,1);
private Command backCommand = new Command("back",Command.BACK, 2);
public ImageMidlet(){
if(display == null){
display = Display.getDisplay(this);
try{
img = Image.createImage("/baby.png");
}catch(IOException ex){
ex.printStackTrace();
}
//創建縮略圖
main.append("Lovely Baby",createThumbnail(img, 40));
main.addCommand(exitCommand);
main.setCommandListener(this);
}
}
public void startApp() {
display.setCurrent(main);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
//借助Clipping的概念實現圖片縮放,精度有限
private Image createThumbnail(Image image,int width) {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
int thumbWidth = width;
int thumbHeight = -1;
if (thumbHeight == -1)
//計算小圖片高度
thumbHeight = thumbWidth * sourceHeight / sourceWidth;
//創建縮略圖
Image thumb = Image.createImage(thumbWidth, thumbHeight);
Graphics g = thumb.getGraphics();
for (int y = 0; y < thumbHeight; y++) {
for (int x = 0; x < thumbWidth; x++) {
g.setClip(x, y, 1, 1);
int dx = x * sourceWidth / thumbWidth;
int dy = y * sourceHeight / thumbHeight;
g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
}
}
Image immutableThumb = Image.createImage(thumb);
return immutableThumb;
}
public void commandAction(Command cmd,Displayable displayable){
if(cmd == List.SELECT_COMMAND){
//顯示圖片
ImageCanvas canvas = new ImageCanvas(img);
canvas.addCommand(backCommand);
canvas.setCommandListener(this);
display.setCurrent(canvas);
}else if(cmd == backCommand){
display.setCurrent(main);
}else if(cmd == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -