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