【问题标题】:How do you set the background image in a BlackBerry application using Java?如何使用 Java 在 BlackBerry 应用程序中设置背景图像?
【发布时间】:2011-12-19 15:34:28
【问题描述】:

我如何在 java 黑莓模拟器中设置背景图像?

backgroundBitmap = Bitmap.getBitmapResource("background.png");
MainScreen mainScreen = new MainScreen();

HorizontalFieldManager horizontalFieldManager = 
    new HorizontalFieldManager(
        HorizontalFieldManager.USE_ALL_WIDTH | 
        HorizontalFieldManager.USE_ALL_HEIGHT){

        //Override the paint method to draw the background image.
        public void paint(Graphics graphics) {
            //Draw the background image and then call paint.
            graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0);
            super.paint(graphics);
        }            

    };

【问题讨论】:

  • 如果您搜索论坛,您可以找到具有相同信息的各种帖子
  • 空指针异常表示代码无法获取png文件。你把文件“background.png”放在哪里了?
  • 我将png图片复制到项目的res文件夹中
  • 请同时检查文件名的拼写,可能是它的问题

标签: blackberry


【解决方案1】:

【讨论】:

  • 您是否遇到过任何具体问题或异常
  • 请贴一些代码,否则没人能调试你的问题
  • 还要确保您在代码中输入了正确的图像文件名称。在您的情况下,空指针异常很可能是因为文件名错误。或文件不存在 .. 如果您刚刚添加了文件并使用 eclipse 检查图像是否在树视图中可用.. 在 res 文件夹中如果没有并且您已复制到那里.. 按 F5 并尝试再次运行它。
  • 您是否尝试过调试以检查发生空指针异常的确切位置?
【解决方案2】:
     Bitmap bitmapOrig = Bitmap.getBitmapResource("ICON.png");            
      // Create a Bitmap of arbitrary size
      int scaledX = 360;
      int scaledY = 415;
      Bitmap bitmapScaled = new Bitmap(scaledX, scaledY);
      bitmapOrig.scaleInto(bitmapScaled , Bitmap.FILTER_LANCZOS);                       
      bitmapOrig.scaleInto(bitmapScaled , Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);                       
      BitmapField bitmapFieldScaled2 = new BitmapField(bitmapScaled , Field.FOCUSABLE);
     homeScreen.add(bitmapFieldScaled2);

【讨论】:

    【解决方案3】:
    HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(
                                    HorizontalFieldManager.USE_ALL_WIDTH | 
                                    HorizontalFieldManager.USE_ALL_HEIGHT){
    
        //Override the paint method to draw the background image.
        public void paint(Graphics graphics) {
            //Draw the background image and then call paint.
            graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0);
            super.paint(graphics);
        }    
       protected void sublayout(int maxWidth, int maxHeight){ 
                super.sublayout(240,240);
                setExtent(240,240);
            }        
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 2020-04-19
      • 2011-02-08
      • 2010-10-06
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多