CaptureAnimManager.java revision 70d41293e9dad8be7a1f2c556ff7c7334a60c8f5
1/*
2 * Copyright (C) 2012 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
17package com.android.camera;
18
19import android.graphics.Color;
20import android.os.SystemClock;
21import android.view.animation.DecelerateInterpolator;
22import android.view.animation.Interpolator;
23
24import com.android.gallery3d.ui.GLCanvas;
25import com.android.gallery3d.ui.RawTexture;
26
27/**
28 * Class to handle the capture animation.
29 */
30public class CaptureAnimManager {
31    public static final int ANIM_FLASH = 1;
32    public static final int ANIM_SLIDE = 2;
33
34    @SuppressWarnings("unused")
35    private static final String TAG = "CAM_Capture";
36    private static final int TIME_FLASH = 200;
37    private static final int TIME_SLIDE = 400;  // milliseconds.
38
39    private final Interpolator mSlideInterpolator = new DecelerateInterpolator();
40
41    private int mAnimOrientation;  // Could be 0, 90, 180 or 270 degrees.
42    private long mAnimStartTime;  // milliseconds.
43    private float mX;  // The center of the whole view including preview and review.
44    private float mY;
45    private float mDelta;
46    private int mDrawWidth;
47    private int mDrawHeight;
48    private int mAnimType;
49
50    /* preview: camera preview view.
51     * review: view of picture just taken.
52     */
53    public CaptureAnimManager() {
54    }
55
56    public void setOrientation(int animOrientation) {
57        mAnimOrientation = animOrientation;
58    }
59
60    public void animateSlide() {
61        if (mAnimType != ANIM_FLASH) {
62            return;
63        }
64        mAnimType = ANIM_SLIDE;
65        mAnimStartTime = SystemClock.uptimeMillis();
66    }
67
68    // x, y, w and h: the rectangle area where the animation takes place.
69    public void startAnimation(int x, int y, int w, int h) {
70        mAnimStartTime = SystemClock.uptimeMillis();
71        mAnimType = ANIM_FLASH;
72        // Set the views to the initial positions.
73        mDrawWidth = w;
74        mDrawHeight = h;
75        mX = x;
76        mY = y;
77        switch (mAnimOrientation) {
78            case 0:  // Preview is on the left.
79                mDelta = w;
80                break;
81            case 90:  // Preview is below.
82                mDelta = -h;
83                break;
84            case 180:  // Preview on the right.
85                mDelta = -w;
86                break;
87            case 270:  // Preview is above.
88                mDelta = h;
89                break;
90        }
91    }
92
93    // Returns true if the animation has been drawn.
94    public boolean drawAnimation(GLCanvas canvas, CameraScreenNail preview,
95                RawTexture review) {
96        long timeDiff = SystemClock.uptimeMillis() - mAnimStartTime;
97        // Check if the animation is over
98        if (mAnimType == ANIM_SLIDE && timeDiff > TIME_SLIDE) return false;
99
100        if (mAnimType == ANIM_FLASH) {
101            review.draw(canvas, (int) mX, (int) mY, mDrawWidth, mDrawHeight);
102            if (timeDiff < TIME_FLASH) {
103                float f = 0.3f - 0.3f * timeDiff / TIME_FLASH;
104                int color = Color.argb((int) (255 * f), 255, 255, 255);
105                canvas.fillRect(mX, mY, mDrawWidth, mDrawHeight, color);
106            }
107        } else if (mAnimType == ANIM_SLIDE) {
108            float fraction = (float) (timeDiff) / TIME_SLIDE;
109            float x = mX;
110            float y = mY;
111            if (mAnimOrientation == 0 || mAnimOrientation == 180) {
112                x = x + mDelta * mSlideInterpolator.getInterpolation(fraction);
113            } else {
114                y = y + mDelta * mSlideInterpolator.getInterpolation(fraction);
115            }
116            // float alpha = canvas.getAlpha();
117            // canvas.setAlpha(fraction);
118            preview.directDraw(canvas, (int) mX, (int) mY,
119                    mDrawWidth, mDrawHeight);
120            // canvas.setAlpha(alpha);
121
122            review.draw(canvas, (int) x, (int) y, mDrawWidth, mDrawHeight);
123        } else {
124            return false;
125        }
126        return true;
127    }
128}
129