android - 如何在android中的图像上添加多个图层

标签 android android-imageview

我希望在应用程序中有一个( 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/

相关文章:

android - 如何使用循环从 Assets 文件夹加载音频文件?

android - 在Android中播放RTSP链接

java - 从 GridView 中删除填充并使用整个 View 来放置项目

android - Glide - 如何将圆形裁剪应用于错误图像?

Imageview上的Android自定义listview点击获取textview数据并打开 Activity

android - 数据库对象未关闭异常

java - 无法缓冲特殊字符

android - Android 中未使用的字符串和图像资源

java - 如何将缓存目录中的文件放入 Android 上的 ImageView 中?

android - 即使在使用高效加载大位图指南后如何避免 OutOfMemoryError