在画布上画上没有删除功能删除背景图片 [英] draw on canvas without removing the background image on erase function

查看:175
本文介绍了在画布上画上没有删除功能删除背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制和擦除图像,然后将其保存到SD卡。我在绘制并保存到SD卡上得到了成功,但是当我在SD卡中检查图像保存在我画我有一个黑色的背景图像不是图像。

请建议我如何应用图片作为背景的​​画布和擦除绘制文本不失背景图像。我看到的堆栈溢出,但我不好没有一个为我工作的各种例子,请帮助。

我需要的:

我从SD卡获取什么:

下面是我的心血:

 公共类HomeActivity延伸活动{
私人位图DrawBitmap;
私人帆布mCanvas;
私人路径的mpath;
私人涂料DrawBitmapPaint;
的RelativeLayout器Rl;
CustomView视图;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.home);    动作条动作条= getActionBar();
    actionBar.setDisplayShowTitleEnabled(假);    this.loadActivity();
}私人涂料mPaint;公共类CustomView扩展视图{    公共CustomView(上下文C){        超(C);        create_image();        setLayerType(android.view.View.LAYER_TYPE_SOFTWARE,mPaint);    }    @覆盖
    保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
        // mCanvas.drawColor(Color.BLUE);        super.onSizeChanged(W,H,oldw,oldh);
    }    @覆盖
    保护无效的onDraw(帆布油画){
        setDrawingCacheEnabled(真);
        canvas.drawBitmap(DrawBitmap,0,0,DrawBitmapPaint);
        canvas.drawPath(的mpath,mPaint);        canvas.drawRect(我,0,我,0,DrawBitmapPaint);
    }    私人浮动MX,我的;
    私有静态最终浮动TOUCH_TOLERANCE = 4;    私人无效touch_start(浮法X,浮法Y){
        mPath.reset();
        mPath.moveTo(X,Y);
        MX = X;
        我= Y;
    }    私人无效TOUCH_MOVE(浮法X,浮法Y){
        浮DX = Math.abs(X - MX);
        浮DY = Math.abs(Y - 我的);
        如果(DX> = || TOUCH_TOLERANCE DY> = TOUCH_TOLERANCE){
            mPath.quadTo(MX,MY,(X + MX)/ 2,(Y + MY)/ 2);
            MX = X;
            我= Y;
        }
    }    私人无效touch_up(){
        mPath.lineTo(MX,我的);        mCanvas.drawPath(的mpath,mPaint);        mPath.reset();
    }    @覆盖
    公共布尔onTouchEvent(MotionEvent事件){
        浮X = event.getX();
        浮Y = event.getY();        开关(event.getAction()){
        案例MotionEvent.ACTION_DOWN:
            touch_start(X,Y​​);
            无效();
            打破;
        案例MotionEvent.ACTION_MOVE:
            TOUCH_MOVE(X,Y);
            无效();
            打破;
        案例MotionEvent.ACTION_UP:
            润色();
            performClick();
            无效();
            打破;
        }
        返回true;
    }    公共无效清除(){
        create_image();        //后来加..
        mPaint =新的油漆();
        mPaint.setAntiAlias​​(真);
        mPaint.setDither(真);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        // mCanvas.drawColor(Color.BLUE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap​​(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
        this.invalidate();
    }}公共无效loadActivity(){    查看=新CustomView(本);
    RL =(RelativeLayout的)findViewById(R.id.Rel);
    Rl.addView(查看);
    mPaint =新的油漆();
    mPaint.setAntiAlias​​(真);
    mPaint.setDither(真);
    // mCanvas.drawColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap​​(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(10);}公共无效create_image(){    DisplayMetrics displaymetrics =新DisplayMetrics();
    。getWindowManager()getDefaultDisplay()getMetrics(displaymetrics)。
    INT屏幕宽度= displaymetrics.widthPixels;
    INT screenHeight = displaymetrics.heightPixels;
    DrawBitmap = Bitmap.createBitmap(屏幕宽度,screenHeight,
            Bitmap.Config.ARGB_4444);    mCanvas =新的Canvas(DrawBitmap);
    mCanvas.drawColor(R.drawable.myBackgroundImage);    的mpath =新路径();
    DrawBitmapPaint =新的油漆(Paint.ANTI_ALIAS_FLAG);}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    。getMenuInflater()膨胀(R.menu.action_menu,菜单);    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    mPaint.setXfermode(NULL);
    开关(item.getItemId()){
    案例R.id.erase:
        mPaint.setStrokeWidth(40);        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(10);        打破;
    案例R.id.DELETE:        View.clear();
        mCanvas.drawColor(Color.BLUE);
        打破;
    案例R.id.color_Red:
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.RED);        打破;
    案例R.id.color_Green:
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.GREEN);        打破;
    案例R.id.color_Yello:
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.YELLOW);        打破;    案例R.id.color_Black:
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.BLACK);        打破;    案例R.id.draw:
        mPaint.setStrokeWidth(8);
        mPaint.setXfermode(NULL);        打破;    案例R.id.Save:
        。字符串根= Environment.getExternalStorageDirectory()的toString();
        文件MYDIR =新的文件(根+/ saved_images);
        myDir.mkdirs();
        随机数发生器=新的随机();
        INT N = 10000;
        N = generator.nextInt(N);
        字符串FNAME =图像 - + N +.JPG;
        档案文件=新的文件(MYDIR,FNAME);
        如果(file.exists())
            file.delete();        尝试{
            FileOutputStream中出=新的FileOutputStream(文件);
            DrawBitmap.com preSS(Bitmap.Com pressFormat.JPEG,90出);
            了out.flush();
            out.close();
            Toast.makeText(这一点,文件保存::+ FNAME,
                    Toast.LENGTH_SHORT).show();            sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse(文件://
                            + Environment.getExternalStorageDirectory())));        }赶上(例外五){
            Toast.makeText(这一点,ERROR+ e.toString(),Toast.LENGTH_SHORT)
                    。显示();
        }
    }    返回super.onOptionsItemSelected(项目);
}


