android 图片合成

时间:2022-10-20 11:33:02

package com.ebensz.eink.demo;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Exchange extends Activity implements OnClickListener {
    private Bitmap bitmap1;
    private Button mExportBtn;
    private static final String BITMAP_He = "/sdcard/bitmap_add.png";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scrollview);

        mExportBtn = (Button) this.findViewById(R.id.export_he);
        mExportBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.export_he:
            exportAll();
            break;
        default:
            break;
        }
    }

    /**
     * 将布局转换为View类型对象
     * @param filename
     */
    private void exportAll( ) {
        View view = getLayoutInflater().inflate(R.layout.scrollview, null);
        view.setDrawingCacheEnabled(true);
        view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        try {
            bitmap1 = view.getDrawingCache();
            heCheng(BITMAP_He);
        } catch (Exception e) {

        }
    }

    /**
     * bitmap1 考评结果图片
     * bitmap2 签字图片
     * bitmap3 带签字的考评结果
     * @param filename 生成带签字考评结果的文件路径和文件名
     */
    private void heCheng(String filename) {

        Bitmap bitmap2 = MainActivity.mbitmap;
        Bitmap bitmap3 = Bitmap.createBitmap(bitmap1.getWidth(),
                bitmap1.getHeight(), bitmap1.getConfig());
        Canvas canvas = new Canvas(bitmap3);
        canvas.drawBitmap(bitmap1, new Matrix(), null);
        canvas.drawBitmap(bitmap2, 20, 1000, null); // 120、350为bitmap2写入点的x、y坐标
        try {
            FileOutputStream out = new FileOutputStream(filename);
            bitmap3.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
        } catch (Exception e) {

        }
    }

}