Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Bitmap : How to save canvas with green background in android?

Tags:

android

bitmap

I am using Bitmap for creating digital signature Image. While storing the Signature on device only signature got stored with black background. I want green background with signature.

Here it's my Bitmap code

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

I can able to see the green background while creating signature, but it is saved on black blackground. please help me out, Thanks in advance

like image 266
Rahul Baradia Avatar asked Mar 28 '12 04:03

Rahul Baradia


3 Answers

@rahul You can also use this in onDraw

 canvas.drawColor(Color.GREEN);   

Please Check update of my code

@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);
}
like image 101
Herry Avatar answered Nov 15 '22 11:11

Herry


Try change this in onDraw

 canvas.drawColor(0xFFFF0000); 
like image 37
sachi Avatar answered Nov 15 '22 12:11

sachi


canvas.drawColor changes the canvas but not the bitmap you can fill the bitmap with

bitmap.eraseColor(color);
like image 24
99mandolins Avatar answered Nov 15 '22 13:11

99mandolins