解决方案

我已经使用这个code解决了这个问题。
当u保存此只画在画布上的位图与你的愿望的背景。

 公共类DrawingView扩展视图{
市民漆mPaint;
公众诠释宽度;
公众诠释高度;
私人位图mBitmap;
私人帆布mCanvas;
私人路径的mpath;
私人涂料mBitmapPaint;
静态上下文的背景下;
私人涂料circlePaint;
公共布尔eraseMode = FALSE;
私人路径circlePath;公共DrawingView(上下文C){
    超(C);
    上下文= C;
    初始化();
}公共无效的initialize(){
    的mpath =新路径();
    mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
    circlePaint =新的油漆();
    circlePath =新路径();
    circlePaint.setAntiAlias​​(真);
    circlePaint.setColor(Color.TRANSPARENT);
    circlePaint.setStyle(Paint.Style.STROKE);
    circlePaint.setStrokeJoin(Paint.Join.MITER);
    circlePaint.setStrokeWidth(4F);
    mPaint =新的油漆();
    mPaint.setAntiAlias​​(真);
    mPaint.setDither(真);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap​​(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(6);
}@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
    super.onSizeChanged(W,H,oldw,oldh);    mBitmap = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
    mCanvas =新的Canvas(mBitmap);
}@覆盖
保护无效的onDraw(帆布油画){
    canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
    canvas.drawPath(circlePath,circlePaint);
    super.onDraw(画布);
}私人浮动MX,我的;
私有静态最终浮动TOUCH_TOLERANCE = 4;私人无效touch_start(浮法X,浮法Y){
    mPath.reset();
    mPath.moveTo(X,Y);
    MX = X;
    我= Y;
}私人无效TOUCH_MOVE(浮法X,浮法Y){
    浮DX = Math.abs(X - MX);
    浮DY = Math.abs(Y - 我的);
    如果(DX> = || TOUCH_TOLERANCE DY> = TOUCH_TOLERANCE){
        mPath.quadTo(MX,MY,(X + MX)/ 2,(Y + MY)/ 2);
        MX = X;
        我= Y;        circlePath.reset();
        circlePath.addCircle(MX,我,30,Path.Direction.CW);
        mCanvas.drawPath(的mpath,mPaint);
    }
}私人无效touch_up(){
    mPath.lineTo(MX,我的);
    circlePath.reset();
    mCanvas.drawPath(的mpath,mPaint);
    mPath.reset();
}@覆盖
公共布尔onTouchEvent(MotionEvent事件){
    浮X = event.getX();
    浮Y = event.getY();    开关(event.getAction()){
    案例MotionEvent.ACTION_DOWN:
        touch_start(X,Y​​);
        无效();
        打破;
    案例MotionEvent.ACTION_MOVE:
        TOUCH_MOVE(X,Y);
        无效();
        打破;
    案例MotionEvent.ACTION_UP:
        润色();
        无效();
        打破;
    }
    返回true;
}公共无效setErase(){
    mPaint.setXfermode(NULL);
    mPaint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    eraseMode = TRUE;
}公共无效removeErase(){
    mPaint.setXfermode(NULL);
     eraseMode = FALSE;
}公共无效SaveImage(){
    位图此搜索= Bitmap.createScaledBitmap(mBitmap,1080,780,真正的);    文件的文件路径= Environment.getExternalStorageDirectory();
    档案文件=新的文件(filepath.getAbsolutePath()+/ NumberAndAlphabet);
    如果(!file.exists()){
        file.mkdir();
    }
    日期日期=新的日期(System.currentTimeMillis的());
    SimpleDateFormat的格式化=新的SimpleDateFormat(YYYYMMDDHHMMSS
            Locale.getDefault());
    字符串时间= formatter.format(日期);
    文件DIR =新的文件(文件,图像+时间+。PNG);
    OutputStream的输出= NULL;
    尝试{
        输出=新的FileOutputStream(DIR);
    }赶上(FileNotFoundException异常五){
        e.printStackTrace();
    }
    位图newbitmap = Bitmap.createBitmap(image1.getWidth(),image1.getHeight(),image1.getConfig());
    帆布能=新的Canvas(newbitmap);
    can.drawColor(Color.WHITE);
    can.drawBitmap(image1的,0,0,NULL);
    newbitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,输出);
    Toast.makeText(背景下,图像保存...,Toast.LENGTH_SHORT).show();
}

}

