0

So this is a weird thing I want to do that would look amazing in my app, but for the life of me cannot figure out how.

Let's say I have a background on my screen like so:

I then add a view to it that can have any number of subviews/content. This view naturally obscures the background:

So far so good. Now comes the tricky part. I want to implement something that will allow me to "see through" part of this view. Essentially it will make the view appear invisible but only in one specific area. Here is what it would look like:

I have not found any way to do this so far.

Henry
  • 17,490
  • 7
  • 63
  • 98
Dale_Plante
  • 876
  • 1
  • 7
  • 15

2 Answers2

2

You could use the Bitmap Masking technique to accomplish what you want. Something like the below in your onDraw() method.

    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    setLayerType(LAYER_TYPE_HARDWARE,paint); // if hardware acceleration is on
    // canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), paint); // Use this if hardware acceleration is off.
    canvas.drawRect(200, 200, 400, 400, paint); // This will be HOLE in the VIEW


For more info read this: https://stackoverflow.com/a/33483016/4747587

Community
  • 1
  • 1
Henry
  • 17,490
  • 7
  • 63
  • 98
0

Henry mentioned a good solution. Another easy solution is often to just use a PNG-drawable or a 9-patch-drawable. If the area of your overlay is always the same the PNG gives you full flexibility in what you want to have as transparent and the rest stays as you define it. You can scale the PNG or - if you want only specific areas to scale, use a 9-patch-drawable.

Christian
  • 4,596
  • 1
  • 26
  • 33