RotateLayout.java revision cce55282e5ae2414811f2e416961f318f553c400
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.graphics.Canvas;
21import android.graphics.Matrix;
22import android.util.AttributeSet;
23import android.view.MotionEvent;
24import android.view.View;
25import android.view.ViewGroup;
26
27import com.android.gallery3d.util.MotionEventHelper;
28
29// A RotateLayout is designed to display a single item and provides the
30// capabilities to rotate the item.
31public class RotateLayout extends ViewGroup implements Rotatable {
32    @SuppressWarnings("unused")
33    private static final String TAG = "RotateLayout";
34    private int mOrientation;
35    private Matrix mMatrix = new Matrix();
36    protected View mChild;
37
38    public RotateLayout(Context context, AttributeSet attrs) {
39        super(context, attrs);
40        // The transparent background here is a workaround of the render issue
41        // happened when the view is rotated as the device's orientation
42        // changed. The view looks fine in landscape. After rotation, the view
43        // is invisible.
44        setBackgroundResource(android.R.color.transparent);
45    }
46
47    @Override
48    protected void onFinishInflate() {
49        mChild = getChildAt(0);
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
70    public boolean dispatchTouchEvent(MotionEvent event) {
71        final int w = getMeasuredWidth();
72        final int h = getMeasuredHeight();
73        switch (mOrientation) {
74            case 0:
75                mMatrix.setTranslate(0, 0);
76                break;
77            case 90:
78                mMatrix.setTranslate(0, -h);
79                break;
80            case 180:
81                mMatrix.setTranslate(-w, -h);
82                break;
83            case 270:
84                mMatrix.setTranslate(-w, 0);
85                break;
86        }
87        mMatrix.postRotate(mOrientation);
88        event = MotionEventHelper.transformEvent(event, mMatrix);
89        return super.dispatchTouchEvent(event);
90    }
91
92    @Override
93    protected void dispatchDraw(Canvas canvas) {
94        canvas.save();
95        int w = getMeasuredWidth();
96        int h = getMeasuredHeight();
97        switch (mOrientation) {
98            case 0:
99                canvas.translate(0, 0);
100                break;
101            case 90:
102                canvas.translate(0, h);
103                break;
104            case 180:
105                canvas.translate(w, h);
106                break;
107            case 270:
108                canvas.translate(w, 0);
109                break;
110        }
111        canvas.rotate(-mOrientation, 0, 0);
112        super.dispatchDraw(canvas);
113        canvas.restore();
114    }
115
116    @Override
117    protected void onMeasure(int widthSpec, int heightSpec) {
118        int w = 0, h = 0;
119        switch(mOrientation) {
120            case 0:
121            case 180:
122                measureChild(mChild, widthSpec, heightSpec);
123                w = mChild.getMeasuredWidth();
124                h = mChild.getMeasuredHeight();
125                break;
126            case 90:
127            case 270:
128                measureChild(mChild, heightSpec, widthSpec);
129                w = mChild.getMeasuredHeight();
130                h = mChild.getMeasuredWidth();
131                break;
132        }
133        setMeasuredDimension(w, h);
134    }
135
136    @Override
137    public boolean shouldDelayChildPressedState() {
138        return false;
139    }
140
141    // Rotate the view counter-clockwise
142    @Override
143    public void setOrientation(int orientation, boolean animation) {
144        orientation = orientation % 360;
145        if (mOrientation == orientation) return;
146        mOrientation = orientation;
147        requestLayout();
148    }
149}
150