代码之家  ›  专栏  ›  技术社区  ›  Mouaad Abdelghafour AITALI

从画布中删除上一个位图

  •  0
  • Mouaad Abdelghafour AITALI  · 技术社区  · 5 年前

    我正试图添加 撤消 选项,允许用户删除绘制的 bitmap canvas

    enter image description here

    当我单击“撤消”按钮时,我想删除当前位图 位图 当它被删除时,我将再次单击“撤消”按钮,以便 位图 2个 应该被移除,直到我移除所有 bitmaps

    我可以得到画布上绘制的每个位图的坐标和位图,下面的代码不起作用,当我试图单击“撤消”按钮时,所有 位图 远离的。

    撤消按钮:

    //check if the list of points is not empty, means something already drawn on the canvas
    if (MainActivity.this.drawView.points.size() > 0)
        i = 0;
    undo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (MainActivity.this.drawView.points.size() > 0) {
                x = drawView.points.get(i).x;
                y = drawView.points.get(i).y;
                prevBitmap = drawView.points.get(i).bt;
                drawView.enableErase(true,x,y,prevBitmap);
                i++;
            }
        }
    });
    

    绘图视图:

    @SuppressLint("CommitPrefEdits")
    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setOnTouchListener(this);
        this.paint.setColor(Color.TRANSPARENT);
        this.paint.setAntiAlias(true);
        this.mContext = context;
        this.prefs = this.mContext.getSharedPreferences("MyGamePreferences", 0);
        this.prefEditor = this.prefs.edit();
        this.prefEditor.putString("font", "f");
        this.prefEditor.apply();
    }
    
    public void onDraw(Canvas canvas) {
        this.ch = getHeight();
        this.cw = getWidth();
        if (!erase)
            for (Point point : this.points) {
                canvas.drawBitmap(point.bt, point.x, point.y, null);
            }
    
    }
    
    public void drawSingleEmoji(MotionEvent motionEvent, Bitmap singleBitmap) {
        this.bt1 = singleBitmap;
        this.bt2 = Bitmap.createScaledBitmap(bt1, w, h, true);
        Point point = new Point();
        point.x = motionEvent.getX() - ((float) (this.bt2.getWidth() / 2));
        point.y = motionEvent.getY() - ((float) (this.bt2.getHeight() / 2));
        point.bt = this.bt2;
        this.points.add(point);
        invalidate();
    }
    
    public boolean onTouch(View view, MotionEvent motionEvent) {
        String str = "grpEmoji";
        this.s = this.prefs.getString("font", str);
        String str2 = this.s;
        float x, y = 0, x1, y1;
        if ((motionEvent.getAction() == MotionEvent.ACTION_DOWN)) {
            x = motionEvent.getX();
            y = motionEvent.getY();
        } else if ((motionEvent.getAction() == MotionEvent.ACTION_UP)) {
            x1 = motionEvent.getX();
            y1 = motionEvent.getY();
            if (y1 > y) {
                if (str2 != null) {
                    if (str2.equals("grpEmoji")) {
                         drawSingleEmoji(motionEvent, bitSingle);
                    } else if (str2.equals("generatedEmoji")) {
                         drawSingleEmoji(motionEvent, bitSingle);
                    } else {
                        if (bitSingle != null)
                            drawSingleEmoji(motionEvent, bitSingle);
                    }
                }
            }
        }
        return true;
    
    }
    
    public void enableErase(boolean isEraseEnabled, float x, float y, Bitmap bitmap) {
        erase = isEraseEnabled;
        bitmap.eraseColor(Color.TRANSPARENT);
        invalidate();
    }
    

    }

    有谁能帮我解决这个问题吗?我花了一整天的时间来解决这个问题,但是没有结果,谢谢

    0 回复  |  直到 5 年前