如何 使用Canvas和Thread设计一个动画????????????
------解决方案--------------------------------------------------------
只要在Canvas 的paint中根据绘制参数绘制就行了,而Thread则是驱动绘制参数的更新。
------解决方案--------------------------------------------------------
很基础的问题,随便翻本书就有的
------解决方案--------------------------------------------------------
一帧一帧地画呗
------解决方案--------------------------------------------------------
package prj4_c;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Canvas_Repaint extends MIDlet {
private Display display;
private MyCanvas mc;
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
mc = new MyCanvas();
display.setCurrent(mc);
//Thread thread = new Thread(mc);
//thread.start();
mc.startAnimation();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
class MyCanvas extends Canvas implements Runnable {
private int current;
private Image[] image;
public MyCanvas() {
image = new Image[11];
try {
for (int i = 1; i < 12; i++) {
image[i - 1] = Image.createImage("/" + i + ".png");
}
} catch (Exception e) {
e.printStackTrace();
}
current = 0;
}
public void paint(Graphics g) {
int width = this.getWidth();
int height = this.getHeight();
g.setColor(255, 255, 255);
g.fillRect(0, 0, width, height);
g.drawImage(image[current], width / 2, height / 2, Graphics.HCENTER
| Graphics.VCENTER);
}
public void startAnimation() {
repaint();
display.callSerially(this);
}
public void run() {
try {
Thread.sleep(300);
current = (current + 1) % 11;
repaint();
display.callSerially(this);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
------解决方案--------------------------------------------------------
以上程序循序显示1.png--11.png的图片