• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.util.Log;
22 import android.graphics.Canvas;
23 import android.view.ViewGroup;
24 import android.widget.FrameLayout;
25 import android.widget.TextView;
26 
27 import com.android.camera.R;
28 
29 // This is a rectangle that contains recording text view. Canvas is rotated
30 // before passing to recording text.
31 public class RotateRecordingTime extends FrameLayout {
32     private static final String TAG = "RotateRecordingTime";
33     private float mTextSize;
34     private int mOrientation;
35 
RotateRecordingTime(Context context, AttributeSet attrs)36     public RotateRecordingTime(Context context, AttributeSet attrs) {
37         super(context, attrs);
38     }
39 
40     @Override
onFinishInflate()41     protected void onFinishInflate() {
42         TextView v = (TextView) findViewById(R.id.recording_time);
43         mTextSize = (float) v.getTextSize();
44     }
45 
46     // degrees in counter-clockwise
setOrientation(int degrees)47     public void setOrientation(int degrees) {
48         if (degrees % 90 == 0) {
49             mOrientation = degrees % 360;
50             if (mOrientation < 0) mOrientation += 360;
51         } else {
52             Log.e(TAG, "Invalid orientation=" + degrees);
53         }
54     }
55 
56     @Override
dispatchDraw(Canvas canvas)57     protected void dispatchDraw(Canvas canvas) {
58         canvas.save();
59         float width = (float) getWidth();
60         float height = (float) getHeight();
61         if (mOrientation == 0 || mOrientation == 180) {
62             canvas.translate(0, height / 2 - mTextSize / 2);
63         } else {
64             canvas.translate(-width / 2 + mTextSize / 2, 0);
65         }
66         // Rotate the canvas in the opposite direction to compensate.
67         canvas.rotate(-mOrientation, width / 2, height / 2);
68         super.dispatchDraw(canvas);
69         canvas.restore();
70     }
71 }
72 
73