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