Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | package com.android.contacts.widget; |
| 17 | |
Daniel Lehmann | cf3d9f0 | 2012-03-07 17:38:34 -0800 | [diff] [blame] | 18 | import android.animation.ObjectAnimator; |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 20 | import android.graphics.Color; |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 23 | import android.widget.FrameLayout; |
| 24 | |
| 25 | /** |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 26 | * A container that places a masking view on top of all other views. The masking view can be |
| 27 | * faded in and out. Currently, the masking view is solid color white. |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 28 | */ |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 29 | public class TransitionAnimationView extends FrameLayout { |
| 30 | private View mMaskingView; |
| 31 | private ObjectAnimator mAnimator; |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 32 | |
| 33 | public TransitionAnimationView(Context context) { |
| 34 | this(context, null, 0); |
| 35 | } |
| 36 | |
| 37 | public TransitionAnimationView(Context context, AttributeSet attrs) { |
| 38 | this(context, attrs, 0); |
| 39 | } |
| 40 | |
| 41 | public TransitionAnimationView(Context context, AttributeSet attrs, int defStyle) { |
| 42 | super(context, attrs, defStyle); |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
Daniel Lehmann | cf3d9f0 | 2012-03-07 17:38:34 -0800 | [diff] [blame] | 46 | protected void onFinishInflate() { |
| 47 | super.onFinishInflate(); |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 48 | mMaskingView = new View(getContext()); |
| 49 | mMaskingView.setVisibility(View.INVISIBLE); |
| 50 | mMaskingView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
Daniel Lehmann | cf3d9f0 | 2012-03-07 17:38:34 -0800 | [diff] [blame] | 51 | LayoutParams.MATCH_PARENT)); |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 52 | mMaskingView.setBackgroundColor(Color.WHITE); |
| 53 | addView(mMaskingView); |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 56 | public void setMaskVisibility(boolean flag) { |
| 57 | if (flag) { |
| 58 | mMaskingView.setAlpha(1.0f); |
| 59 | mMaskingView.setVisibility(View.VISIBLE); |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 60 | } else { |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 61 | mMaskingView.setVisibility(View.INVISIBLE); |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 65 | /** |
Yorke Lee | d7b32b6 | 2012-12-26 16:17:34 -0800 | [diff] [blame] | 66 | * Starts the transition of showing or hiding the mask. To the user, the view will appear to |
| 67 | * either fade in or out of view. |
| 68 | * |
| 69 | * @param showMask If true, the mask the mask will be set to be invisible then fade into hide |
| 70 | * the other views in this container. If false, the the mask will be set to be hide other |
| 71 | * views initially. Then, the other views in this container will be revealed. |
| 72 | * @param duration The duration the animation should last for. If -1, the system default(300) |
| 73 | * is used. |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 74 | */ |
Yorke Lee | d7b32b6 | 2012-12-26 16:17:34 -0800 | [diff] [blame] | 75 | public void startMaskTransition(boolean showMask, int duration) { |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 76 | // Stop any animation that may still be running. |
| 77 | if (mAnimator != null && mAnimator.isRunning()) { |
| 78 | mAnimator.end(); |
| 79 | } |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 80 | mMaskingView.setVisibility(View.VISIBLE); |
| 81 | if (showMask) { |
| 82 | mAnimator = ObjectAnimator.ofFloat(mMaskingView, View.ALPHA, 0.0f, 1.0f); |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 83 | } else { |
| 84 | // asked to hide the view |
| 85 | mAnimator = ObjectAnimator.ofFloat(mMaskingView, View.ALPHA, 1.0f, 0.0f); |
Maurice Chu | 6e607d5 | 2012-05-15 16:57:26 -0700 | [diff] [blame] | 86 | } |
Yorke Lee | d7b32b6 | 2012-12-26 16:17:34 -0800 | [diff] [blame] | 87 | if (duration != -1) { |
| 88 | mAnimator.setDuration(duration); |
| 89 | } |
| 90 | mAnimator.start(); |
Dmitri Plotnikov | 85609ed | 2010-11-11 15:59:01 -0800 | [diff] [blame] | 91 | } |
| 92 | } |