blob: aed1e891f68d0e11e4208a12a6a18decf81005ba [file] [log] [blame]
The Android Open Source Project0eaec582008-12-17 18:05:54 -08001/*
2 * Copyright (C) 2007 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;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.util.Log;
22import android.view.SurfaceView;
23import android.view.View.MeasureSpec;
24
25class VideoPreview extends SurfaceView {
26 private float mAspectRatio;
27 private int mHorizontalTileSize = 1;
28 private int mVerticalTileSize = 1;
29
30 /**
31 * Setting the aspect ratio to this value means to not enforce an aspect ratio.
32 */
33 public static float DONT_CARE = 0.0f;
34
35 public VideoPreview(Context context) {
36 super(context);
37 }
38
39 public VideoPreview(Context context, AttributeSet attrs) {
40 super(context, attrs);
41 }
42
43 public VideoPreview(Context context, AttributeSet attrs, int defStyle) {
44 super(context, attrs, defStyle);
45 }
46
47 public void setTileSize(int horizontalTileSize, int verticalTileSize) {
48 if ((mHorizontalTileSize != horizontalTileSize)
49 || (mVerticalTileSize != verticalTileSize)) {
50 mHorizontalTileSize = horizontalTileSize;
51 mVerticalTileSize = verticalTileSize;
52 requestLayout();
53 invalidate();
54 }
55 }
56
57 public void setAspectRatio(int width, int height) {
58 setAspectRatio(((float) width) / ((float) height));
59 }
60
61 public void setAspectRatio(float aspectRatio) {
62 if (mAspectRatio != aspectRatio) {
63 mAspectRatio = aspectRatio;
64 requestLayout();
65 invalidate();
66 }
67 }
68
69 @Override
70 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
71 if (mAspectRatio != DONT_CARE) {
72 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
73 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
74
75 int width = widthSpecSize;
76 int height = heightSpecSize;
77
78 if (width > 0 && height > 0) {
79 float defaultRatio = ((float) width) / ((float) height);
80 if (defaultRatio < mAspectRatio) {
81 // Need to reduce height
82 height = (int) (width / mAspectRatio);
83 } else if (defaultRatio > mAspectRatio) {
84 width = (int) (height * mAspectRatio);
85 }
86 width = roundUpToTile(width, mHorizontalTileSize, widthSpecSize);
87 height = roundUpToTile(height, mVerticalTileSize, heightSpecSize);
88 Log.i("VideoPreview", "ar " + mAspectRatio + " setting size: " + width + 'x' + height);
89 setMeasuredDimension(width, height);
90 return;
91 }
92 }
93 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
94 }
95
96 private int roundUpToTile(int dimension, int tileSize, int maxDimension) {
97 return Math.min(((dimension + tileSize - 1) / tileSize) * tileSize, maxDimension);
98 }
99}