I am trying to draw and erase on image and then save it to sd card. I got success in drawing and saving it to sd card, but when i checked the save image in sd card I got a black background image not the image at i draw.

Please suggest me how to apply image as a canvas background and erase draw text without losing the background image. I have seen various example on stack overflow but my bad no one is working for me, pls help.

What I need :

What I am getting from sd card :

Here is my effort :

public class HomeActivity extends Activity {
private Bitmap DrawBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint DrawBitmapPaint;
RelativeLayout Rl;
CustomView View;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);

    this.loadActivity();
}

private Paint mPaint;

public class CustomView extends View {

    public CustomView(Context c) {

        super(c);

        create_image();

        setLayerType(android.view.View.LAYER_TYPE_SOFTWARE, mPaint);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // mCanvas.drawColor(Color.BLUE);

        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        setDrawingCacheEnabled(true);
        canvas.drawBitmap(DrawBitmap, 0, 0, DrawBitmapPaint);
        canvas.drawPath(mPath, mPaint);

        canvas.drawRect(mY, 0, mY, 0, DrawBitmapPaint);
    }

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

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

        mCanvas.drawPath(mPath, mPaint);

        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();
            performClick();
            invalidate();
            break;
        }
        return true;
    }

    public void clear() {
        create_image();

        // Added later..
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        // mCanvas.drawColor(Color.BLUE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
        this.invalidate();
    }

}

