黑莓 - 在屏幕上绘制图像

时间:2009-09-15 10:18:39

标签: user-interface graphics blackberry java-me png

如何在屏幕上绘制具有特定大小和位置的png图像?

4 个答案:

答案 0 :(得分:27)

调整图像大小

 public EncodedImage sizeImage(EncodedImage image, int width, 
  int height) {
  EncodedImage result = null;

  int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
  int currentHeightFixed32 = Fixed32.toFP(image.getHeight());

  int requiredWidthFixed32 = Fixed32.toFP(width);
  int requiredHeightFixed32 = Fixed32.toFP(height);

  int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
    requiredWidthFixed32);
  int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
    requiredHeightFixed32);

  result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
  return result;
 }

此功能将在下面的代码中使用。

只是画图像

Simply painting images http://img268.imageshack.us/img268/9918/bb8310.png

让我们以表格方式绘制9幅图像,图像尺寸不同,但我们会将它们调整为80x80,并为它们提供10像素的边距。

假设您的项目资源中有9个png图像。

  1. 加载图片
  2. 调整图片大小
  3. 在特定位置的每个油漆事件上绘制图像
  4. 代码:

    class Scr extends MainScreen {
     int mImgWidth = 80;
     int mImgHeight = 80;
     int mImgMargin = 10;
     String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
       "6.png", "7.png", "8.png", "9.png" };
     EncodedImage[] mImages;
    
     public Scr() {
     super();
      prepareImages();
     }
    
     private void prepareImages() {
      mImages = new EncodedImage[fileNames.length];
      for (int i = 0; i < fileNames.length; i++) {
       EncodedImage image = EncodedImage
         .getEncodedImageResource(fileNames[i]);
       mImages[i] = sizeImage(image, mImgWidth, mImgHeight);
      }
     }
    
     protected void paint(Graphics graphics) {
      paintImages(graphics);
      super.paint(graphics);
     }
    
     private void paintImages(Graphics graphics) {
      int scrWidth = Display.getWidth();
      int columns = scrWidth / (mImgWidth + 2 * mImgMargin);
      int rows = mImages.length / columns
        + (mImages.length % columns > 0 ? 1 : 0);
      for (int i = 0; i < rows; i++) {
       for (int j = 0; j < columns; j++) {
        int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
        int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
        EncodedImage image = mImages[i * columns + j];
        graphics.drawImage(posX, posY, mImgWidth, mImgHeight,
          image, 0, 0, 0);
       }
      }
     }
    }
    

    只需绘制图像 - 优化

    看一下Scr的paint()方法。每次刷新时,整个图像表都会重新绘制,这意味着每个画面上都会有9个drawImage调用。如果我们只是对这个表进行shapshot并在paint()方法中使用它,该怎么办?

    class ScrOpt extends MainScreen {
     int mScrWidth = Display.getWidth();
     int mScrHeight = Display.getHeight();
     int mImgWidth = 80;
     int mImgHeight = 80;
     int mImgMargin = 10;
     String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
       "6.png", "7.png", "8.png", "9.png" };
     EncodedImage[] mImages;
     Bitmap mImgTable;
    
     public ScrOpt() {
      super();
      prepareImages();
      mImgTable = paintImages();
     }
    
     private void prepareImages() {
      mImages = new EncodedImage[fileNames.length];
      for (int i = 0; i < fileNames.length; i++) {
       EncodedImage image = EncodedImage
         .getEncodedImageResource(fileNames[i]);
       mImages[i] = sizeImage(image, mImgWidth, mImgHeight);
      }
     }
    
     private Bitmap paintImages() {
      Bitmap result = new Bitmap(mScrWidth, mScrHeight);
      Graphics graphics = new Graphics(result);
      int scrWidth = mScrWidth;
      int columns = scrWidth / (mImgWidth + 2 * mImgMargin);
      int rows = mImages.length / columns
        + (mImages.length % columns > 0 ? 1 : 0);
      for (int i = 0; i < rows; i++) {
       for (int j = 0; j < columns; j++) {
        int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
        int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
        EncodedImage image = mImages[i * columns + j];
        graphics.drawImage(posX, posY, mImgWidth, mImgHeight, image, 0,
          0, 0);
       }
      }
      return result;
     }
    
     protected void paint(Graphics graphics) {  
      super.paint(graphics);
      graphics.drawBitmap(0, 0, mScrWidth, mScrHeight, mImgTable, 0, 0);
     }
    }
    

    您可以进一步优化它,using paintBackground() method

    使用BitmapField

    以上所有内容都是使用Graphics直接将图像绘制到屏幕上。有时它很棒 - 当你想要显示一些动画或背景图像时。但是,如果您想保持标准的UI用户体验,并将图像用作字段,该怎么办?

    alt text http://img142.imageshack.us/img142/7485/bb83102.png

    海峡方式是BitmapField

    class ScrBmpField extends MainScreen {
     int mImgWidth = 80;
     int mImgHeight = 80;
     int mImgMargin = 10;
     String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
       "6.png", "7.png", "8.png", "9.png" };
     BitmapField[] mBmpFields;
    
     public ScrBmpField() {
      super(VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
      prepareBmpFields();  
     }
    
     private void prepareBmpFields() {
      mBmpFields = new BitmapField[fileNames.length];
      for (int i = 0; i < fileNames.length; i++) {
       EncodedImage image = EncodedImage
         .getEncodedImageResource(fileNames[i]);
       image = sizeImage(image, mImgWidth, mImgHeight);
       mBmpFields[i] = 
           new BitmapField(image.getBitmap(), FOCUSABLE|FIELD_HCENTER);
       mBmpFields[i].setMargin(mImgMargin, mImgMargin, 
           mImgMargin, mImgMargin);
       add(mBmpFields[i]);
      }
     }
    }
    

    使用BitmapField - 自定义布局

    alt text http://img9.imageshack.us/img9/403/bb83103.png

    要在管理器中设置BitmapField的自定义位置,您可以实现manager with custom layout

    class ScrLayout extends MainScreen {
        int mScrWidth = Display.getWidth();
        int mScrHeight = Display.getHeight();
        int mImgWidth = 80;
        int mImgHeight = 80;
        int mImgMargin = 10;
        String fileNames[] = { "1.png", "2.png", "3.png", "4.png", "5.png",
                "6.png", "7.png", "8.png", "9.png" };
        BitmapField[] mBmpFields;
    
        public ScrLayout() {
            super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
            prepareBmpFields();
        }
    
        private void prepareBmpFields() {
            LayoutManager manager = new LayoutManager();
            add(manager);
            mBmpFields = new BitmapField[fileNames.length];
            for (int i = 0; i < fileNames.length; i++) {
                EncodedImage image = EncodedImage
                        .getEncodedImageResource(fileNames[i]);
                image = sizeImage(image, mImgWidth, mImgHeight);
                mBmpFields[i] = 
                    new BitmapField(image.getBitmap(), FOCUSABLE);
                manager.add(mBmpFields[i]);
            }
        }
    
        class LayoutManager extends VerticalFieldManager {
            public LayoutManager() {
                super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
            }
    
            protected void sublayout(int width, int height) {
                int columns = mScrWidth / (mImgWidth + 2 * mImgMargin);
                for (int i = 0, j = 0; i < mBmpFields.length; i++) {
                    int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin;
                    int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin;
                    Field field = mBmpFields[i];
                    layoutChild(field, mImgWidth, mImgHeight);
                    setPositionChild(field, posX, posY);
    
                    j = (j == columns - 1) ? 0 : j + 1;
                }
                setExtent(mScrWidth, mScrHeight);
            }
            public int getPreferredWidth() {
                return mScrWidth;
            }
            public int getPreferredHeight() {
                return mScrHeight;
            }
        }
    }
    

