• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 package com.android.cts.verifier.camera.fov;
3 
4 import android.content.Context;
5 import android.graphics.Canvas;
6 import android.graphics.Color;
7 import android.graphics.Paint;
8 import android.util.AttributeSet;
9 import android.view.View;
10 
11 /**
12  * View that draws an overlay on the camera preview.
13  * @author settinger@google.com(Scott Ettinger)
14  */
15 class CameraPreviewView extends View {
16 
17     private static final int GRID_ALPHA = 50;
18     private static final int GRID_WIDTH = 50;
19     private Paint mPaint = new Paint();
20 
CameraPreviewView(Context context)21     public CameraPreviewView(Context context) {
22         super(context);
23         this.setWillNotDraw(false);
24     }
25 
CameraPreviewView(Context context, AttributeSet attrs)26     public CameraPreviewView(Context context, AttributeSet attrs) {
27         super(context, attrs);
28         this.setWillNotDraw(false);
29     }
30 
CameraPreviewView( Context context, AttributeSet attrs, int defStyle)31     public CameraPreviewView(
32             Context context, AttributeSet attrs, int defStyle) {
33         super(context, attrs, defStyle);
34         this.setWillNotDraw(false);
35     }
36 
37     @Override
onDraw(Canvas canvas)38     public void onDraw(Canvas canvas) {
39         super.onDraw(canvas);
40 
41         // Draw a single vertical line on the center of the image to help align
42         // the camera when setting up.
43         float centerX = canvas.getWidth() / 2.0f;
44         float centerY = canvas.getHeight() / 2.0f;
45         mPaint.setStyle(Paint.Style.STROKE);
46         mPaint.setColor(Color.GREEN);
47         mPaint.setStrokeWidth(3);
48         canvas.drawLine(centerX, 0, centerX, canvas.getHeight(), mPaint);
49 
50         // Draw the transparent grid.
51         mPaint.setAlpha(GRID_ALPHA);
52         int vertLines = canvas.getWidth() / 2 / GRID_WIDTH;
53         int horizLines = canvas.getHeight() / 2 / GRID_WIDTH;
54         for (int i = 0; i < horizLines; ++i) {
55             int y = (int) centerY - i * GRID_WIDTH;
56             canvas.drawLine(0, y, canvas.getWidth(), y, mPaint);
57             y = (int) centerY + i * GRID_WIDTH;
58             canvas.drawLine(0, y, canvas.getWidth(), y, mPaint);
59         }
60         for (int i = 0; i < vertLines; ++i) {
61             int x = (int) centerX - i * GRID_WIDTH;
62             canvas.drawLine(x, 0, x, canvas.getHeight(), mPaint);
63             x = (int) centerX + i * GRID_WIDTH;
64             canvas.drawLine(x, 0, x, canvas.getHeight(), mPaint);
65         }
66     }
67 }
68