2012-03-28 46 views
7

我正在使用位圖來創建數字簽名圖像。在設備上存儲簽名時,只有簽名以黑色背景存儲。我想要簽名的綠色背景。Android:位圖:如何在Android中保存具有綠色背景的畫布?

這是我的位圖碼

// Bitmap View 
public class MyView extends View implements OnClickListener 
{ 
    public int height; 
    public int width;  
    private Bitmap mBitmap;   
    private Path mPath; 
    private Paint mBitmapPaint; 

    public MyView(Context c) 
    { 
     super(c); 
     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) 
    { 
     super.onSizeChanged(w, h, oldw, oldh); 
     Wid = w; 
     Ht = h; 
     mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);    
     mCanvas = new Canvas(mBitmap);  

    } 

    @Override 
    protected void onDraw(final Canvas canvas) 
    { 

     canvas.drawColor(0xFFFFFFFF);  
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
     canvas.drawPath(mPath, mPaint); 

     // onclick listner for SAVE button 
     saveButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       //capture the image 
       try {      
        saveAsJpg(mBitmap);  
        startActivity(new Intent(Paint.this, SignatureActivity.class)); 
        finish(); 
       } catch (IOException e) {     
        e.printStackTrace(); 
       } 
      } 
     });    
    } 

    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; 
     System.out.println("---- " +mX); 
    } 
    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, 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; 
    } 

我可以看到綠色背景而產生的簽名,但它被保存在黑色blackground。請幫助我,在此先感謝

+0

發佈saveAsJpg()方法的代碼.... – himanshu 2012-03-28 05:20:27

+1

@rahul我們需要設置畫布底色中'onSizeChanged'的方法,因爲您用來保存的位圖使用'onSizeChanged'的'canvas',並且已經在'onDraw'中設置了Canvas顏色,所以它會顯示,但它不適用於位圖。請檢查我更新的代碼。 – Herry 2012-03-28 06:47:55

回答

14

@rahul您還可以在onDraw

canvas.drawColor(Color.GREEN); 

使用請查看我的代碼更新

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);    
    mCanvas = new Canvas(mBitmap); 
    mCanvas.drawColor(Color.GREEN); 
    super.onSizeChanged(w, h, oldw, oldh); 
} 
1

嘗試改變這一點的onDraw

canvas.drawColor(0xFFFF0000); 
+0

但它不是用綠色背景保存而是用黑色背景色存儲。請幫助我。在哪裏我錯了不知道...謝謝Sachi – 2012-03-28 05:08:00

+0

@rahul你可以在'saveAsJpg(mBitmap);'中顯示你的代碼。 – Herry 2012-03-28 05:20:28

0

試用本代碼保存文件

FileOutputStream fos = new FileOutputStream(Yourpath); 
bitmap.compress(CompressFormat.JPEG, 80, fos); 

正如你已經發布,你想保存這將工作。

1

canvas.drawColor改變畫布,但不是位圖,你可以填寫位圖

bitmap.eraseColor(color); 
相關問題