1/*
2 * Copyright (C) 2014 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
17package com.android.camera.ui;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Paint;
22import android.graphics.RectF;
23import android.util.AttributeSet;
24import android.view.View;
25
26import com.android.camera2.R;
27
28/**
29 * GridLines is a view which directly overlays the preview and draws
30 * evenly spaced grid lines.
31 */
32public class GridLines extends View
33    implements PreviewStatusListener.PreviewAreaChangedListener {
34
35    private RectF mDrawBounds;
36    Paint mPaint = new Paint();
37
38    public GridLines(Context context, AttributeSet attrs) {
39        super(context, attrs);
40        mPaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.grid_line_width));
41        mPaint.setColor(getResources().getColor(R.color.grid_line));
42    }
43
44    @Override
45    public void onDraw(Canvas canvas) {
46        super.onDraw(canvas);
47        if (mDrawBounds != null) {
48            float thirdWidth = mDrawBounds.width() / 3;
49            float thirdHeight = mDrawBounds.height() / 3;
50            for (int i = 1; i < 3; i++) {
51                // Draw the vertical lines.
52                final float x = thirdWidth * i;
53                canvas.drawLine(mDrawBounds.left + x, mDrawBounds.top,
54                        mDrawBounds.left + x, mDrawBounds.bottom, mPaint);
55                // Draw the horizontal lines.
56                final float y = thirdHeight * i;
57                canvas.drawLine(mDrawBounds.left, mDrawBounds.top + y,
58                        mDrawBounds.right, mDrawBounds.top + y, mPaint);
59            }
60        }
61    }
62
63    @Override
64    public void onPreviewAreaChanged(final RectF previewArea) {
65        setDrawBounds(previewArea);
66    }
67
68    private void setDrawBounds(final RectF previewArea) {
69        mDrawBounds = new RectF(previewArea);
70        invalidate();
71    }
72}
73