Android Photo Background Eraser need help- Not era

2019-05-24 08:01发布

问题:

I am developing 1 app in that i need to erase the Bitmap for that i have done is.... (code is working but just draw black lines..)

@Override
        protected void onDraw(Canvas canvas) {

            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            for (PathPoints p : paths) {
                mPaint.setColor(p.getColor());
                mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
                Log.v("", "Color code : " + p.getColor());
                if (p.isTextToDraw()) {
                    canvas.drawText(p.textToDraw, p.x, p.y, mPaint);
                } else {
                    canvas.drawPath(p.getPath(), mPaint);
                }
            }

But the above code only draws the black lines on Bitmap I have Seen this link But not getting Actually I want it LIKE THIS (in this image move mouse on imge that type of work i need.. but i am not able to remove portion of bitmap just draw black lines)What am I doing wrong????? Plz help me to sole this Problem

回答1:

Finally after 1 day i found the solution...

 public class FingerPaint extends Activity {
        LinearLayout mainContainer;
        LinearLayout buttonContainer;
        LinearLayout.LayoutParams btnParams;
        Button btnText, btnSketch, btnColor, btnUndo, btnRedo, btnDone;
        // MyView drawView;
        DrawingPanel drawView;
        int lastColor = 0xFFFF0000;
        public static final int SUCCESS = 200;

        private final String TAG = getClass().getSimpleName();
        private String textToDraw = null;
        private boolean isTextModeOn = false;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            generateViews();
        }

        private void generateViews() {
            btnDone = new Button(this);
            btnDone.setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            btnDone.setText(getString(R.string.btn_done));
            btnText = new Button(this);
            btnSketch = new Button(this);
            btnColor = new Button(this);
            btnUndo = new Button(this);
            btnRedo = new Button(this);
            mainContainer = new LinearLayout(this);
            mainContainer.setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            mainContainer.setOrientation(LinearLayout.VERTICAL);
            buttonContainer = new LinearLayout(this);
            buttonContainer.setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            buttonContainer.setOrientation(LinearLayout.HORIZONTAL);
            btnParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, 1);
            /*
             * btnText.setText("Text"); btnSketch.setText("Sketch");
             * btnColor.setText("Color"); btnUndo.setText("Undo");
             * btnRedo.setText("Redo");
             */

            btnText.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.text_mode_selector));
            btnText.setLayoutParams(btnParams);
            btnSketch.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.sketch_selector));
            btnSketch.setLayoutParams(btnParams);
            btnColor.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.color_selector));
            btnColor.setLayoutParams(btnParams);
            btnUndo.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.undo_selector));
            btnUndo.setLayoutParams(btnParams);
            btnRedo.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.redo_selector));
            btnRedo.setLayoutParams(btnParams);
            buttonContainer.addView(btnText);
            buttonContainer.addView(btnSketch);
            buttonContainer.addView(btnColor);
            buttonContainer.addView(btnUndo);
            buttonContainer.addView(btnRedo);
            // drawView=new MyView(this);
            drawView = new DrawingPanel(this, lastColor);
            drawView.setDrawingCacheEnabled(true);
            drawView.measure(
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            drawView.layout(0, 0, drawView.getMeasuredWidth(),
                    drawView.getMeasuredHeight());
            drawView.buildDrawingCache(true);
            drawView.setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
            mainContainer.addView(btnDone);
            mainContainer.addView(drawView);
            mainContainer.addView(buttonContainer);
            setContentView(mainContainer);
            btnSketch.setSelected(true);
            btnText.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    resetButtons();
                    btnText.setSelected(true);
                    AlertDialog.Builder alert = new AlertDialog.Builder(
                            FingerPaint.this);
                    alert.setMessage(getString(R.string.msg_enter_text_to_draw));
                    final EditText edText = new EditText(FingerPaint.this);
                    alert.setView(edText);
                    alert.setPositiveButton(R.string.btn_ok,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    if (edText.getText().toString().length() > 0) {
                                        textToDraw = edText.getText().toString();
                                        isTextModeOn = true;
                                        ChooseActivity.displayAlert(
                                                FingerPaint.this,
                                                getString(R.string.msg_tap_image));
                                    } else {
                                        ChooseActivity
                                                .displayAlert(
                                                        FingerPaint.this,
                                                        getString(R.string.msg_enter_text_to_draw));
                                    }
                                }
                            });
                    alert.setNegativeButton(R.string.btn_cancel,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    isTextModeOn = false;
                                }
                            });
                    alert.show();
                }
            });
            btnSketch.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    resetButtons();
                    btnSketch.setSelected(true);
                    isTextModeOn = false;
                }
            });
            btnColor.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    AmbilWarnaDialog dialog = new AmbilWarnaDialog(
                            FingerPaint.this, lastColor,
                            new OnAmbilWarnaListener() {
                                @Override
                                public void onOk(AmbilWarnaDialog dialog, int color) {
                                    // color is the color selected by the user.
                                    colorChanged(color);
                                }

                                @Override
                                public void onCancel(AmbilWarnaDialog dialog) {
                                    // cancel was selected by the user
                                }
                            });

                    dialog.show();
                }
            });

            btnUndo.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    drawView.onClickUndo();
                }
            });
            btnRedo.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    drawView.onClickRedo();
                }
            });
            btnDone.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.v(TAG, "Here");
                    Bitmap editedImage = Bitmap.createBitmap(drawView
                            .getDrawingCache());
                    editedImage = Bitmap.createScaledBitmap(editedImage, 200, 300,
                            true);
                    if (editedImage != null) {
                        Intent intent = new Intent();
                        intent.putExtra(ChooseActivity.BITMAP, editedImage);
                        // AddReportItemActivity.mPhoto =
                        // drawView.getDrawingCache();
                        setResult(SUCCESS, intent);
                        finish();
                    }
                }
            });
        }

        public void resetButtons() {
            btnText.setSelected(false);
            btnSketch.setSelected(false);
            btnColor.setSelected(false);
            btnUndo.setSelected(false);
            btnRedo.setSelected(false);
        }

        public class DrawingPanel extends ImageView implements OnTouchListener {
            Paint paint = new Paint();
            private Canvas mCanvas, c2;
            private Path mPath;
            private Paint mPaint, mBitmapPaint;
            private ArrayList<PathPoints> paths = new ArrayList<PathPoints>();
            private ArrayList<PathPoints> undonePaths = new ArrayList<PathPoints>();
            private Bitmap mBitmap, Transparent, Bitmap2;
            private int color;
            private int x, y;
            private boolean isTouched = false;
            public DrawingPanel(Context context, int color) {
                super(context);

                this.color = color;
                setFocusable(true);
                setFocusableInTouchMode(true);
                DisplayMetrics metrics = context.getResources().getDisplayMetrics();
                int width = metrics.widthPixels;
                int height = metrics.heightPixels;
                this.setOnTouchListener(this);
                Transparent = Bitmap.createBitmap(width, height,
                        Bitmap.Config.ARGB_8888);
                mBitmapPaint = new Paint(Paint.DITHER_FLAG);
                Bitmap2 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.test);
            //  setBackgroundResource(R.drawable.test);
                mPaint = new Paint();
                mPaint.setAntiAlias(true);
                mPaint.setDither(true);
                mPaint.setColor(color);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeJoin(Paint.Join.ROUND);
                mPaint.setStrokeCap(Paint.Cap.ROUND);
                mPaint.setStrokeWidth(3);
                mPaint.setTextSize(30);
                c2 = new Canvas();
                c2.setBitmap(Transparent);
                // c2.drawBitmap(Bitmap1, 0, 0, null);
            //c2.drawBitmap(Bitmap2, 0, 0, mPaint);

            mPaint.setAlpha(0);
                // mPaint.setStyle(Style.FILL);
            mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
            mPaint.setAntiAlias(true);
                mPath = new Path();
                paths.add(new PathPoints(mPath, color, false));
                mCanvas = new Canvas();
            }

            public void colorChanged(int color) {
                this.color = color;
                mPaint.setColor(color);
            }

            @Override
            protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                super.onSizeChanged(w, h, oldw, oldh);
                // mBitmap = AddReportItemActivity.mPhoto;
                Drawable myDrawable = getResources().getDrawable(
                        R.drawable.ic_launcher);
                mBitmap = ((BitmapDrawable) myDrawable).getBitmap();
                Drawable myDrawable2 = getResources().getDrawable(
                        R.drawable.test);
                Bitmap2 = ((BitmapDrawable) myDrawable2).getBitmap();

                // mBitmap =
                // getIntent().getExtras().getParcelable(ChooseActivity.BITMAP);
                float xscale = (float) w / (float) mBitmap.getWidth();
                float yscale = (float) h / (float) mBitmap.getHeight();
                if (xscale > yscale) // make sure both dimensions fit (use the
                                        // smaller scale)
                    xscale = yscale;
                float newx = (float) w * xscale;
                float newy = (float) h * xscale; // use the same scale for both
                                                    // dimensions

                float xscale1 = (float) w / (float) Bitmap2.getWidth()-10;
                float yscale1 = (float) h / (float) Bitmap2.getHeight()-10;
                if (xscale1 > yscale1) // make sure both dimensions fit (use the
                                        // smaller scale)
                    xscale1 = yscale1;
                float newx1 = (float) w * xscale;
                float newy1 = (float) h * xscale; 
                // if you want it centered on the display (black borders)
                mBitmap = Bitmap.createScaledBitmap(mBitmap, this.getWidth(),
                        this.getHeight(), true);
                Bitmap2 = Bitmap.createScaledBitmap(Bitmap2, this.getWidth()-10,
                        this.getHeight()-10, true);
                // mCanvas = new Canvas(mBitmap);
            }

            @Override
            protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        c2.drawBitmap(Bitmap2, 0, 0, mBitmapPaint);
        canvas.drawBitmap(Transparent, 0, 0, null);
            //  c2.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
                for (PathPoints p : paths) {
                    mPaint.setColor(p.getColor());
                    Log.v("", "Color code : " + p.getColor());
                    if (p.isTextToDraw()) {
                        canvas.drawText(p.textToDraw, p.x, p.y, mPaint);
                    } else {
                        // if(isTouched)
                            //{ 
                        //   canvas.drawPath(p.getPath(), mPaint);
                                c2.drawPath(p.getPath(), mPaint);

                                //canvas.drawPath(p.getPath(),mPaint);
                            // canvas.drawBitmap(mBitmap, 0, 0, null);
                            // c2.drawPath(p.getPath(), mPaint);
                            // c2.drawCircle(X, Y, 50, mPaint);
                        //   canvas.drawBitmap(mBitmap, 0, 0, null);
                              //  canvas.drawBitmap(mBitmap, 0, 0, null);
                           // }
    //                      canvas.drawBitmap(Transparent, 0, 0, null);
                        //canvas.drawPath(p.getPath(), mPaint);
                    }

                }
            }

            private float mX, mY;
            private static final float TOUCH_TOLERANCE = 0;

            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);
                // commit the path to our offscreen
                mCanvas.drawPath(mPath, mPaint);
                // kill this so we don't double draw
                mPath = new Path();
                paths.add(new PathPoints(mPath, color, false));

            }

            private void drawText(int x, int y) {
                Log.v(TAG, "Here");
                Log.v(TAG, "X " + x + " Y " + y);
                this.x = x;
                this.y = y;
                paths.add(new PathPoints(color, textToDraw, true, x, y));
                // mCanvas.drawText(textToDraw, x, y, mPaint);
            }

            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                isTouched=true;
                float x = event.getX();
                float y = event.getY();

                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (!isTextModeOn) {
                        touch_start(x, y);
                        invalidate();
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (!isTextModeOn) {
                        touch_move(x, y);
                        invalidate();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (isTextModeOn) {
                        drawText((int) x, (int) y);
                        invalidate();
                    } else {
                        touch_up();
                        invalidate();
                    }
                    break;
                }
                return true;
            }

            public void onClickUndo() {
                if (paths.size() > 0) {
                    undonePaths.add(paths.remove(paths.size() - 1));
                    invalidate();
                } else {

                }
                // toast the user
            }

            public void onClickRedo() {
                if (undonePaths.size() > 0) {
                    paths.add(undonePaths.remove(undonePaths.size() - 1));
                    invalidate();
                } else {

                }
                // toast the user
            }
        }

        /*
         * public class MyView extends View {
         * 
         * private static final float MINP = 0.25f; private static final float MAXP
         * = 0.75f;
         * 
         * private Bitmap mBitmap; private Canvas mCanvas; private Path mPath;
         * private Paint mBitmapPaint; private ArrayList<Path> undoablePaths;
         * 
         * public MyView(Context c) { super(c);
         * 
         * mPath = new Path(); mBitmapPaint = new Paint(Paint.DITHER_FLAG);
         * undoablePaths=new ArrayList<Path>(); }
         * 
         * 
         * @Override protected void onSizeChanged(int w, int h, int oldw, int oldh)
         * { super.onSizeChanged(w, h, oldw, oldh); mBitmap =
         * CaptureActivity.mPhoto; mCanvas = new Canvas(mBitmap); }
         * 
         * @Override protected void onDraw(Canvas canvas) {
         * canvas.drawColor(0xFFAAAAAA);
         * 
         * canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
         * 
         * canvas.drawPath(mPath, mPaint);
         * 
         * for(Path path : undoablePaths){ canvas.drawPath(path, mPaint); } }
         * 
         * private float mX, mY; private static final float TOUCH_TOLERANCE = 4;
         * 
         * private void touch_start(float x, float y) { mPath=new Path();
         * 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); // commit the path to our
         * offscreen mCanvas.drawPath(mPath, mPaint); // Path tempPath=mPath;
         * undoablePaths.add(mPath); // 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; }
         * 
         * public void undoLastAction() { undoablePaths.remove(undoablePaths.size()
         * - 1); invalidate();
         * 
         * } }
         */

        public void colorChanged(int color) {
            // TODO Auto-generated method stub
            lastColor = color;
            drawView.colorChanged(lastColor);
        }

        class PathPoints {
            private Path path;
            // private Paint mPaint;
            private int color;
            private String textToDraw;
            private boolean isTextToDraw;
            private int x, y;

            public PathPoints(Path path, int color, boolean isTextToDraw) {
                this.path = path;
                this.color = color;
                this.isTextToDraw = isTextToDraw;
            }

            public PathPoints(int color, String textToDraw, boolean isTextToDraw,
                    int x, int y) {
                this.color = color;
                this.textToDraw = textToDraw;
                this.isTextToDraw = isTextToDraw;
                this.x = x;
                this.y = y;
            }

            public Path getPath() {
                return path;
            }

            public void setPath(Path path) {
                this.path = path;
            }

            /*
             * private Paint getPaint() { mPaint = new Paint();
             * mPaint.setAntiAlias(true); mPaint.setColor(color);
             * mPaint.setStyle(Paint.Style.STROKE);
             * mPaint.setStrokeJoin(Paint.Join.ROUND);
             * mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(6);
             * return mPaint; }
             */

            public int getColor() {
                return color;
            }

            public void setColor(int color) {
                this.color = color;
            }

            public String getTextToDraw() {
                return textToDraw;
            }

            public void setTextToDraw(String textToDraw) {
                this.textToDraw = textToDraw;
            }

            public boolean isTextToDraw() {
                return isTextToDraw;
            }

            public void setTextToDraw(boolean isTextToDraw) {
                this.isTextToDraw = isTextToDraw;
            }

            public int getX() {
                return x;
            }

            public void setX(int x) {
                this.x = x;
            }

            public int getY() {
                return y;
            }

            public void setY(int y) {
                this.y = y;
            }

        }
    }


回答2:

Make sure the alpha layer is set on the canvas before you do any drawing:

canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 0xff, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);