我有两个 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/