Blackberry - 背景图像/动画 RIM OS 4.5.0 [英] Blackberry - background image/animation RIM OS 4.5.0

查看:22
本文介绍了Blackberry - 背景图像/动画 RIM OS 4.5.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please help me, how to set a background image for screen and How to do animations on any-field or on text?

谢谢....

推荐答案

背景图片

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

Background image

In Screen class there is a protected void paintBackground(Graphics graphics) method.
By some reason we can't use it directly to paint background image in screen. The catch: paintBackground method is derived from Field class, and we can use it in VerticalFieldManager on example:

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 使字段无效:

GIF animation

To use GIF animation, override protected void paint(Graphics graphics) method and use drawImage of incremented frame index. Use Timer.scheduleAtFixedRate to invalidate field:

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

很棒的文章 - 直接屏幕绘制

这篇关于Blackberry - 背景图像/动画 RIM OS 4.5.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