public void loadActivity() {

    View = new CustomView(this);
    Rl = (RelativeLayout) findViewById(R.id.Rel);
    Rl.addView(View);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    // mCanvas.drawColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(10);

}

public void create_image() {

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenWidth = displaymetrics.widthPixels;
    int screenHeight = displaymetrics.heightPixels;
    DrawBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
            Bitmap.Config.ARGB_4444);

    mCanvas = new Canvas(DrawBitmap);
    mCanvas.drawColor(R.drawable.myBackgroundImage);

    mPath = new Path();
    DrawBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.action_menu, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    mPaint.setXfermode(null);
    switch (item.getItemId()) {
    case R.id.erase:
        mPaint.setStrokeWidth(40);

        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(10);

        break;
    case R.id.DELETE:

        View.clear();
        mCanvas.drawColor(Color.BLUE);
        break;
    case R.id.color_Red:
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.RED);

        break;
    case R.id.color_Green:
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.GREEN);

        break;
    case R.id.color_Yello:
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.YELLOW);

        break;

    case R.id.color_Black:
        mPaint.setStrokeWidth(8);
        mPaint.setColor(Color.BLACK);

        break;

    case R.id.draw:
        mPaint.setStrokeWidth(8);
        mPaint.setXfermode(null);

        break;

    case R.id.Save:
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-" + n + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists())
            file.delete();

        try {
            FileOutputStream out = new FileOutputStream(file);
            DrawBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            Toast.makeText(this, "File Saved ::" + fname,
                    Toast.LENGTH_SHORT).show();

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));

        } catch (Exception e) {
            Toast.makeText(this, "ERROR" + e.toString(), Toast.LENGTH_SHORT)
                    .show();
        }
    }

    return super.onOptionsItemSelected(item);
}

解决方案

I have solved this problem by using this code. When u save this just draw bitmap on canvas with your desire background.

public class DrawingView extends View {


public Paint mPaint;
public int width;
public int height;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
static Context context;
private Paint circlePaint;
public boolean eraseMode = false;
private Path circlePath;

public DrawingView(Context c) {
    super(c);
    context = c;
    initialize();
}

public void initialize(){
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    circlePaint = new Paint();
    circlePath = new Path();
    circlePaint.setAntiAlias(true);
    circlePaint.setColor(Color.TRANSPARENT);
    circlePaint.setStyle(Paint.Style.STROKE);
    circlePaint.setStrokeJoin(Paint.Join.MITER);
    circlePaint.setStrokeWidth(4f);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(6);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(circlePath, circlePaint);
    super.onDraw(canvas);
}

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

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;

        circlePath.reset();
        circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
        mCanvas.drawPath(mPath, mPaint);
    }
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    circlePath.reset();
    mCanvas.drawPath(mPath, mPaint);
    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;
}

public void setErase() {
    mPaint.setXfermode(null);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    eraseMode = true;
}

public void removeErase() {
    mPaint.setXfermode(null);
     eraseMode = false;
}

public void SaveImage() {
    Bitmap image1 = Bitmap.createScaledBitmap(mBitmap, 1080, 780, true);

    File filepath = Environment.getExternalStorageDirectory();
    File file = new File(filepath.getAbsolutePath() + "/NumberAndAlphabet");
    if (!file.exists()) {
        file.mkdir();
    }
    Date date = new Date(System.currentTimeMillis());
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss",
            Locale.getDefault());
    String time = formatter.format(date);
    File dir = new File(file, "Image" + time + ".PNG");
    OutputStream output = null;
    try {
        output = new FileOutputStream(dir);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Bitmap newbitmap = Bitmap.createBitmap(image1.getWidth(), image1.getHeight(), image1.getConfig());
    Canvas can = new Canvas(newbitmap);
    can.drawColor(Color.WHITE);
    can.drawBitmap(image1, 0, 0, null);
    newbitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
    Toast.makeText(context, "Image Saved...", Toast.LENGTH_SHORT).show();
}

}

这篇关于在画布上画上没有删除功能删除背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