使位图的触摸区域透明

时间:2014-12-22 06:55:51

标签: java android bitmap transparent

我在画布上有2个位图。在接触顶部位图时,下部位图应该是可见的,顶部位图应该被擦除。 我从这个帖子中得到了帮助。 Make certain area of bitmap transparent on touch。我可以通过圆圈看到底部位图,但顶部位图在触摸时没有擦除。如何在触摸时擦除位图。我知道之前已经问过这个问题,但我无法解决问题。

这是我的代码:

public class MainActivity extends Activity {

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

    setContentView(new Panel(this));
}

class Panel extends View {
    Bitmap bmOverlay;
    private Paint mPaint;
    Bitmap bm2, bm1;
    Bitmap bitmap;
    Canvas pcanvas;

    int x = 0;
    int y = 0;
    int r = 0;

    public Panel(Context context) {
        super(context);
        setFocusable(true);
        setBackgroundColor(Color.TRANSPARENT);

        // setting paint
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.TRANSPARENT);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
        mPaint.setAntiAlias(true);

        bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.aa);
        bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.aaa);

        bmOverlay = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(),
                Bitmap.Config.ARGB_8888);
        pcanvas = new Canvas(bmOverlay);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // draw a circle that is erasing bitmap
        super.onDraw(canvas);
        canvas.drawBitmap(bm2, 0, 0, null);
        pcanvas.drawBitmap(bm1, 0, 0, null);
        pcanvas.drawCircle(x, y, 40, mPaint);
        canvas.drawBitmap(bmOverlay, 0, 0, null);

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // set parameter to draw circle on touch event

        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;

        }

        case MotionEvent.ACTION_UP:

            break;

        }
        return true;
    }



}
}

1 个答案:

答案 0 :(得分:1)

您需要进行两项更改才能删除顶部位图。首先,将其设为可变位图,以便在删除内容时更改内容:

Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.aa);
bm1 = temp.copy(Bitmap.Config.ARGB_8888, true); // mutable = true

此处请注意内存不足错误。

其次,在onDraw函数中,通过写回bm1:

来更新位图内容
@Override
protected void onDraw(Canvas canvas) {
    // draw a circle that is erasing bitmap
    super.onDraw(canvas);
    canvas.drawBitmap(bm2, 0, 0, null);
    pcanvas.drawBitmap(bm1, 0, 0, null);
    pcanvas.drawCircle(x, y, 40, mPaint);
    canvas.drawBitmap(bmOverlay, 0, 0, null);

    // erase the top bitmap:
    Canvas bitmapCanvas = new Canvas(bm1);
    bitmapCanvas.drawBitmap(bm2, 0, 0, null);
    bitmapCanvas.drawBitmap(bmOverlay, 0, 0, null);
}

此外,要在启动应用程序时停止在左上角删除圆圈,请创建一个默认值为false的布尔值,并在有有效坐标时将其设置在onTouchEvent中,并在调用drawCircle之前进行检查