黑莓 - 在屏幕上绘制图像

6
如何在屏幕上绘制指定大小和位置的PNG图像?
4个回答

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

以下代码将使用此函数。

简单绘制图像

简单绘制图像 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()方法。每次刷新时,整个图像表格都会重新绘制,这意味着每次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

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

alt文本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

要在管理器中设置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;
        }
    }
}

4

在初始化函数中:

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

在您的画布的绘制函数中:

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

(其中g是从绘制函数中获取的Graphics对象)
编辑:根据评论中指出的小错误进行修正。

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

唯一的缺点是代码仅使用rim类,因此无法在任何其他Java设备上移植。 - Toad
1
真的,但你的答案只满足了 OP 指定的两个要求中的一个。 - Richard

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接