我设法使用此代码将两个图像合二为一,但我不知道如何将其保存到 SD 卡。我想将它保存到以我的应用名称命名的 sd 卡中的文件夹中。
额外:如果图像名称在时间和日期之后或独特的东西之后会很好,这样就不会有图像名称的任何副本。
public class CombineImages extends View{
private Bitmap buffer;
private Canvas canvas;
private Matrix matrix = new Matrix();
public CombineImages(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CombineImages(Context context) {
super(context);
}
public void combine(ImageView imageView){
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
combine(bitmap);
}
public void combine(Bitmap bitmap) {
updateBuffer(bitmap);
draw(canvas);
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(buffer, matrix , null);
}
private void updateBuffer(Bitmap bitmap) {
if(buffer == null){
createBuffer(bitmap);
}
else{
if(bitmap.getWidth() > buffer.getWidth() || bitmap.getHeight() > buffer.getHeight()){
Bitmap oldBuffer = buffer;
createBuffer(bitmap);
drawBitmnapToBuffer(oldBuffer);
oldBuffer.recycle();
}
drawBitmnapToBuffer(bitmap);
}
getLayoutParams().height = buffer.getHeight();
getLayoutParams().width = buffer.getWidth();
}
private void drawBitmnapToBuffer(Bitmap bitmap) {
canvas.save();
// add your translation logic here using canvas.translate(dx, dy);
canvas.drawBitmap(bitmap, matrix, null);
canvas.restore();
}
private void createBuffer(Bitmap bitmap) {
buffer = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
canvas = new Canvas(buffer);
}
}