2012-03-28 13 views
7

Estoy usando Bitmap para crear una imagen de firma digital. Al almacenar la Firma en el dispositivo, solo la firma se almacenó con fondo negro. Quiero fondo verde con firma.Android: mapa de bits: cómo guardar un lienzo con fondo verde en android?

aquí Es mi código de mapa de bits

// 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; 
    } 

puedo capaz de ver el fondo verde, mientras que la creación de la firma, pero es salvado con fondo negro. por favor me ayude, gracias por adelantado

+0

publicar la saveAsJpg() Código del método .... – himanshu

+1

@rahul tenemos que establecer el fondo del lienzo en 'onSizeChanged' método porque El mapa de bits que ha usado para guardarlo tiene el uso 'canvas' of' onSizeChanged', y ha establecido el color Canvas en 'onDraw' para que se muestre, pero no se aplica a Bitmap. Compruebe mi código actualizado. – Herry

Respuesta

14

@rahul También puede utilizar esto en onDraw

canvas.drawColor(Color.GREEN); 

Por favor marque la actualización de mi código

@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

probar este cambio en onDraw

canvas.drawColor(0xFFFF0000); 
+0

Pero no se guarda con fondo verde sino que se almacena con un fondo negro. Por favor, ayúdame. Donde me estoy equivocando no lo sé ... Gracias Sachi –

+0

@rahul ¿me puede mostrar su código en 'saveAsJpg (mBitmap);'. – Herry

0

Pruebe este código para guardar el archivo

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

Como ha indicado que desea guardar esto, funcionará.

1

canvas.drawColor cambia el lienzo, pero no el mapa de bits que puede llenar el mapa de bits con

bitmap.eraseColor(color); 
Cuestiones relacionadas