Home | History | Annotate | Download | only in ui
      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.ui;
     18 
     19 import com.android.gallery3d.common.Utils;
     20 
     21 // FadeTexture is a texture which fades the given texture along the time.
     22 public 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     protected final BasicTexture mTexture;
     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 
     36     public FadeTexture(BasicTexture texture) {
     37         mTexture = texture;
     38         mWidth = mTexture.getWidth();
     39         mHeight = mTexture.getHeight();
     40         mIsOpaque = mTexture.isOpaque();
     41         mStartTime = now();
     42         mIsAnimating = true;
     43     }
     44 
     45     @Override
     46     public void draw(GLCanvas canvas, int x, int y) {
     47         draw(canvas, x, y, mWidth, mHeight);
     48     }
     49 
     50     @Override
     51     public boolean isOpaque() {
     52         return mIsOpaque;
     53     }
     54 
     55     @Override
     56     public int getWidth() {
     57         return mWidth;
     58     }
     59 
     60     @Override
     61     public int getHeight() {
     62         return mHeight;
     63     }
     64 
     65     public boolean isAnimating() {
     66         if (mIsAnimating) {
     67             if (now() - mStartTime >= DURATION) {
     68                 mIsAnimating = false;
     69             }
     70         }
     71         return mIsAnimating;
     72     }
     73 
     74     protected float getRatio() {
     75         float r = (float)(now() - mStartTime) / DURATION;
     76         return Utils.clamp(1.0f - r, 0.0f, 1.0f);
     77     }
     78 
     79     private long now() {
     80         return AnimationTime.get();
     81     }
     82 }
     83