Android Custom Eraser Effect

  • 2021-10-27 08:53:15
  • OfStack

In this paper, we share the Android custom eraser effect and use Bessel curve to deal with curve turning points


public class picFingerToTest extends View {

  private Paint paint;
  private Bitmap decodeResourceSRC;
  private Bitmap createBitmapDST;
  //  Finger path, use Bessel route 
  private Path path;
  private float perX;
  private float perY;

  public picFingerToTest(Context context, AttributeSet attrs) {
    super(context, attrs);
    // 1 Setting Disable Hardware Settings 
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);

    // 2 Set the finger brush 
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(45);

    // 3 Generate the image finger source target 
    //  Source 
    decodeResourceSRC = BitmapFactory.decodeResource(getResources(), R.drawable.welcome, null);
    //  Objectives 
    createBitmapDST = Bitmap.createBitmap(decodeResourceSRC.getWidth(), decodeResourceSRC.getHeight(),
        Config.ARGB_8888);
    path = new Path();

  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //  Hierarchical rendering 
    int saveLayer = canvas.saveLayer(0, 0, getWidth(), getHeight(), null,Canvas.ALL_SAVE_FLAG);

    //  Draw the finger trajectory to the target path 
    Canvas canvas2 = new Canvas(createBitmapDST);
    canvas2.drawPath(path, paint);

    //  Draw the target image on the canvas 
    canvas.drawBitmap(createBitmapDST, 0, 0, paint);

    //  Calculate the source image area 

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
    canvas.drawBitmap(decodeResourceSRC, 0, 0, paint);

    paint.setXfermode(null);
    canvas.restoreToCount(saveLayer);

  }

  // Use Bessel curve to make the broken line excessively smooth 
  @Override
  public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
    //  Record the initialization position of finger touch 
    case MotionEvent.ACTION_DOWN:

      path.moveTo(event.getX(), event.getY());

      perX = event.getX();
      perY = event.getY();

      return true;

    case MotionEvent.ACTION_MOVE:

      float endX = (perX + event.getX()) / 2;
      float endY = (perY + event.getY()) / 2;

      path.quadTo(perX, perY, endX, endY);
      perX = event.getX();
      perY = event.getY();
      postInvalidate();

      break;
    case MotionEvent.ACTION_UP:

      break;

    default:
      break;
    }

    return super.onTouchEvent(event);
  }

}

This site adds another code for everyone: android eraser wipe picture function


public void onCreate() {
  // Bottom edge picture 
  ImageView ivTop = (ImageView) findViewByid(R.id.iv_top);
 
  Options opts = new Options(); // Image loader for configuring 1 Some scaling ratios, and pixel units 
  opts.inSampleSize = 2; // Set the loader to scale the width and height of the original picture to 2/1 Effect loading of 
  // Get the outer picture ,decodeResource The unit of pixels obtained by default by the method is RGB(red,green,blue),ARGB(alpha,red,green,blue)
  Bitmap topImage = BitmapFactory.decodeResources( getResource(),R.drawable.top, opts);
 
  // Create 1 Blank picture, and specify the unit of the picture you want to read as :ARGB
  Bitmap blank = Bitmap.createBitmap(topImage.getWidth(), topImage.getHeight, Config.ARGB_4444);
 
  // Put the top one topImage Draw on a blank picture 
  Canvas canvas = new Canvas(blank);
  // Put topImage Drawing on a blank picture but the pixel unit becomes ARGB()
  canvas.drawBitmap(topImage, 0, 0, null);
  ivTop.setImageBitmap(blank);
}
 
class MyOnTouchListoner implements OnTouchListener {
 
  @Override pulic boolean OnTouch(View v, MotionEvent event) {
    // Is it a moving event 
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
      // Get the press coordinates 
      int x = (int) event.getX();
      int y = (int) event.getY();
 
      for (int i = x - 10; i < x + 10; i++) {
        for (int j = y - 10; j < y + 10; j++) {
          // Prevent exceeding the boundary 
          if (j >= 0 && blank.getHeight() && i >= 0 && i < blank.getWidth()) {
            blank.setPixel(i, j, Color.TRANSPARENT);
          }
        }
      }
      // The modified picture is set to ImageView
      ivTop.setImageBitmap(blank);
    }
 
    return true; //true  Consumes this touch event .false  No consumption 
  }
 
}


Related articles: