0

I am using below code to erase bitmap from canvas on touch event.

it's working fine in android 2.3 but in 4.0 it show me black spot instead of erasing bitmap.

anyone can help.

public class DemoTouch extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new TouchView(this));

}

class TouchView extends View {
    Bitmap bgr;
    Bitmap overlayDefault;
    Bitmap overlay;
    Paint pTouch;
    int X = -100;
    int Y = -100;
    Canvas c2;

    public TouchView(Context context) {
        super(context);

        bgr = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
        overlayDefault = BitmapFactory.decodeResource(getResources(),
                R.drawable.a2);
        overlay = BitmapFactory.decodeResource(getResources(),
                R.drawable.a2).copy(Config.ARGB_4444, true);
        c2 = new Canvas(overlay);

        pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
        pTouch.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        pTouch.setColor(Color.TRANSPARENT);
        pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
        pTouch.setAntiAlias(true);

    }

    @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;

        }

        case MotionEvent.ACTION_UP:

            break;

        }
        return true;
    }

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

        // draw background
        canvas.drawBitmap(bgr, 0, 0, null);
        // copy the default overlay into temporary overlay and punch a hole
        // in it
        // c2.drawBitmap(overlayDefault, 0, 0, null); // exclude this line
        // to
        // show all as you draw
        c2.drawCircle(X, Y, 20, pTouch);
        // draw the overlay over the background
        canvas.drawBitmap(overlay, 0, 0, null);

    }

}

}

dev
  • 461
  • 3
  • 6
  • 18
  • JUST see the below code http://stackoverflow.com/questions/20904314/replacing-one-bitmap-with-second-one-canvas-android – Siddhpura Amit Jan 03 '14 at 13:41
  • below code will be useful for you http://stackoverflow.com/questions/20904314/replacing-one-bitmap-with-second-one-canvas-android – Siddhpura Amit Jan 03 '14 at 13:59

1 Answers1

1

Have a look at this method of mine. The view EditImageView is a custom ImageView that I also applied that erase function to. I had the same problem until I applied the code below inside the if statement. That fixes the problem for 3.0 and up

/**
 * Sets the image to the view in the content view
 * @param view
 */
private void setImageView(EditImageView view) {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(params);

    // #### THIS IS THE IMPORTANT PART ######
    if (Build.VERSION.SDK_INT >= 11) {
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    LinearLayout main = (LinearLayout) findViewById(R.id.galleryImage);
    main.removeAllViews();
    main.addView(view);
}

EDIT

You can probably do something like this in your code.

public TouchView(Context context) {
    super(context);

    // This should fix it from 3.0 and up
    if (Build.VERSION.SDK_INT >= 11) {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    bgr = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
    overlayDefault = BitmapFactory.decodeResource(getResources(),
            R.drawable.a2);
    overlay = BitmapFactory.decodeResource(getResources(),
            R.drawable.a2).copy(Config.ARGB_4444, true);
    c2 = new Canvas(overlay);

    pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
    pTouch.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
    pTouch.setColor(Color.TRANSPARENT);
    pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
    pTouch.setAntiAlias(true);

}

Hope this also helps you

the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45