Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.ui; |
| 18 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 20 | import android.graphics.Matrix; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 24 | |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 25 | import com.android.camera.debug.Log; |
| 26 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 27 | // A RotateLayout is designed to display a single item and provides the |
| 28 | // capabilities to rotate the item. |
| 29 | public class RotateLayout extends ViewGroup implements Rotatable { |
| 30 | @SuppressWarnings("unused") |
Angus Kong | 2bca210 | 2014-03-11 16:27:30 -0700 | [diff] [blame^] | 31 | private static final Log.Tag TAG = new Log.Tag("RotateLayout"); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 32 | private int mOrientation; |
| 33 | private Matrix mMatrix = new Matrix(); |
| 34 | protected View mChild; |
| 35 | |
| 36 | public RotateLayout(Context context, AttributeSet attrs) { |
| 37 | super(context, attrs); |
| 38 | // The transparent background here is a workaround of the render issue |
| 39 | // happened when the view is rotated as the device's orientation |
| 40 | // changed. The view looks fine in landscape. After rotation, the view |
| 41 | // is invisible. |
| 42 | setBackgroundResource(android.R.color.transparent); |
| 43 | } |
| 44 | |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 45 | @Override |
| 46 | protected void onFinishInflate() { |
| 47 | mChild = getChildAt(0); |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 48 | mChild.setPivotX(0); |
| 49 | mChild.setPivotY(0); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected void onLayout( |
| 54 | boolean change, int left, int top, int right, int bottom) { |
| 55 | int width = right - left; |
| 56 | int height = bottom - top; |
| 57 | switch (mOrientation) { |
| 58 | case 0: |
| 59 | case 180: |
| 60 | mChild.layout(0, 0, width, height); |
| 61 | break; |
| 62 | case 90: |
| 63 | case 270: |
| 64 | mChild.layout(0, 0, height, width); |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Override |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 70 | protected void onMeasure(int widthSpec, int heightSpec) { |
| 71 | int w = 0, h = 0; |
| 72 | switch(mOrientation) { |
| 73 | case 0: |
| 74 | case 180: |
| 75 | measureChild(mChild, widthSpec, heightSpec); |
| 76 | w = mChild.getMeasuredWidth(); |
| 77 | h = mChild.getMeasuredHeight(); |
| 78 | break; |
| 79 | case 90: |
| 80 | case 270: |
| 81 | measureChild(mChild, heightSpec, widthSpec); |
| 82 | w = mChild.getMeasuredHeight(); |
| 83 | h = mChild.getMeasuredWidth(); |
| 84 | break; |
| 85 | } |
| 86 | setMeasuredDimension(w, h); |
| 87 | |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 88 | switch (mOrientation) { |
| 89 | case 0: |
| 90 | mChild.setTranslationX(0); |
| 91 | mChild.setTranslationY(0); |
| 92 | break; |
| 93 | case 90: |
| 94 | mChild.setTranslationX(0); |
| 95 | mChild.setTranslationY(h); |
| 96 | break; |
| 97 | case 180: |
| 98 | mChild.setTranslationX(w); |
| 99 | mChild.setTranslationY(h); |
| 100 | break; |
| 101 | case 270: |
| 102 | mChild.setTranslationX(w); |
| 103 | mChild.setTranslationY(0); |
| 104 | break; |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 105 | } |
Sascha Haeberling | 638e6f0 | 2013-09-18 14:28:51 -0700 | [diff] [blame] | 106 | mChild.setRotation(-mOrientation); |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean shouldDelayChildPressedState() { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | // Rotate the view counter-clockwise |
| 115 | @Override |
| 116 | public void setOrientation(int orientation, boolean animation) { |
| 117 | orientation = orientation % 360; |
| 118 | if (mOrientation == orientation) return; |
| 119 | mOrientation = orientation; |
| 120 | requestLayout(); |
| 121 | } |
| 122 | |
| 123 | public int getOrientation() { |
| 124 | return mOrientation; |
| 125 | } |
Michael Kolb | 8872c23 | 2013-01-29 10:33:22 -0800 | [diff] [blame] | 126 | } |