GridLines.java revision b3f59a874f675ceb7542298760a3c813c5e0d9ac
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.util.Log;
25import android.view.View;
26import android.view.ViewGroup;
27
28import com.android.camera2.R;
29
30/**
31 * GridLines is a view which directly overlays the preview and draws
32 * evenly spaced grid lines.
33 */
34public class GridLines extends View
35    implements PreviewStatusListener.PreviewAreaSizeChangedListener {
36
37    Paint mPaint = new Paint();
38    float mPreviewWidth = 0f;
39    float mPreviewHeight = 0f;
40
41    public GridLines(Context context, AttributeSet attrs) {
42        super(context, attrs);
43        int strokeWidth = getResources().getDimensionPixelSize(R.dimen.grid_line_width);
44        mPaint.setStrokeWidth(strokeWidth);
45        mPaint.setColor(getResources().getColor(R.color.grid_line));
46    }
47
48    @Override
49    public void onDraw(Canvas canvas) {
50        super.onDraw(canvas);
51        if (mPreviewWidth > 0 && mPreviewHeight > 0) {
52            float thirdWidth = mPreviewWidth / 3;
53            float thirdHeight = mPreviewHeight / 3;
54            // draw the first vertical line
55            canvas.drawLine(thirdWidth, 0, thirdWidth, mPreviewHeight, mPaint);
56            // draw the second vertical line
57            canvas.drawLine(thirdWidth*2, 0, thirdWidth*2, mPreviewHeight, mPaint);
58
59            // draw the first horizontal line
60            canvas.drawLine(0, thirdHeight, mPreviewWidth, thirdHeight, mPaint);
61            // draw the second horizontal line
62            canvas.drawLine(0, thirdHeight*2, mPreviewWidth, thirdHeight*2, mPaint);
63        }
64    }
65
66    @Override
67    public void onPreviewAreaSizeChanged(RectF previewArea) {
68        mPreviewWidth = previewArea.width();
69        mPreviewHeight = previewArea.height();
70        post(new Runnable() {
71                @Override
72                public void run() {
73                    // Causes another layout pass, which will resize this
74                    // view and trigger a redraw of the grid lines.
75                    matchPreviewDimensions();
76                }
77            });
78    }
79
80    /**
81     * Reset the height and width of this view to match cached the height
82     * and width of the preview.
83     */
84   private void matchPreviewDimensions() {
85        if (mPreviewWidth > 0 && mPreviewHeight > 0) {
86            ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) getLayoutParams();
87            params.width = (int) mPreviewWidth;
88            params.height = (int) mPreviewHeight;
89            setLayoutParams(params);
90        }
91    }
92}