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 
18 import android.animation.ObjectAnimator;
19 import android.content.Context;
20 import android.graphics.Color;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.FrameLayout;
24 
25 /**
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.
28  */
29 public class TransitionAnimationView extends FrameLayout {
30     private View mMaskingView;
31     private ObjectAnimator mAnimator;
32 
TransitionAnimationView(Context context)33     public TransitionAnimationView(Context context) {
34         this(context, null, 0);
35     }
36 
TransitionAnimationView(Context context, AttributeSet attrs)37     public TransitionAnimationView(Context context, AttributeSet attrs) {
38         this(context, attrs, 0);
39     }
40 
TransitionAnimationView(Context context, AttributeSet attrs, int defStyle)41     public TransitionAnimationView(Context context, AttributeSet attrs, int defStyle) {
42         super(context, attrs, defStyle);
43     }
44 
45     @Override
onFinishInflate()46     protected void onFinishInflate() {
47         super.onFinishInflate();
48         mMaskingView = new View(getContext());
49         mMaskingView.setVisibility(View.INVISIBLE);
50         mMaskingView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
51                 LayoutParams.MATCH_PARENT));
52         mMaskingView.setBackgroundColor(Color.WHITE);
53         addView(mMaskingView);
54     }
55 
setMaskVisibility(boolean flag)56     public void setMaskVisibility(boolean flag) {
57         if (flag) {
58             mMaskingView.setAlpha(1.0f);
59             mMaskingView.setVisibility(View.VISIBLE);
60         } else {
61             mMaskingView.setVisibility(View.INVISIBLE);
62         }
63     }
64 
65     /**
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.
74      */
startMaskTransition(boolean showMask, int duration)75     public void startMaskTransition(boolean showMask, int duration) {
76         // Stop any animation that may still be running.
77         if (mAnimator != null && mAnimator.isRunning()) {
78             mAnimator.end();
79         }
80         mMaskingView.setVisibility(View.VISIBLE);
81         if (showMask) {
82             mAnimator = ObjectAnimator.ofFloat(mMaskingView, View.ALPHA, 0.0f, 1.0f);
83         } else {
84             // asked to hide the view
85             mAnimator = ObjectAnimator.ofFloat(mMaskingView, View.ALPHA, 1.0f, 0.0f);
86         }
87         if (duration != -1) {
88             mAnimator.setDuration(duration);
89         }
90         mAnimator.start();
91     }
92 }
93