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