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.graphics.Matrix; 20 import android.view.MotionEvent; 21 22 import javax.microedition.khronos.opengles.GL11; 23 24 25 class RotatePane extends GLView { 26 27 public static final int UP = 0; 28 public static final int RIGHT = 1; 29 public static final int DOWN = 2; 30 public static final int LEFT = 3; 31 32 private int mOrientation = 0; 33 34 private GLView mChild; 35 36 @Override 37 protected void onLayout( 38 boolean change, int left, int top, int right, int bottom) { 39 int width = right - left; 40 int height = bottom - top; 41 switch (mOrientation) { 42 case UP: 43 case DOWN: 44 mChild.layout(0, 0, width, height); 45 break; 46 case LEFT: 47 case RIGHT: 48 mChild.layout(0, 0, height, width); 49 break; 50 } 51 } 52 53 @Override 54 protected void onMeasure(int widthSpec, int heightSpec) { 55 GLView c = mChild; 56 switch(mOrientation) { 57 case UP: 58 case DOWN: 59 c.measure(widthSpec, heightSpec); 60 setMeasuredSize(c.getMeasuredWidth(), c.getMeasuredHeight()); 61 break; 62 case LEFT: 63 case RIGHT: 64 mChild.measure(heightSpec, widthSpec); 65 setMeasuredSize(c.getMeasuredHeight(), c.getMeasuredWidth()); 66 } 67 } 68 69 @Override 70 protected void render(GLRootView view, GL11 gl) { 71 72 if (mOrientation == UP) { 73 mChild.render(view, gl); 74 return; 75 } 76 77 view.pushTransform(); 78 Matrix matrix = view.getTransformation().getMatrix(); 79 float width = getWidth(); 80 float height = getHeight(); 81 switch (mOrientation) { 82 case DOWN: 83 matrix.preRotate(180, width / 2, height / 2); 84 break; 85 case LEFT: 86 matrix.preRotate(270, height / 2, height / 2); 87 break; 88 case RIGHT: 89 matrix.preRotate(90, width / 2, width / 2); 90 break; 91 } 92 mChild.render(view, gl); 93 view.popTransform(); 94 } 95 96 @Override 97 protected boolean dispatchTouchEvent(MotionEvent event) { 98 float x = event.getX(); 99 float y = event.getY(); 100 float width = getWidth(); 101 float height = getHeight(); 102 switch (mOrientation) { 103 case DOWN: event.setLocation(width - x, height - y); break; 104 case LEFT: event.setLocation(height - y, x); break; 105 case RIGHT: event.setLocation(y, width - x); break; 106 } 107 boolean result = mChild.dispatchTouchEvent(event); 108 event.setLocation(x, y); 109 return result; 110 } 111 112 public void setOrientation(int orientation) { 113 if (mOrientation == orientation) return; 114 mOrientation = orientation; 115 requestLayout(); 116 } 117 118 public void setContent(GLView view) { 119 if (mChild == view) return; 120 121 if (mChild != null) super.clearComponents(); 122 mChild = view; 123 if (view != null) super.addComponent(view); 124 requestLayout(); 125 } 126 127 @Override 128 public void addComponent(GLView view) { 129 throw new UnsupportedOperationException("use setContent(GLView)"); 130 } 131 132 @Override 133 public void clearComponents() { 134 throw new UnsupportedOperationException("use setContent(null)"); 135 } 136 } 137