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