雙緩沖技術(shù)的應(yīng)用很廣泛,設(shè)計(jì)游戲的時(shí)候更是需要它,
在midp1.0中,api中并沒有g(shù)ame這個(gè)包,看到網(wǎng)上很多人在討論設(shè)計(jì)游戲的時(shí)候會(huì)出現(xiàn)圖片斷裂,屏幕閃爍等問題。
我經(jīng)過這幾天的學(xué)習(xí)整理下自己的學(xué)習(xí)心得,用來拋磚,希望對(duì)此有研究高手們相互討論。讓我也學(xué)習(xí)學(xué)習(xí)。
雙緩沖的原理可以這樣形象的理解:把電腦屏幕看作一塊黑板。首先我們?cè)趦?nèi)存環(huán)境中建立一個(gè)“虛擬“的黑板,然后在這塊黑板上繪制復(fù)雜的圖形,等圖形全部繪 制完畢的時(shí)候,再一次性的把內(nèi)存中繪制好的圖形“拷貝”到另一塊黑板(屏幕)上。采取這種方法可以提高繪圖速度,極大的改善繪圖效果。
對(duì)于手機(jī)來說。具體的過程就是通過extends Canvas。然后獲取bufferImage。再然后就getGraphics。最后就是在這個(gè)graphics中繪制圖片等,再最后就是把這個(gè)繪制好的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);
- }
- }
- }
|