我正在尝试为黑莓开发一个类似地图的小应用程序。我需要知道如何将小图像定位到另一个更大的图像上。
较大的图像就像一张地图,我需要使用较小的图像来确定地图上的特定位置。所以较小的图像应该覆盖在较大的图像之上。同时,我需要移动地图,较小的图像应该跟随地图上的那个位置。
有谁知道这个问题是否有特定的 API?
我正在尝试为黑莓开发一个类似地图的小应用程序。我需要知道如何将小图像定位到另一个更大的图像上。
较大的图像就像一张地图,我需要使用较小的图像来确定地图上的特定位置。所以较小的图像应该覆盖在较大的图像之上。同时,我需要移动地图,较小的图像应该跟随地图上的那个位置。
有谁知道这个问题是否有特定的 API?
将图标位图定位在地图位图中心的示例:
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)));
}
}
查看示例
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);
}
}
对于透明度,请检查此图形类 >setGlobalAlpha 方法。您可以根据所需的透明度将其值设置为 0-100。
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
在 Blackberry(和其他基于 Java 的移动设备)上,您可以根据需要将任意数量的位图堆叠在一起。
只需确保您在第一个上绘制的位图包含透明像素,以允许透视位和不规则形状。
最好的方法是使用 png8 并将其中一种颜色设置为完全透明。
现在更现代的手机允许使用 alpha 值,可用于半透明效果。要尝试您的黑莓是否也支持此功能,请为第二个位图使用 png32 图像,并确保某些像素具有半透明的 alpha 值(例如 128)。