android - 如何用彩绘画布保存位图

时间:2014-03-15 22:18:56

标签: android image canvas bitmap drawing

我试图在图像上绘制画布并保存更改。当应用程序启动时,我可以看到我的画布绘图,但它保存没有画布绘图。这是我的代码:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawView(this));
    }
    class DrawView extends View {
        Paint p;
        Path path;
        public DrawView(Context context) {
            super(context);
            p = new Paint();
            p.setStrokeWidth(30);
            p.setStyle(Paint.Style.STROKE);
            path = new Path();
        }

        @Override
        protected void onDraw(Canvas can) {
            Bitmap bm = BitmapFactory.decodeFile("/sdcard/MANUAL/img1394916221.png");
            can.drawBitmap(bm, 0, 0, p);
            path.reset();
            path.moveTo(250, 100);
            path.lineTo(300, 200);
            path.lineTo(200, 200);
            path.close();
            p.setColor(Color.RED);
            can.drawPath(path, p);

            FileOutputStream out;
            try {
                out = new FileOutputStream("/sdcard/MANUAL/img.png");
                bm.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

检查此样本。如何将视图导出为位图 http://gmariotti.blogspot.it/2013/10/how-to-export-view-as-bitmap.html?m=1