【问题标题】:Blackberry - background image/animation RIM OS 4.5.0Blackberry - 背景图像/动画 RIM OS 4.5.0
【发布时间】:2010-11-24 20:13:36
【问题描述】:

请帮助我,如何为屏幕设置背景图像以及如何在任意字段或文本上制作动画?

谢谢……

【问题讨论】:

  • 这真的应该是两个独立的问题

标签: user-interface blackberry animation rim-4.5


【解决方案1】:

背景图片

在 Screen 类中有一个 protected void paintBackground(Graphics graphics) 方法。
由于某种原因,我们不能直接使用它在屏幕上绘制背景图像。 catch:paintBackground 方法派生自 Field 类,我们可以在 VerticalFieldManager 示例中使用它:

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 动画

要使用 GIF 动画,请覆盖 protected void paint(Graphics graphics) 方法并使用递增帧索引的 drawImage。使用Timer.scheduleAtFixedRate 使字段无效:

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;
        }
    }
}

编辑:很棒的文章 - Direct Screen Drawing

【讨论】:

  • 这是一个很好的答案,我建议将其分成两个单独的问题,以便于查找。创建一个新问题,将下半部分切入其中并编辑原始问题。会自己做,但我还差几百个代表。
猜你喜欢
  • 2010-10-23
  • 1970-01-01
  • 2011-01-08
  • 2023-04-04
  • 1970-01-01
  • 2016-03-31
  • 2017-05-11
  • 1970-01-01
相关资源
最近更新 更多