我希望在应用程序中有一个( map 的)图像并以编程方式添加 它上面的一些层(占位符,路径等......) 我认为类似 photoshop 的图层方法可能会有所帮助,但我不知道从哪里开始。 任何简单示例/指向教程或文档的链接都是有用的:)
谢谢
最佳答案
我为您提供了一个可以构建的简单方法:
- 创建一个空位图
finalBitmap
。这将是所有图层组合的最终目的地。 - 创建一个
Canvas
以绘制到finalBitmap
。此 Canvas 将用于将所有图层绘制到最终位图中。 - 用您的 map 图像创建一个
位图
。使用 Canvas 将其绘制到finalBitmap
。这将是第 1 层。 - 使用相同的方法放置标记、路线等。这些将是第 2、3 层等。
示例代码:
//The empty Bitmap
finalBitmap = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888);
canvas = new Canvas(finalBitmap );
imageView.setImageBitmap(finalBitmap );
//Create the map image bitmap
Config config = Config.RGB_565;
Options options = new Options();
options.inPreferredConfig = config;
InputStream in = null;
Bitmap bitmap = null;
try {
in = new FileInputStream(fMapImage);
bitmap = BitmapFactory.decodeStream(in);
if (bitmap == null)
throw new RuntimeException("Couldn't load bitmap from asset :" + fMapImage.getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException("Couldn't load bitmap from asset :" + fMapImage.getAbsolutePath());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
//Draw the map image bitmap
Rect dst = new Rect(pt00.x, pt00.y, ptMM.x, ptMM.y);
canvas.drawBitmap(bitmap, null, dst, null);
//Here draw whatever else you want (markers, routes, etc.)
问候
关于android - 如何在android中的图像上添加多个图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13790270/