代码之家  ›  专栏  ›  技术社区  ›  Jessu

黑莓手机-背景图像/动画RIM OS 4.5.0

  •  1
  • Jessu  · 技术社区  · 17 年前

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

    非常感谢。...

    1 回复  |  直到 17 年前
        1
  •  13
  •   Maksym Gontar    17 年前

    背景图像

    在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