擦除叠加位图上的纹理 - Android

时间:2013-02-26 11:22:03

标签: android bitmap imageview paint transparent

我有一个ImageView,其中包含两个位图,一个背景和一个在前景上绘制的图案。我想要做的是擦除前景上绘制的图案,以便在擦除的部分上显示完整的背景。我已经弄明白了这个擦除部分。

我的问题是,当我擦除触摸位置是不准确的。这是因为前景画布上的BitmapImageView画布的大小不同。我在ImageView方法中调整onMeasure的大小以适应屏幕而不缩小屏幕。

如何解决此问题?我想过使用两个图像视图,每个位图一个,但这个过程很繁琐,也会导致性能问题。我想使用一个ImageView和两个位图。

这是我的代码,可以解释我的问题。

      final Paint pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);         
      pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
      pTouch.setColor(Color.TRANSPARENT);
      pTouch.setMaskFilter(new BlurMaskFilter(10, Blur.NORMAL));

      final Canvas c2 = new Canvas(_currentTextureBitmap);

      ImageView iv = new ImageView(this) {
            int x = 0;
            int y = 0;
            int _width;
            int _height;

            @Override
            public boolean onTouchEvent(MotionEvent ev) {

                switch (ev.getAction()) {

                case MotionEvent.ACTION_DOWN: { 

                    x = (int) ev.getX();
                    y = (int) ev.getY();

                    invalidate();

                    break;
                }

                case MotionEvent.ACTION_MOVE: {

                    x = (int) ev.getX();
                    y = (int) ev.getY();

                    invalidate();
                    break;

                }    

                }
                return true;
            }

            @Override 
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
                Drawable d = getDrawable();

                if(d!=null){
                    // ceil not round - avoid thin vertical gaps along the left/right edges
                    _width = MeasureSpec.getSize(widthMeasureSpec);
                    _height = (int) Math.ceil((float) _width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
                    setMeasuredDimension(_width, _height);
                }else{
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                }
            }

            @Override
            public void onDraw(Canvas canvas){
                super.onDraw(canvas);

                if (_selectedImageURI != null)
                    if (_erase) {
                        //draw background
                        canvas.drawBitmap(_backgroundBitmap, null, new RectF(0, 0, _width, _height), null);

                        if (_currentTextureBitmap != null) {
                            c2.drawCircle(x, y, 40, pTouch);
                            canvas.drawBitmap(_currentTextureBitmap, null, new RectF(0, 0, _width, _height), null);
                        }
                    }
               }
        };

1 个答案:

答案 0 :(得分:0)

找到一个非常简单的解决方案。只使用最大画布的宽度,将其除以较小的画布,使用该浮动值并将其与触摸时设置的x和y值相乘。这样就可以在更大的画布上找到正确的坐标。

@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);

    if (_erase) {
        //draw background
        canvas.drawBitmap(_originalBitmap, null, new RectF(0, 0, _width, _height), null);

        if (_overlay != null) { 
            float x = (_x * ((float)_overlay.getWidth()/_width));
            float y = (_y * ((float)_overlay.getHeight()/_height));

            _canvasOverlay.drawCircle(x, y, 40, _touch);
            canvas.drawBitmap(_overlay, null, new RectF(0, 0, _width, _height), null);
        }
    }
}