title: Change background color to a limited area of a view - Android
body: <p>I am trying to develop a BarcodeReader using Google play services, and i would like to get a camera surface background like Zxing sample.<a href="https://i.stack.imgur.com/psKB0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/psKB0.png" alt="zxing sample image"></a></p>
  
  <p>Actually I view with a black transparent background and a white Rect Canvas, and i would like this Rect to be transparent, so my question is if can change the view background without this Rect bounds.</p>
  
  <p><a href="https://i.stack.imgur.com/54eYR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/54eYR.png" alt="enter image description here"></a></p>
  
  <p>This is my Custom View where i draw some lines</p>
  
  <pre><code>public class MyView extends View {
  
  private Paint paint;
  private Path path;
  private int width, height;
  Canvas canvas;
  private Rect rectangle;
  
  
  public MyView(Context context) {
      super(context);
      init();
  }
  
  public MyView(Context context, @Nullable AttributeSet attrs) {
      super(context, attrs);
      init();
  }
  
  public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      init();
  }
  
  private void init() {
      paint = new Paint();
      paint.setColor(Color.RED);
      paint.setStrokeWidth(10);
      paint.setStyle(Paint.Style.STROKE);
      this.setBackgroundColor(getResources().getColor(R.color.black60));
  
  }
  
  
  @Override
  protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      this.canvas = canvas;
      drawMiddleLine();
      drawLeftUpCorner();
      drawLeftDownCorner();
      drawRightUpCorner();
      drawRightDownCorner();
      drawRectSample();
  }
  
  private void drawRectSample() {
      int x = width / 6;
      int y = height / 4;
      int widthLength = x*5;
      int heightLenght = y* 3;
      Paint paint = new Paint();
      // create a rectangle that we'll draw later
      rectangle = new Rect(x, y, widthLength, heightLenght);
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(getResources().getColor(R.color.white50));
      canvas.drawRect(rectangle, paint);
  
  
  }
  
  private void drawLeftUpCorner() {
      if (width != 0 &amp;&amp; height != 0) {
          int startX = width / 6;
          int startY = height / 4;
          int endX = startX * 2;
          int endY = startY;
          canvas.drawLine(startX, startY, endX, endY, paint);
          endX = startX;
          endY += startX;
          canvas.drawLine(startX, startY, endX, endY, paint);
      }
  }
  
  private void drawLeftDownCorner() {
      if (width != 0 &amp;&amp; height != 0) {
          int startX = width / 6;
          int startY = height / 4 * 3;
          int endX = startX * 2;
          int endY = startY;
          canvas.drawLine(startX, startY, endX, endY, paint);
          endX = startX;
          endY -= startX;
          canvas.drawLine(startX, startY, endX, endY, paint);
  
      }
  }
  
  private void drawRightUpCorner() {
      if (width != 0 &amp;&amp; height != 0) {
          int startX = (width / 6) * 4;
          int startY = height / 4;
          int endX = startX + (width / 6);
          int endY = startY;
          canvas.drawLine(startX, startY, endX, endY, paint);
          startX = endX;
          endY = startY + (width / 6);
          canvas.drawLine(startX, startY, endX, endY, paint);
      }
  }
  
  private void drawRightDownCorner() {
      if (width != 0 &amp;&amp; height != 0) {
          int startX = (width / 6) * 4;
          int startY = (height / 4) * 3;
          int endX = startX + (width/ 6);
          int endY = startY;
          canvas.drawLine(startX, startY, endX, endY, paint);
          startX = endX;
          endY -= width / 6;
          canvas.drawLine(startX, startY, endX, endY, paint);
  
      }
  }
  
  
  private void drawMiddleLine() {
      if (width != 0 &amp;&amp; height != 0) {
          int startX = width / 3;
          int startY = height / 2;
          int endX = startX * 2;
          int endY = startY;
          canvas.drawLine(startX, startY, endX, endY, paint);
          paint.setColor(Color.GREEN);
      }
  }
  
  public void setBounds(int width, int height) {
      this.width = width;
      this.height = height;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my xml where the CameraSourePreview and MyView (custom view just to paint screen lines) are:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;                                        
  &lt;RelativeLayout                                                               
  xmlns:android="http://schemas.android.com/apk/res/android"                
  android:id="@+id/topLayout"                                               
  android:orientation="vertical"                                            
  android:layout_width="match_parent"                                       
  android:layout_height="match_parent"                                      
  android:keepScreenOn="true"                                               
  &gt;                                                                         
  
  &lt;t_systems.qrlabs.camera.CameraSourcePreview                              
      android:id="@+id/preview"                                             
      android:layout_width="match_parent"                                   
      android:layout_height="match_parent"&gt;                                 
  
  &lt;/t_systems.qrlabs.camera.CameraSourcePreview&gt;                            
  
  
  &lt;t_systems.qrlabs.barcode.MyView                                          
      android:layout_width="match_parent"                                   
      android:id="@+id/myView"                     
      android:layout_centerInParent="true"                                  
      android:layout_height="match_parent" /&gt;                               
  &lt;/RelativeLayout&gt;                                                             
  </code></pre>
  
  <p>Any help? Thanks.</p>