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 import android.os.SystemClock;
     22 
     23 // FadeInTexture is a texture which begins with a color, then gradually animates
     24 // into a given texture.
     25 public class FadeInTexture implements Texture {
     26     private static final String TAG = "FadeInTexture";
     27 
     28     // The duration of the animation in milliseconds
     29     private static final int DURATION = 180;
     30 
     31     private final BasicTexture mTexture;
     32     private final int mColor;
     33     private final long mStartTime;
     34     private final int mWidth;
     35     private final int mHeight;
     36     private final boolean mIsOpaque;
     37     private boolean mIsAnimating;
     38 
     39     public FadeInTexture(int color, BasicTexture texture) {
     40         mColor = color;
     41         mTexture = texture;
     42         mWidth = mTexture.getWidth();
     43         mHeight = mTexture.getHeight();
     44         mIsOpaque = mTexture.isOpaque();
     45         mStartTime = now();
     46         mIsAnimating = true;
     47     }
     48 
     49     public void draw(GLCanvas canvas, int x, int y) {
     50         draw(canvas, x, y, mWidth, mHeight);
     51     }
     52 
     53     public void draw(GLCanvas canvas, int x, int y, int w, int h) {
     54         if (isAnimating()) {
     55             canvas.drawMixed(mTexture, mColor, getRatio(), x, y, w, h);
     56         } else {
     57             mTexture.draw(canvas, x, y, w, h);
     58         }
     59     }
     60 
     61     public boolean isOpaque() {
     62         return mIsOpaque;
     63     }
     64 
     65     public int getWidth() {
     66         return mWidth;
     67     }
     68 
     69     public int getHeight() {
     70         return mHeight;
     71     }
     72 
     73     public boolean isAnimating() {
     74         if (mIsAnimating) {
     75             if (now() - mStartTime >= DURATION) {
     76                 mIsAnimating = false;
     77             }
     78         }
     79         return mIsAnimating;
     80     }
     81 
     82     private float getRatio() {
     83         float r = (float)(now() - mStartTime) / DURATION;
     84         return Utils.clamp(1.0f - r, 0.0f, 1.0f);
     85     }
     86 
     87     private long now() {
     88         return SystemClock.uptimeMillis();
     89     }
     90 }
     91