RotateLayout.java revision 98d615769af2b08bcddf02ee1b11f5288ec5cf92
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 {
27    private static final String TAG = "RotateLayout";
28    private int mOrientation;
29    private View mChild;
30
31    public RotateLayout(Context context, AttributeSet attrs) {
32        super(context, attrs);
33    }
34
35    @Override
36    protected void onFinishInflate() {
37        mChild = getChildAt(0);
38        mChild.setPivotX(0);
39        mChild.setPivotY(0);
40    }
41
42    @Override
43    protected void onLayout(
44            boolean change, int left, int top, int right, int bottom) {
45        int width = right - left;
46        int height = bottom - top;
47        switch (mOrientation) {
48            case 0:
49            case 180:
50                mChild.layout(0, 0, width, height);
51                break;
52            case 90:
53            case 270:
54                mChild.layout(0, 0, height, width);
55                break;
56        }
57    }
58
59    @Override
60    protected void onMeasure(int widthSpec, int heightSpec) {
61        int w = 0, h = 0;
62        switch(mOrientation) {
63            case 0:
64            case 180:
65                measureChild(mChild, widthSpec, heightSpec);
66                w = mChild.getMeasuredWidth();
67                h = mChild.getMeasuredHeight();
68                break;
69            case 90:
70            case 270:
71                measureChild(mChild, heightSpec, widthSpec);
72                w = mChild.getMeasuredHeight();
73                h = mChild.getMeasuredWidth();
74                break;
75        }
76        setMeasuredDimension(w, h);
77
78        switch (mOrientation) {
79            case 0:
80                mChild.setTranslationX(0);
81                mChild.setTranslationY(0);
82                break;
83            case 90:
84                mChild.setTranslationX(0);
85                mChild.setTranslationY(h);
86                break;
87            case 180:
88                mChild.setTranslationX(w);
89                mChild.setTranslationY(h);
90                break;
91            case 270:
92                mChild.setTranslationX(w);
93                mChild.setTranslationY(0);
94                break;
95        }
96        mChild.setRotation(-mOrientation);
97    }
98
99    // Rotate the view counter-clockwise
100    public void setOrientation(int orientation) {
101        orientation = orientation % 360;
102        if (mOrientation == orientation) return;
103        mOrientation = orientation;
104        requestLayout();
105    }
106}
107