2011-11-22 1 views
0

이 데모를 사용하여 응용 프로그램에서 페인트를 구현하고 있습니다. This Demo.이 예제를 사용하여 이미지에 지우개를 구현하고 저장하는 방법은 무엇입니까?

이제 이미지를 페인트의 배경으로 사용하고 싶습니다. 그 후에 그 그림에 대한 페인트는 페인트와 같은 기능성을 지니 듯이 지워 져야합니다. 그리고 그 이미지를 저장하는 동안 그 이미지로 저장해야합니다.

그래서 내가 어떻게해야합니까 ?? 업데이트

: 저장 코드 :

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();   
     }   
    } 
} 

답변

2

작은 변화. mBackground

 @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); 
     } 

MyView의 생성자가 초기화 Bitmap이다.

문제가 있으면 다시 신고해야합니다.

업데이트 : 위의 변경을 잊지 댓글이 경우

를 참조하고,이 같은 onDraw 방법을 수정합니다.

저장하는 동안 두 비트 맵을 새 비트 맵 객체 위에 오버레이 한 다음 마침내 디스크에 기록해야합니다.

는 모두 비트 맵을 겹쳐

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; 
    } 
관련 문제