Checked in the ZXing Test app for Android under androidtest. This application exercis...
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / ViewfinderView.java
1 /*
2  * Copyright (C) 2008 ZXing authors
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.google.zxing.client.androidtest;
18
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Canvas;
22 import android.graphics.Paint;
23 import android.graphics.Rect;
24 import android.util.AttributeSet;
25 import android.view.View;
26
27 public class ViewfinderView extends View {
28
29   private Paint mPaint;
30   private Rect mBox;
31   private int mMaskColor;
32   private int mFrameColor;
33
34   // This constructor is used when the class is built from an XML resource.
35   public ViewfinderView(Context context, AttributeSet attrs) {
36     super(context, attrs);
37
38     // Initialize these once for performance rather than calling them every time in onDraw().
39     mPaint = new Paint();
40     mBox = new Rect();
41     Resources resources = getResources();
42     mMaskColor = resources.getColor(R.color.viewfinder_mask);
43     mFrameColor = resources.getColor(R.color.viewfinder_frame);
44   }
45
46   @Override
47   public void onDraw(Canvas canvas) {
48     Rect frame = CameraManager.get().getFramingRect();
49     int width = canvas.getWidth();
50     int height = canvas.getHeight();
51
52     // Draw the exterior (i.e. outside the framing rect) darkened, in red to distinguish it from
53     // the regular barcodes app
54     mPaint.setColor(mMaskColor);
55     mBox.set(0, 0, width, frame.top);
56     canvas.drawRect(mBox, mPaint);
57     mBox.set(0, frame.top, frame.left, frame.bottom + 1);
58     canvas.drawRect(mBox, mPaint);
59     mBox.set(frame.right + 1, frame.top, width, frame.bottom + 1);
60     canvas.drawRect(mBox, mPaint);
61     mBox.set(0, frame.bottom + 1, width, height);
62     canvas.drawRect(mBox, mPaint);
63
64     // Draw a two pixel solid white border inside the framing rect
65     mPaint.setColor(mFrameColor);
66     mBox.set(frame.left, frame.top, frame.right + 1, frame.top + 2);
67     canvas.drawRect(mBox, mPaint);
68     mBox.set(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1);
69     canvas.drawRect(mBox, mPaint);
70     mBox.set(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1);
71     canvas.drawRect(mBox, mPaint);
72     mBox.set(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1);
73     canvas.drawRect(mBox, mPaint);
74   }
75
76 }