blob: 0110d9cbe4970d849ebd27c4e1431b8b86ed426b [file] [log] [blame]
Michael Kolb8872c232013-01-29 10:33:22 -08001/*
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
17package com.android.camera.ui;
18
Michael Kolb8872c232013-01-29 10:33:22 -080019import android.content.Context;
Michael Kolb8872c232013-01-29 10:33:22 -080020import android.graphics.Matrix;
Michael Kolb8872c232013-01-29 10:33:22 -080021import android.util.AttributeSet;
Michael Kolb8872c232013-01-29 10:33:22 -080022import android.view.View;
23import android.view.ViewGroup;
Michael Kolb8872c232013-01-29 10:33:22 -080024
Angus Kong2bca2102014-03-11 16:27:30 -070025import com.android.camera.debug.Log;
26
Michael Kolb8872c232013-01-29 10:33:22 -080027// A RotateLayout is designed to display a single item and provides the
28// capabilities to rotate the item.
29public class RotateLayout extends ViewGroup implements Rotatable {
30 @SuppressWarnings("unused")
Angus Kong2bca2102014-03-11 16:27:30 -070031 private static final Log.Tag TAG = new Log.Tag("RotateLayout");
Michael Kolb8872c232013-01-29 10:33:22 -080032 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 Kolb8872c232013-01-29 10:33:22 -080045 @Override
46 protected void onFinishInflate() {
47 mChild = getChildAt(0);
Sascha Haeberling638e6f02013-09-18 14:28:51 -070048 mChild.setPivotX(0);
49 mChild.setPivotY(0);
Michael Kolb8872c232013-01-29 10:33:22 -080050 }
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 Kolb8872c232013-01-29 10:33:22 -080070 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 Haeberling638e6f02013-09-18 14:28:51 -070088 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 Kolb8872c232013-01-29 10:33:22 -0800105 }
Sascha Haeberling638e6f02013-09-18 14:28:51 -0700106 mChild.setRotation(-mOrientation);
Michael Kolb8872c232013-01-29 10:33:22 -0800107 }
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 Kolb8872c232013-01-29 10:33:22 -0800126}