android - 在android中合并两个png文件

标签 android image

我有两个 png 图像文件,我希望我的 android 应用程序以编程方式将它们组合成一个 png 图像文件,我想知道是否可以这样做?如果是这样,我想做的就是将它们相互叠加以创建一个文件。

这背后的想法是我有一些 png 文件,其中一些图像的一部分在左侧,其余部分透明,其他图像在右侧,其余部分透明。并根据用户输入将两者结合起来制作一个文件来显示。 (我不能只是并排显示两个图像,它们需要是一个文件)

这有可能在 android 中以编程方式实现吗?

最佳答案

我一直在尝试解决这个问题。

这是(基本上)我用来使它工作的代码。

// Get your images from their files
Bitmap bottomImage = BitmapFactory.decodeFile("myFirstPNG.png");
Bitmap topImage = BitmapFactory.decodeFile("myOtherPNG.png");

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

// comboImage is now a composite of the two. 

// To write the file out to the SDCard:
OutputStream os = null;
try {
    os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
    comboImage.compress(CompressFormat.PNG, 50, os)
} catch(IOException e) {
    e.printStackTrace();
}

编辑:

有一个错字, 所以,我改变了

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

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

关于android - 在android中合并两个png文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2738834/

相关文章:

android - 有关 GoogleAdView.jar 的帮助

android - AndEngine-停止所有声音和音乐运行时-更改引擎的AudioOptions

html - 如何使用 css 添加 border-bottom-image

jquery - 如何放置全宽背景图像?

html - 如何将单选按钮与 html 中的图像中间垂直对齐?

android - 启动Youtube并从应用程序自动连接到Chrome Cast设备

android - 创建 Osmdroid Mapview 的屏幕截图

android - 尝试在 Firebase 测试实验室中调用虚方法 android.arch.lifecycle.Lifecycle

javascript - 在 dropzonejs 中预加载图像后,图像预览(缩略图)不显示

iOS 将多个图像复制到粘贴板