views:

206

answers:

3

Hello all,

I'm trying to develop a small map-like application for blackberry. I need to know how can I position a small image onto another much larger image.

The larger image is like a map, I'll need to use the small the image to pin point a specific position on the map. So the smaller image should overlay on top of the larger image. At the same time, I'll need to move the map around, and the smaller image should follow that position on the map.

Does anyone know if there is a specific API for this problem?

A: 

On the Blackberry (and other java based mobile devices) you can stack as many bitmaps on top of each-other as you like.

Just make sure that the bitmap you draw over the first one contains transparent pixels to allow see through bits and irregular shapes.

Best way is to use png8 with one of the colors set as completely transparent.

The more modern phones allow alpha values nowadays, which can be used for semi-transparent effects. To try if your Blackberry supports this too, use a png32 image for the second bitmap and make sure that some of the pixels have semi transparent alpha values (128 for instance).

Toad
Do you know if there is a specific API for Blackberry?
A: 

Well for transparency, check this graphics class >setGlobalAlpha method. You can set its value from 0-100 according to the transparency you want.

http://www.blackberry.com/developers/docs/4.0.2api/net/rim/device/api/ui/Graphics.html http://www.blackberry.com/developers/docs/4.0.2api/net/rim/device/api/ui/Graphics.html#setGlobalAlpha%28int%29

Bohemian
A: 

sample to position icon bitmap over the center of map bitmap:

class Scr extends MainScreen {
    Bitmap mMapBitmap = Bitmap.getBitmapResource("map.png");
    Bitmap mIconBitmap = Bitmap.getBitmapResource("icon.png");

    Bitmap getOverlappedBitmap(Bitmap map, Bitmap icon) {
     Graphics g = new Graphics(map);
     int width = icon.getWidth();
     int height = icon.getHeight();
     int x = (map.getWidth() - width) / 2;
     int y = (map.getHeight() - height) / 2;
     g.drawBitmap(x, y, width, height, icon, 0, 0);
     return map;
    }

    public Scr() {
     add(new BitmapField(getOverlappedBitmap(mMapBitmap, mIconBitmap)));
    }
}
  • create array of bitmaps
  • create Graphics from new bitmap and draw all bitmaps with defined alpha value
  • use custom class PNGEncoder to save bitmap png image

See example

class Scr extends MainScreen {
 Bitmap[] images = new Bitmap[0];

 int width = Display.getWidth();
 int height = Display.getHeight();
 Bitmap bitmap = new Bitmap(width, height);
 BitmapField bitmapField = new BitmapField(bitmap);

 public Scr() {
  add(bitmapField);
 }

 MenuItem mAddBitmap = new MenuItem("Add image", 0, 0) {
  public void run() {
   Bitmap image = Bitmap.getBitmapResource("img" + (images.length + 1)
     + ".png");
   Arrays.add(images, image);
   refresh();
  }
 };

 MenuItem mClean = new MenuItem("Clean", 0, 0) {
  public void run() {
   images = new Bitmap[0];
   refresh();
  }
 };

 MenuItem mSave = new MenuItem("Save", 0, 0) {
  public void run() {
   String mFileName = System.getProperty("fileconn.dir.photos")
     + "test.bmp";

   // bitmap to byte array conversion
   byte[] bitmapBuffer = new byte[0];
   PNGEncoder encoder = new PNGEncoder(bitmap, true);
   try {
    bitmapBuffer = encoder.encode(true);
   } catch (IOException e) {
    e.printStackTrace();
   }

   writeFile(bitmapBuffer, mFileName);
  }
 };

 private void writeFile(byte[] data, String fileName) {
  FileConnection fconn = null;
  try {
   fconn = (FileConnection) Connector.open(fileName,
     Connector.READ_WRITE);
  } catch (IOException e) {
   System.out.print("Error opening file");
  }

  if (fconn.exists())
   try {
    fconn.delete();
   } catch (IOException e) {
    System.out.print("Error deleting file");
   }
  try {
   fconn.create();
  } catch (IOException e) {
   System.out.print("Error creating file");
  }
  OutputStream out = null;
  try {
   out = fconn.openOutputStream();
  } catch (IOException e) {
   System.out.print("Error opening output stream");
  }

  try {
   out.write(data);
  } catch (IOException e) {
   System.out.print("Error writing to output stream");
  }

  try {
   fconn.close();
  } catch (IOException e) {
   System.out.print("Error closing file");
  }
 }

 void refresh() {

  bitmap = new Bitmap(width, height);

  Graphics g = new Graphics(bitmap);

  for (int i = 0; i < images.length; i++) {
   g.setGlobalAlpha(100);
   g.drawBitmap(0, 0, width, height, images[i], 0, 0);
  }

  bitmapField.setBitmap(bitmap);
 }

 protected void makeMenu(Menu menu, int instance) {
  super.makeMenu(menu, instance);
  menu.add(mAddBitmap);
  menu.add(mSave);
  menu.add(mClean);
 }
}
Max Gontar