1/*
2 * Copyright (C) 2011 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.gallery3d.ui;
18
19import com.android.gallery3d.common.Utils;
20
21// FadeTexture is a texture which fades the given texture along the time.
22public abstract class FadeTexture implements Texture {
23    @SuppressWarnings("unused")
24    private static final String TAG = "FadeTexture";
25
26    // The duration of the fading animation in milliseconds
27    public static final int DURATION = 180;
28
29    private final long mStartTime;
30    private final int mWidth;
31    private final int mHeight;
32    private final boolean mIsOpaque;
33    private boolean mIsAnimating;
34
35    public FadeTexture(int width, int height, boolean opaque) {
36        mWidth = width;
37        mHeight = height;
38        mIsOpaque = opaque;
39        mStartTime = now();
40        mIsAnimating = true;
41    }
42
43    @Override
44    public void draw(GLCanvas canvas, int x, int y) {
45        draw(canvas, x, y, mWidth, mHeight);
46    }
47
48    @Override
49    public boolean isOpaque() {
50        return mIsOpaque;
51    }
52
53    @Override
54    public int getWidth() {
55        return mWidth;
56    }
57
58    @Override
59    public int getHeight() {
60        return mHeight;
61    }
62
63    public boolean isAnimating() {
64        if (mIsAnimating) {
65            if (now() - mStartTime >= DURATION) {
66                mIsAnimating = false;
67            }
68        }
69        return mIsAnimating;
70    }
71
72    protected float getRatio() {
73        float r = (float)(now() - mStartTime) / DURATION;
74        return Utils.clamp(1.0f - r, 0.0f, 1.0f);
75    }
76
77    private long now() {
78        return AnimationTime.get();
79    }
80}
81