如何使用此示例实现图像擦除并保存?

时间:2011-11-22 12:17:29

标签: android image android-canvas paint

我正在使用这个演示来补充我的应用程序中的绘画。 This Demo

现在我希望Image作为绘画的背景。之后,应该擦除在该图像上完成的油漆,就像它现在具有油漆功能一样。当我保存该图像时,它应该与该图像一起保存。

那我该怎么办呢?

更新: 保存代码:

 case PHOTO_SAVE:
            final Activity currentActivity  = this;
            Handler saveHandler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    final AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
                    alertDialog.setTitle("Drawing App");
                    alertDialog.setMessage("Your drawing is saved. :)");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }
            } ;
            System.out.println("1");
           new ExportBitmapToFile(this,saveHandler, mBitmap).execute();
           System.out.println("2");
           return true;

ExportBitmapToFile类是:

private class ExportBitmapToFile extends AsyncTask<Intent,Void,Boolean> {
    private Context mContext;
    private Handler mHandler;
    private Bitmap nBitmap;
    private ProgressDialog  m_progressDialog = null; 
    @Override     
    protected void onPreExecute(){         
        m_progressDialog = new ProgressDialog(mContext);  
        m_progressDialog.setTitle("Drawing App");
        m_progressDialog.setMessage("Please wait...");
        m_progressDialog.setCancelable(false);         
        m_progressDialog.show();     
        System.out.println("3");
    }

    public ExportBitmapToFile(Context context,Handler handler,Bitmap bitmap) {
        mContext = context;
        nBitmap = bitmap;
        mHandler = handler;
    }

    @Override
    protected Boolean doInBackground(Intent... arg0) {
        try {
            if (!APP_FILE_PATH.exists()) {
                APP_FILE_PATH.mkdirs();
            }
            System.out.println("4");
            final FileOutputStream out = new FileOutputStream(new File(APP_FILE_PATH + "/"+filename+".jpg"));
            nBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            System.out.println("5");
            out.flush();
            out.close();
            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        //mHandler.post(completeRunnable);
        return false;
    }

    @Override
    protected void onPostExecute(Boolean bool) {
        super.onPostExecute(bool);
        if ( bool ){
            mHandler.sendEmptyMessage(1);
        }
        if (m_progressDialog.isShowing()) {             
            m_progressDialog.dismiss();          
        }         
    }
}

1 个答案:

答案 0 :(得分:2)

小改变。

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createScaledBitmap(mBackground, w, h, true);
            mCanvas = new Canvas(mBitmap);
        }

mBackground是您在Bitmap的构造函数中初始化的MyView

我很确定,如果你有任何问题需要报告,那应该这样做。

更新:见评论

在这种情况下,请忘记上述更改,并像这样修改onDraw方法。

  @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);
        canvas.drawBitmap(mBackground, 0, 0, mBitmapPaint); //Make sure mBackground is the same size as the view. 
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);
    }

保存时,您必须将两个位图叠加到新的位图对象上,然后最终将其写入磁盘。

重叠两个位图,

private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) { //code borrowed from stackoverflow question 1540272
        Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmp1, new Matrix(), null); // or use the other overloaded functions
        canvas.drawBitmap(bmp2, new Matrix(), null);
        return bmOverlay;
    }