Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.camera.util;
     18 
     19 import android.graphics.Canvas;
     20 import android.graphics.Paint;
     21 import android.graphics.Rect;
     22 import android.graphics.RectF;
     23 
     24 /**
     25  * Drawing utilities for rendering debug artifacts.
     26  */
     27 public class DebugBoundsRenderer {
     28     /**
     29      * Draw a bounding box with indicators in the corners and crosshairs in
     30      * the provided canvas object.
     31      */
     32     public static void drawBounds(Canvas canvas, Paint paint, float size, Rect rect) {
     33         drawBounds(canvas, paint, size, rect.left, rect.top, rect.right, rect.bottom);
     34     }
     35 
     36     /**
     37      * Draw a bounding box with indicators in the corners and crosshairs in
     38      * the provided canvas object.
     39      */
     40     public static void drawBounds(Canvas canvas, Paint paint, float size, RectF rect) {
     41         drawBounds(canvas, paint, size, rect.left, rect.top, rect.right, rect.bottom);
     42     }
     43 
     44     /**
     45      * Draw a bounding box with indicators in the corners and crosshairs in
     46      * the provided canvas object.
     47      */
     48     public static void drawBounds(Canvas canvas, Paint paint, float size, float x1, float y1,
     49           float x2, float y2) {
     50         // Top left
     51         // horizontal
     52         canvas.drawLine(x1, y1, x1 + size, y1, paint);
     53         // vertical
     54         canvas.drawLine(x1, y1, x1, y1 + size, paint);
     55 
     56         // top right
     57         // horizontal
     58         canvas.drawLine(x2 - size, y1, x2, y1, paint);
     59         // vertical
     60         canvas.drawLine(x2, y1, x2, y1 + size, paint);
     61 
     62         // bottom right
     63         // horizontal
     64         canvas.drawLine(x2 - size, y2, x2, y2, paint);
     65         // vertical
     66         canvas.drawLine(x2, y2- size, x2, y2, paint);
     67 
     68         // bottom left
     69         // horizontal
     70         canvas.drawLine(x1, y2, x1 + size, y2, paint);
     71         // vertical
     72         canvas.drawLine(x1, y2 - size, x1, y2, paint);
     73 
     74         // crosshairs in the center
     75         float cX = (x1 + x2) / 2;
     76         float cY = (y1 + y2) / 2;
     77         float halfSize = size / 2;
     78         canvas.drawLine(cX - halfSize, cY, cX + halfSize, cY, paint);
     79         canvas.drawLine(cX, cY - halfSize, cX, cY + halfSize, paint);
     80     }
     81 }
     82