Question

S'il vous plaît aidez-moi, comment définir une image d'arrière-plan pour l'écran et comment faire des animations sur tout-terrain ou sur le texte?

Merci ....

Était-ce utile?

La solution

Image de fond

Dans la classe d'écran il y a un protégée PaintBackground vide (graphiques) Les méthode .
Par une raison quelconque, nous ne pouvons pas l'utiliser directement pour peindre l'image d'arrière-plan dans l'écran. Le hic: méthode PaintBackground est dérivé de la classe Field et nous pouvons l'utiliser dans VerticalFieldManager sur exemple:

class BgScreen extends MainScreen implements FieldChangeListener {
 ButtonField mButton;
 public BgScreen(Bitmap background) {
  super();
  BGVerticalFieldManager manager = 
   new BGVerticalFieldManager(background);
  add(manager);
  mButton = new ButtonField("Button", ButtonField.CONSUME_CLICK);
  mButton.setChangeListener(this);
  manager.add(mButton);
 }

 public void fieldChanged(Field field, int context) {
  if (mButton == field)
   Dialog.inform("You pressed button");
 }
}

class BGVerticalFieldManager extends VerticalFieldManager {
 Bitmap mBgBitmap = null;
 int mBgWidth = -1;
 int mBgHeight = -1;
 int mBgX = -1;
 int mBgY = -1;

 public BGVerticalFieldManager(Bitmap background) {
  super(USE_ALL_WIDTH | USE_ALL_HEIGHT);
  mBgBitmap = background;
  mBgWidth = mBgBitmap.getWidth();
  mBgHeight = mBgBitmap.getHeight();
  mBgX = (Display.getWidth() - mBgWidth) >> 1;
  mBgY = (Display.getHeight() - mBgHeight) >> 1;

 }

 protected void paintBackground(Graphics graphics) {
  paintBackgroundBitmap(graphics);
  super.paintBackground(graphics);
 }

 private void paintBackgroundBitmap(Graphics graphics) {
  if (null != mBgBitmap) {
   graphics.drawBitmap(
    mBgX, mBgY, mBgWidth, mBgHeight, mBgBitmap, 0, 0);
  }
 }
}

GIF animation

Pour utiliser l'animation GIF, remplacer protégé peinture vide (graphique graphiques) méthode et en utilisant drawImage d'index de trame incrémenté. Utilisez Timer.scheduleAtFixedRate infirment champ:

class GIFVerticalFieldManager extends VerticalFieldManager {
    EncodedImage mGIFImage = null;
    int mGIFWidth = -1;
    int mGIFHeight = -1;
    int mGIFX = -1;
    int mGIFY = -1;
    int mGIFFrameCount = -1;
    int mGIFFrameIndex = -1;
    final int mGIFDelay = 30;

    public GIFVerticalFieldManager(EncodedImage gifAnimation) {
        super(USE_ALL_WIDTH | USE_ALL_HEIGHT);
        mGIFImage = gifAnimation;
        mGIFWidth = mGIFImage.getWidth();
        mGIFHeight = mGIFImage.getHeight();
        mGIFX = (Display.getWidth() - mGIFWidth) >> 1;
        mGIFY = (Display.getHeight() - mGIFHeight) >> 1;
        mGIFFrameCount = mGIFImage.getFrameCount();
        mGIFFrameIndex = 0;

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                invalidate();
            }
        }, mGIFDelay, mGIFDelay);
    }

    protected void paint(Graphics graphics) {
        paintGifAnimation(graphics);
        super.paint(graphics);
    }

    private void paintGifAnimation(Graphics graphics) {
        if (null != mGIFImage) {
            graphics.drawImage(
                mGIFX, mGIFY, mGIFWidth, mGIFHeight, 
            mGIFImage, mGIFFrameIndex, 0, 0);
            mGIFFrameIndex++;
            if (mGIFFrameIndex > mGIFFrameCount - 1)
                mGIFFrameIndex = 0;
        }
    }
}

EDIT: Grand article - Dessin d'écran direct

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top