在android中结合两个png文件

Joh*_*ohn 17 android image

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

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

这有可能在android中以编程方式进行,怎么做?

Ehx*_*xor 32

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

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

// 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();
}
Run Code Online (Sandbox Code Playgroud)

编辑:

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

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

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