答案 1 :(得分:4)

初始化函数中的某处:

 Image myImage = Image.createImage("/myimage.png");

在画布的绘画功能中:

 g.drawImage(myImage, posX, posY, Graphics.TOP|Graphics.LEFT);

(其中g是从paint函数获得的Graphics对象)

编辑:修复了注释中指出的小错误

答案 2 :(得分:2)

如果您使用net.rim.device.api.system.PNGEncodedImage或从net.rim.device.api.system.EncodedImage扩展的其他类之一,您可以使用scaleImage32(int scaleX,int scaleY)方法(在OS 4.2和后者中可用)将图像缩放到您想要的大小。请注意,虽然scaleX和scaleY虽然输入为int实际上是net.rim.device.api.math.Fixed32所以要显示一半大小的图像:

EncodedImage halfSize = myImage.scaleImage32(Fixed32.toFP(2), Fixed32.toFP(2));

或者是原始尺寸的两倍图像:

EncodedImage twiceSize = myImage.scaleImage32(Fixed32.tenThouToFP(5000), Fixed32.tenThouToFP(5000));

答案 3 :(得分:0)

private VerticalFieldManager mainBackVerticalFieldManager = null;

int deviceWidth = Display.getWidth();
int deviceHeight = Display.getHeight();    

mainBackVerticalFieldManager   = new VerticalFieldManager(VerticalFieldManager.NO_VERTICAL_SCROLL|VerticalFieldManager.NO_VERTICAL_SCROLLBAR){
        protected void paint(Graphics graphics) {
            graphics.clear();
            graphics.drawBitmap(0, 0, deviceWidth,deviceHeight,Bitmap.getBitmapResource(Constants.PURCHASE_SUMMARY_BG), 0,0);
            super.paint(graphics);
        };
    };

您可以在此处根据需要更改deviceWidth和height。