图片已保存但没有背景

Image saved but without background

我创建了一个 Android 应用程序,我可以在其中在触摸屏上绘制任何内容,然后 Button 将其保存在 SD 卡中。

现在我的应用程序正在保存图像,但问题是它在保存图像时没有背景

代码:

 package com.example.writeonscreen;

import android.support.v7.app.ActionBarActivity;
import android.util.AttributeSet;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;


public class WriteOnScreenActivity extends ActionBarActivity {
private String state;
Bitmap bmp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TouchEventView(this,null));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.write_on_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.savepic) {
            state=Environment.getExternalStorageState();
            if(state.equals(Environment.MEDIA_MOUNTED)){


                 String fileJPG = null;
                 final File path =
                            Environment.getExternalStoragePublicDirectory
                            (
                                //Environment.DIRECTORY_PICTURES
                                //Environment.DIRECTORY_DCIM
                                Environment.DIRECTORY_DCIM + "/Utkarshpic/"
                            );

                        // Make sure the Pictures directory exists.
                        if(!path.exists())
                        {
                            path.mkdirs();
                        }

                        final File file = new File(path, fileJPG + ".jpg");

                        try
                        {
                            final FileOutputStream fos = new FileOutputStream(file);
                            final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

                            //bmp.compress(CompressFormat.JPEG, 100, bos);
                            bmp.compress(CompressFormat.JPEG, 85, bos);
Toast.makeText(getBaseContext(), "Picture saved in Utkarshpic", Toast.LENGTH_SHORT).show();
                            bos.flush();
                            bos.close();
                        }
                        catch (final IOException e)
                        {
                            e.printStackTrace();
                        }



            }else{
                Toast.makeText(getBaseContext(), "Cannot Save Image.No SD Card !", Toast.LENGTH_LONG).show();
            }

            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    public class TouchEventView extends View {
        Paint paint=new Paint();
        Path path=new Path();
        Bitmap myBitmap;
    public TouchEventView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            paint.setAntiAlias(true);
            paint.setColor(Color.BLUE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(5);
        }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.drawPath(path, paint);
        myBitmap=Bitmap.createBitmap((int)canvas.getWidth(), (int)canvas.getHeight(), Config.ARGB_8888);// bitmap created
        canvas=new Canvas(myBitmap);


        canvas.drawPath(path, paint);
        pass(myBitmap);

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        float xPos=event.getX();
        float yPos=event.getY();
        switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            path.moveTo(xPos, yPos);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(xPos, yPos);
            break;
        case MotionEvent.ACTION_UP:
            break;
            default:
                return false;

        }
        invalidate();

        return true;


    }


    }



    public void pass(Bitmap myBitmap) {
        // TODO Auto-generated method stub
        bmp=myBitmap;
    }

}

如何保存带有背景的图片?

好吧,您可以在绘制 Path 之前为 Canvas 绘制背景。

例如:

// drawing the background
canvas.drawARGB(255, 225, 225, 255);

// drawing the Path
canvas.drawPath(path, paint);

pass(myBitmap);

来自drawARGB()documentation

Fill the entire canvas' bitmap (restricted to the current clip) with the specified ARGB color, using srcover porterduff mode.