如何仅在画布中清除绘图?

时间:2013-03-25 09:17:41

标签: android android-canvas

我正在尝试开发一个应用程序。我现在要清除画布,我编写了清除屏幕的编码。它的工作..但一个问题背景图像也被清除。我只想清除图纸。如何清除图纸?在这里我动态设置背景图像。

package com.example.graphicsdraw;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
    private Button Back, Next, Erase,clrScreen;
    int FlagSet = 0, ImageSet = 0;
    static int Flag = 0;
    MyView myview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myview = new MyView(this);
        RelativeLayout myLayout = new RelativeLayout(this);        
        myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        Back = new Button(this); 
        Back.setText("Back"); 
        Next = new Button(this);
        Next.setText("Next");
        Erase = new Button(this);
        Erase.setText("Eraser");
        clrScreen = new Button(this);
        clrScreen.setText("Clear");
        myLayout.addView(myview); 
        myLayout.addView(Back); 
        myLayout.addView(Next);
        myLayout.addView(Erase);
        myLayout.addView(clrScreen);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(60, LayoutParams.WRAP_CONTENT); 
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(60, LayoutParams.WRAP_CONTENT); 
        params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
        params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
        RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(65, LayoutParams.WRAP_CONTENT); 
        params3.addRule(RelativeLayout.CENTER_HORIZONTAL); 
        params3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        Erase.setLayoutParams(params3); 
        Erase.bringToFront(); 
        Next.setLayoutParams(params2); 
        Next.bringToFront(); 
        Back.setLayoutParams(params); 
        Back.bringToFront(); 
        clrScreen.setLayoutParams(params3); 
        clrScreen.bringToFront(); 
        this.setContentView(myLayout);


        Back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, MainActivity.class);
                Flag = Flag - 1;
                startActivity(i);
                finish();

            }
        });
        Next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, MainActivity.class);
                Flag = Flag + 1;
                startActivity(i);
                finish();

            }
        });

        Erase.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                /*Intent i = new Intent(MainActivity.this, MainActivity.class);
                Flag = Flag;
                startActivity(i);
                finish();*/
                mPaint = new Paint();
                mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
                mPaint.setColor(Color.BLUE);
                mPaint.setAntiAlias(true); 
            }
        });
        clrScreen.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                myview.Clear_Drawing();

            }


        });

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.WHITE);//0xffffffff
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(10);
        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
        mPaint.setMaskFilter(mEmboss);
    }
    private Paint       mPaint;
    private MaskFilter  mEmboss;
    public  class MyView extends View 
    {
        MainActivity ma=new MainActivity();
        Bitmap  mBitmap = null;
        private Canvas  mCanvas;
        Path    mPath = null;
        private Paint   mBitmapPaint;
        Bitmap bmp;
        Display  display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        public MyView(Context c) {
            super(c);
            switch(Flag)
            {
            case 0:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 1:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 2:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 3:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 4:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 5:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 6:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 7:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 8:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 9:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 10:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 11:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 12:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 13:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 14:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 15:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 16:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 17:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 18:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 19:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 20:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 21:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 22:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 23:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 24:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 25:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 26:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 27:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 28:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 29:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 30:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 31:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 32:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 33:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 34:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 35:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 36:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 37:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 38:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 39:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 40:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 41:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 42:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 43:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 44:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            case 45:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                break;
            case 46:
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a);
                break;
            default:
                break;
            }
            //Display  display = getWindowManager().getDefaultDisplay();
           // int width = display.getWidth();
            mBitmap = Bitmap.createScaledBitmap(bmp, width, 350, true);
            //mBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) ;                 

        }     
        protected void onDraw(Canvas canvas)
        {
            canvas.drawColor(Color.TRANSPARENT);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawPath(mPath, mPaint);
        }
        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();
        }
        public void Clear_Drawing()
        {
            //mBitmap = null;
            mPath = null;
            mBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888);
            mPath = new Path();
        }
       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;
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您应该使用myview.setBackground()作为背景,而不是直接在背景上绘图。继续使用Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888)创建一个空白位图以便绘图。

mBitmap = Bitmap.createScaledBitmap(bmp, width, 350, true);
setBackground(mBitmap);
mBitmap = Bitmap.createBitmap(width, 350, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);

答案 1 :(得分:0)

它肯定会清除背景图像,因为它是画布的一部分你应该做的是使用framelayout地方背景作为第一个孩子并在其上创建透明画布并在framelayout中改变第一个孩子的背景在画布上绘制图像

在透明画布下创建一个布局,当用户更改背景图片时,在布局下方的图片上更改该图片而不是在画布上,休息就可以了

答案 2 :(得分:0)

这就像重置绘图工具并提供一个新的画布来绘制。

 mBitmap = Bitmap.createBitmap(mv.getWidth(),mv.getHeight() ,Bitmap.Config.ARGB_8888);      
 mCanvas = new Canvas(mBitmap);
 mPaint = new Paint();
 mPaint.setAntiAlias(true);
 mPaint.setDither(true);
 mPaint.setColor(0xFFFF0000);
 mPaint.setStyle(Paint.Style.STROKE);                               
 mPaint.setStrokeJoin(Paint.Join.ROUND);       
 mPaint.setStrokeCap(Paint.Cap.ROUND);
 mPaint.setStrokeWidth(0);
 mv.invalidate();// call invalidate to refresh the view's draw.