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