1

我正在制作一个应用程序,其中在擦除覆盖位图的某些部分后,我在画布上绘制位图作为覆盖物在此处输入图像描述

我的代码如下:-

public EraserView(Context context) {
    super(context);
    setFocusable(true);
    setBackgroundResource(R.drawable.back);
    // setting paint
    mPaint = new Paint();
    mPaint.setAlpha(0);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    mPaint.setColor(Color.TRANSPARENT);
    mPaint.setAntiAlias(true);
    Resources r = this.getContext().getResources();
    Bitmap bm = BitmapFactory.decodeResource(getResources(),
            R.drawable.image2);
    bitmap = bm.createBitmap(295, 260, Config.ARGB_8888);
    pcanvas = new Canvas();
    pcanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
    pcanvas.drawBitmap(bm, 0, 0, null);
}

@Override
protected void onDraw(Canvas canvas) {
    pcanvas.drawCircle(x, y, r, mPaint);
    canvas.drawBitmap(bitmap, 0, 0, null);
    setBitmap(bitmap);
    super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction())
    {
    case MotionEvent.ACTION_DOWN:
        x = (int) event.getX();
        y = (int) event.getY();
        r = 2;
       invalidate();
        break;
    case MotionEvent.ACTION_UP:
        x = (int) event.getX();
        y = (int) event.getY();
        r = 20;
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        x = (int) event.getX();
        y = (int) event.getY();
        r =2;
        invalidate();
        break;
    case MotionEvent.ACTION_POINTER_UP:
        x = (int) event.getX();
        y = (int) event.getY();
        r = 2;
        // Atlast invalidate canvas
        invalidate();
        break;
    }
    return true;
}

public void setBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
}

public Bitmap getBitmap() {
    return bitmap;
}

但我的要求只是提前保存覆盖。

4

1 回答 1

2

当 CompressFormat 为JPEG时,它会显示黑色背景,因为JPEG 格式不支持 alpha 透明度,只需将 CompressFormat 更改为PNG,甚至将图像保存为png 格式而不是 jpeg。检查以下代码:

ByteArrayOutputStream objbytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, objbytes);

在 delcare 图像路径时,使用 .png 扩展名。

目录 + "/pics+"+System.currentTimeMillis()+".png";

于 2013-07-01T05:41:18.373 回答