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