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