[软件设计/软件工程] 在android中合并两个png文件

[复制链接]
发表于 2022-5-5 09:19:26
问题
我有两个 png 图像文件,我希望我的 android 应用程序以编程方式组合成一个 png 图像文件,我想知道是否可以这样做?如果是这样,我要做的就是将它们堆叠在一起以创建一个文件。

这背后的想法是我有一些 png 文件,一些图像的左侧部分透明,而另一些图像的右侧部分透明。根据用户输入,它将两者组合到一个文件中进行显示。 (我不能只是并排显示两个图像,它们必须是一个文件)

在android中这可能以编程方式吗?这怎么可能?

回答
一段时间以来,我一直在想办法解决这个问题。

这是(本质上)我用来让它工作的代码。
  1. // Get your images from their files
  2. Bitmap bottomImage = BitmapFactory.decodeFile("myFirstPNG.png");
  3. Bitmap topImage = BitmapFactory.decodeFile("myOtherPNG.png");

  4. // As described by Steve Pomeroy in a previous comment,
  5. // use the canvas to combine them.
  6. // Start with the first in the constructor..
  7. Canvas comboImage = new Canvas(bottomImage);
  8. // Then draw the second on top of that
  9. comboImage.drawBitmap(topImage, 0f, 0f, null);

  10. // comboImage is now a composite of the two.

  11. // To write the file out to the SDCard:
  12. OutputStream os = null;
  13. try {
  14.     os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
  15.     comboImage.compress(CompressFormat.PNG, 50, os)
  16. } catch(IOException e) {
  17.     e.printStackTrace();
  18. }
复制代码

编辑:

有错别字,

所以,我改变了

image.compress(CompressFormat.PNG, 50, os)

到达

bottomImage.compress(CompressFormat.PNG, 50, os)





上一篇:在 JavaScript 中解析 JSON 对象数组
下一篇:单链表 C++ 的快速选择算法

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表