2012-11-13 1 views
2

J'essaie de sauvegarder mon dessin dans Canvas et Framelayout avec un arrière-plan dessus, j'ai pu enregistrer le canevas et dessiner mais le problème est le FrameLayout. Il semble que mon code enregistre seulement la toile et non le Framelayout (image de fond). Est-ce que quelqu'un peut m'aider avec ça.Comment enregistrer le dessin dans le canevas et la mise en page avec le fond

public class Draw extends Activity implements { 


    public static String filex; 

    private static final int SELECT_PICTURE = 1; 

    public static String selectedImagePath; 
    public static Bitmap cBitmap; 

    public static Integer resize; 
    public static Integer imageBackgroundEraser; 
    // Instance variables 

    public static Paint  mPaint; 
    private MaskFilter mEmboss; 
    private MaskFilter mBlur; 
    private MyView  mView; 
    private int mImageCount = 0; 
    private int mLastBg = 0; 

    public static Integer WW; 
    public static Integer WH; 

    private float BrushWidth; 
    private int thickness; 
    private int BrushColor; 
    public static int backgroundColor; 
    float Mx1,My1; 
    float x,y; 

    @SuppressWarnings("deprecation") 
    @Override 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_draw); 

     Display display = getWindowManager().getDefaultDisplay(); //*** 


     final MyView myView = new MyView(this); 
     FrameLayout frm_layout=(FrameLayout) findViewById(R.id.main_frame); 
     frm_layout.addView(myView); 
     Drawable x = Drawable.createFromPath(selectedImagePath) 
     frm_layout.setBackgroundDrawable(x); 
     frm_layout.addView(myView); 

     setInitialPaint(); 

    } 

    private void setInitialPaint() { 
     //bgColor = 0xFFFFFFFF; // default bg color white 
     BrushWidth = 3; 
     BrushColor = 0xFF000000; //** 
     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(BrushColor); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(BrushWidth); 
     mPaint.setPathEffect(new CornerPathEffect(30)); 
     //mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },0.4f, 6, 3.5f); 

     //mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); 
    } 

    public void onClick(View view){ 
     switch (view.getId()){ 

     case R.id.saveBtn: 
      File myDir=new File("/sdcard/Pictures/Draw"); 
      myDir.mkdirs(); 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(System.currentTimeMillis())); //*** 
      String fname = timeStamp + ".png"; 
      File file = new File (myDir, fname); 
      System.out.println(file+"----> file"); 
      if (file.exists()) file.delete(); 
      saveAsPng(file); 
     break; 


     } 
    } 

    public void saveAsPng (File f) 
    { 
     String fname = f.getAbsolutePath(); 

     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream (f); 
      mView.mBitmap.compress (CompressFormat.PNG, 100, fos); 
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
      Toast.makeText (getApplicationContext(), "Saved " + fname, Toast.LENGTH_LONG).show(); 
     } catch (Throwable ex) { 
      Toast.makeText (getApplicationContext(), "Error: " + ex.getMessage(), Toast.LENGTH_LONG).show(); 
      ex.printStackTrace(); 
     } 

    } // end saveAsPng 

    } // end Main 

    public class MyView extends View { 

    // Constants and variables 
     private static final float MINP = 0.25f; 
     private static final float MAXP = 0.75f; 

     public static Bitmap mBitmap; 
     public static Canvas mCanvas; 
     private Path mPath; 
     private Paint mBitmapPaint; 
     private Bitmap cBitmap; 
     private Canvas xCanvas; 
     private Bitmap xBitmap; 



     public MyView(Context c) 
     { 
      super(c); 


       // Figure out how this works. Seems odd that we set up a canvas and a bitmap. 
       // I don't see how it connects to what shows on screen.  
       mBitmap = Bitmap.createBitmap(Draw4FunMain.WW, Draw4FunMain.WH, Bitmap.Config.ARGB_8888); 
       mCanvas = new Canvas(mBitmap); 
       mPath = new Path(); 
       mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
       // mCanvas.setBackgroundResource(0xFFFFFFFF); 
       if (Draw4FunMain.imageBackgroundEraser == null) { 
        mCanvas.drawColor (Color.WHITE); 
       } else { 

        mCanvas.drawColor (Color.TRANSPARENT); 
       } 

     } 

     public MyView (Context c, int color) 
     { 
      super(c); 

       mBitmap = Bitmap.createBitmap(Draw4FunMain.WW, Draw4FunMain.WH, Bitmap.Config.ARGB_8888); 
       mCanvas = new Canvas(mBitmap); 
       mPath = new Path(); 
       mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
       mCanvas.drawColor (color); 
       //Draw4FunMain.backgroundColor = color; 
     } 

    /** 
    */ 
    // Methods 

    /** 
    */ 
    @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) 
    { 
     super.onSizeChanged(w, h, oldw, oldh); 
    } 

    /** 
    */ 
    @Override protected void onDraw(Canvas canvas) 
    { 

      //canvas.drawColor(Color.TRANSPARENT); 

      canvas.drawBitmap(mBitmap, 0, 0, null); 

      canvas.drawPath(mPath, Draw4FunMain.mPaint); 

    } 

    /** 
    */ 
     private float mX, mY; 
     private static final float TOUCH_TOLERANCE = 4; 

    /** 
    */ 
     private void touch_start(float x, float y) { 
      mPath.reset(); 
      mPath.moveTo(x, y); 
      mX = x; 
      mY = y; 
     } 
    /** 
    */ 
     private void touch_move(float x, float y) { 
      float dx = Math.abs(x - mX); 
      float dy = Math.abs(y - mY); 
      if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
       mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
       mX = x; 
       mY = y; 
      } 
     } 
    /** 
    */ 
     private void touch_up() { 
      mPath.lineTo(mX, mY); 
      // commit the path to our offscreen 
      mCanvas.drawPath(mPath, Draw4FunMain.mPaint); 
      // kill this so we don't double draw 
      mPath.reset(); 
     } 

    /** 
    */ 

    @Override public boolean onTouchEvent(MotionEvent event) { 
      float x = event.getX(); 
      float y = event.getY(); 

      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        touch_start(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        touch_move(x, y); 
        invalidate(); 
        break; 
       case MotionEvent.ACTION_UP: 
        touch_up(); 
        invalidate(); 
        break; 
      } 
      return true; 
     } 

    } // end MyView 

Répondre

0

que l'image d'arrière-plan défini pour FrameLayout est pas enregistré est parce que vous ne l'avez pas dessiner sur le bitmap que vous essayez d'enregistrer. Il existe un moyen facile de faire ce que vous voulez, vous prenez simplement une capture d'écran du FrameLayout, de sorte que tout ce qui est dans le FrameLayout sera capturé, et sauvegardez cette capture d'écran comme des octets bruts au fichier.

frameLayout.buildDrawingCache(); 
Bitmap bitmap = frameLayout.getDrawingCache(); 
bitmap.compress (CompressFormat.PNG, 100, fos); 
+0

merci neevek je l'ai eu. c'est presque le même que celui-ci http://stackoverflow.com/questions/5029775/save-the-image-made-by-user-in-fingerpaint-api-demos-in-android?rq=1 – Aid

+0

@Aid, si cela résout votre problème, s'il vous plaît marquer comme la réponse. – neevek

Questions connexes