雙緩沖技術的應用很廣泛,設計游戲的時候更是需要它,
在midp1.0中,api中并沒有game這個包,看到網上很多人在討論設計游戲的時候會出現圖片斷裂,屏幕閃爍等問題。
我經過這幾天的學習整理下自己的學習心得,用來拋磚,希望對此有研究高手們相互討論。讓我也學習學習。
雙緩沖的原理可以這樣形象的理解:把電腦屏幕看作一塊黑板。首先我們在內存環境中建立一個“虛擬“的黑板,然后在這塊黑板上繪制復雜的圖形,等圖形全部繪 制完畢的時候,再一次性的把內存中繪制好的圖形“拷貝”到另一塊黑板(屏幕)上。采取這種方法可以提高繪圖速度,極大的改善繪圖效果。
對于手機來說。具體的過程就是通過extends Canvas。然后獲取bufferImage。再然后就getGraphics。最后就是在這個graphics中繪制圖片等,再最后就是把這個繪制好的bufferImage繪制的屏幕上。
說歸說。具體還是要看代碼的。里面的代碼參照了一些開源的代碼。
java 代碼
-
-
-
-
-
- package org.wuhua.game;
-
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
-
-
-
-
-
-
-
-
-
-
-
- public abstract class GameCanvas extends Canvas {
-
-
-
-
- private Image bufferImage;
-
- private int height;
-
- private int width;
-
- private int clipX, clipY, clipWidth, clipHeight;
-
- private boolean setClip;
-
- protected GameCanvas() {
-
- super();
-
- width = getWidth();
- height = getHeight();
-
- this.bufferImage = Image.createImage(width, height);
-
- }
-
- protected void paint(Graphics g) {
-
- if (this.setClip) {
- g.clipRect(this.clipX, this.clipY, this.clipWidth, this.clipHeight);
- this.setClip = false;
- }
- g.drawImage(this.bufferImage, 0, 0, Graphics.TOP | Graphics.LEFT);
-
- }
-
- public void flushGraphics(int x, int y, int width, int height) {
- this.setClip = true;
- this.clipX = x;
- this.clipY = y;
- this.clipWidth = width;
- this.clipHeight = height;
-
- repaint();
- serviceRepaints();
- }
-
- public void flushGraphics() {
- repaint();
- serviceRepaints();
- }
-
-
-
-
-
- protected Graphics getGraphics() {
- return this.bufferImage.getGraphics();
- }
-
-
-
-
- protected final void sizeChanged(int w, int h) {
- if (h > height) {
- this.bufferImage = Image.createImage(w, h);
- }
- }
- }
|
|