| The Android Open Source Project | 0eaec58 | 2008-12-17 18:05:54 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.camera; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.util.Log; |
| 22 | import android.view.SurfaceView; |
| 23 | import android.view.View.MeasureSpec; |
| 24 | |
| 25 | class 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 | } |