Android:位图:如何在Android中保存绿色背景的画布?

Android:位图:如何在Android中保存绿色背景的画布?,android,bitmap,Android,Bitmap,我正在使用位图创建数字签名图像。在设备上存储签名时,只有签名以黑色背景存储。我想要有签名的绿色背景 这是我的位图代码 // Bitmap View public class MyView extends View implements OnClickListener { public int height; public int width; private Bitmap mBitmap; private Path mPat

我正在使用位图创建数字签名图像。在设备上存储签名时,只有签名以黑色背景存储。我想要有签名的绿色背景

这是我的位图代码

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

我可以在创建签名时看到绿色背景,但它保存在黑色的黑底上。请帮助我,提前谢谢

尝试在onDraw中更改此选项

 canvas.drawColor(0xFFFF0000); 

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

请尝试使用此代码保存文件

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

正如您所发布的,您希望保存的内容将起作用。

canvas.drawColor
更改画布,但不更改可填充位图的位图

bitmap.eraseColor(color);

但它不是用绿色背景保存,而是用黑色背景颜色存储。请帮帮我。我错在哪里不知道。。。感谢Sachipost saveAsJpg()方法代码…@rahul我们需要在
onSizeChanged
方法中设置画布背景,因为您用于保存它的位图使用了
onSizeChanged
canvas
,并且您在
onDraw
中设置了画布颜色,因此它将显示,但它不适用于位图。请检查我的更新代码。