BlackBerry - 在屏幕上绘制图像

发布于 2024-08-05 05:34:15 字数 29 浏览 6 评论 0原文

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

How to draw png images with specific size and position on the screen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烛影斜 2024-08-12 05:34:15

调整图像大小

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

该函数将在下面的代码中使用。

简单地绘制图像

简单地绘制图像 http://img268.imageshack.us/img268/9918/ bb8310.png

让我们以表格方式绘制 9 个图像,图像大小不同,但我们将它们调整为 80x80 并给它们边距 10 像素。

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

  1. 加载图像
  2. 调整图像大小
  3. 在特定位置内的每个绘制事件上绘制图像

代码:

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。如果我们只获取该表的快照并在 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);
 }
}

您可以进一步优化它,使用paintBackground()方法

使用BitmapField

以上都是关于使用图形。有时它很棒 - 当您想要显示一些动画或背景图像时。但是,如果您想保持标准的 UI 用户体验并使用图像作为字段怎么办?

替代文本 http://img142.imageshack.us/img142/7485/bb83102.png< /a>

海峡的方式是 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 - 自定义布局

替代文本 http:// img9.imageshack.us/img9/403/bb83103.png

要在管理器中设置 BitmapFields 的自定义位置,您可以实现 具有自定义布局的管理器

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

Resize image

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

This function will be used in the code below.

Simply painting images

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

Lets paint 9 images in a table way, images are different in size, but we will resize them to 80x80 and give them margins 10 px.

Assuming you have 9 png images in your project resources.

  1. Load images
  2. Resize images
  3. Paint images on every paint event, within certain position

Code:

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

Simply painting images - optimization

Take a look at paint() method of Scr. On every refresh the whole table of images is repainting, that means 9 drawImage call on every paint. What if we just take a shapshot of this table and use it in paint() method?

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

You can otimize it even more, using paintBackground() method

Using BitmapField

All above is about painting images directly to screen using Graphics. Sometimes its great - when you want to display some animation or background image. But what if you want to keep standard UI user experience, and use images as a fields?

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

The strait way is a 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]);
  }
 }
}

Using BitmapField - custom layout

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

To set a custom positions of BitmapFields within manager, you can implement 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;
        }
    }
}
如何视而不见 2024-08-12 05:34:15

在初始化函数中的某处:

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

以及在画布的绘制函数中:(

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

其中 g 是从绘制函数获得的 Graphics 对象)

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

Somewhere in an initialization function:

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

And in the paint function of your canvas:

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

(where g is the Graphics object you get from the paint function)

edit: fixed small error as pointed out in comments

我不会写诗 2024-08-12 05:34:15

如果您使用 net.rim.device.api.system.PNGEncodedImage 或从 net.rim.device.api.system.EncodedImage 扩展的其他类之一,您可以使用 scaleImage32(int scaleX, int scaleY) 方法(在操作系统中可用) 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));

If you use net.rim.device.api.system.PNGEncodedImage or one of the other classes extended from net.rim.device.api.system.EncodedImage you can use the scaleImage32(int scaleX, int scaleY) method (available in OS 4.2 and latter) to scale the image to the size you want. Be aware though that scaleX and scaleY, though typed as int are actually net.rim.device.api.math.Fixed32 so to display the image at one half size:

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

Or for an image twice original size:

EncodedImage twiceSize = myImage.scaleImage32(Fixed32.tenThouToFP(5000), Fixed32.tenThouToFP(5000));
他是夢罘是命 2024-08-12 05:34:15
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);
        };
    };

在这里您可以根据需要更改设备宽度和高度。

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

Here you can change deviceWidth and height as your requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文