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