185609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov/*
285609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * Copyright (C) 2010 The Android Open Source Project
385609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov *
485609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * Licensed under the Apache License, Version 2.0 (the "License");
585609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * you may not use this file except in compliance with the License.
685609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * You may obtain a copy of the License at
785609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov *
885609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov *      http://www.apache.org/licenses/LICENSE-2.0
985609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov *
1085609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * Unless required by applicable law or agreed to in writing, software
1185609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * distributed under the License is distributed on an "AS IS" BASIS,
1285609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1385609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * See the License for the specific language governing permissions and
1485609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov * limitations under the License.
1585609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov */
1685609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikovpackage com.android.contacts.widget;
1785609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov
18cf3d9f04955a8ec3cb405b8a4ddcaddcece2a245Daniel Lehmannimport android.animation.ObjectAnimator;
1985609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikovimport android.content.Context;
2085609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikovimport android.graphics.Color;
2185609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikovimport android.util.AttributeSet;
2285609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikovimport android.view.View;
2385609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikovimport android.widget.FrameLayout;
2485609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov
2585609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov/**
266e607d54ce08ebcc78d94963646a42605697f132Maurice Chu * A container that places a masking view on top of all other views.  The masking view can be
276e607d54ce08ebcc78d94963646a42605697f132Maurice Chu * faded in and out.  Currently, the masking view is solid color white.
2885609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov */
296e607d54ce08ebcc78d94963646a42605697f132Maurice Chupublic class TransitionAnimationView extends FrameLayout {
306e607d54ce08ebcc78d94963646a42605697f132Maurice Chu    private View mMaskingView;
316e607d54ce08ebcc78d94963646a42605697f132Maurice Chu    private ObjectAnimator mAnimator;
3285609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov
3385609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    public TransitionAnimationView(Context context) {
3485609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov        this(context, null, 0);
3585609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    }
3685609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov
3785609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    public TransitionAnimationView(Context context, AttributeSet attrs) {
3885609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov        this(context, attrs, 0);
3985609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    }
4085609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov
4185609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    public TransitionAnimationView(Context context, AttributeSet attrs, int defStyle) {
4285609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov        super(context, attrs, defStyle);
4385609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    }
4485609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov
4585609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    @Override
46cf3d9f04955a8ec3cb405b8a4ddcaddcece2a245Daniel Lehmann    protected void onFinishInflate() {
47cf3d9f04955a8ec3cb405b8a4ddcaddcece2a245Daniel Lehmann        super.onFinishInflate();
486e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        mMaskingView = new View(getContext());
496e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        mMaskingView.setVisibility(View.INVISIBLE);
506e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        mMaskingView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
51cf3d9f04955a8ec3cb405b8a4ddcaddcece2a245Daniel Lehmann                LayoutParams.MATCH_PARENT));
526e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        mMaskingView.setBackgroundColor(Color.WHITE);
536e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        addView(mMaskingView);
5485609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    }
5585609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov
566e607d54ce08ebcc78d94963646a42605697f132Maurice Chu    public void setMaskVisibility(boolean flag) {
576e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        if (flag) {
586e607d54ce08ebcc78d94963646a42605697f132Maurice Chu            mMaskingView.setAlpha(1.0f);
596e607d54ce08ebcc78d94963646a42605697f132Maurice Chu            mMaskingView.setVisibility(View.VISIBLE);
606e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        } else {
616e607d54ce08ebcc78d94963646a42605697f132Maurice Chu            mMaskingView.setVisibility(View.INVISIBLE);
62bcf864578e0fe25813af75f33f5560709a5c8515Dmitri Plotnikov        }
63bcf864578e0fe25813af75f33f5560709a5c8515Dmitri Plotnikov    }
64bcf864578e0fe25813af75f33f5560709a5c8515Dmitri Plotnikov
656e607d54ce08ebcc78d94963646a42605697f132Maurice Chu    /**
66d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee     * Starts the transition of showing or hiding the mask. To the user, the view will appear to
67d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee     * either fade in or out of view.
68d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee     *
69d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee     * @param showMask If true, the mask the mask will be set to be invisible then fade into hide
70d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee     * the other views in this container. If false, the the mask will be set to be hide other
71d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee     * views initially.  Then, the other views in this container will be revealed.
72d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee     * @param duration The duration the animation should last for. If -1, the system default(300)
73d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee     * is used.
746e607d54ce08ebcc78d94963646a42605697f132Maurice Chu     */
75d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee    public void startMaskTransition(boolean showMask, int duration) {
766e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        // Stop any animation that may still be running.
776e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        if (mAnimator != null && mAnimator.isRunning()) {
786e607d54ce08ebcc78d94963646a42605697f132Maurice Chu            mAnimator.end();
7958d8be3ff47747454afdfadfe2f566a183cdee3bDmitri Plotnikov        }
806e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        mMaskingView.setVisibility(View.VISIBLE);
816e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        if (showMask) {
826e607d54ce08ebcc78d94963646a42605697f132Maurice Chu            mAnimator = ObjectAnimator.ofFloat(mMaskingView, View.ALPHA, 0.0f, 1.0f);
836e607d54ce08ebcc78d94963646a42605697f132Maurice Chu        } else {
846e607d54ce08ebcc78d94963646a42605697f132Maurice Chu            // asked to hide the view
856e607d54ce08ebcc78d94963646a42605697f132Maurice Chu            mAnimator = ObjectAnimator.ofFloat(mMaskingView, View.ALPHA, 1.0f, 0.0f);
8685609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov        }
87d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee        if (duration != -1) {
88d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee            mAnimator.setDuration(duration);
89d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee        }
90d7b32b6f1293362d1ca186a99a2cc80791a291deYorke Lee        mAnimator.start();
9185609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov    }
9285609edf326d86d4a2b67be3610f8de7cce28d1dDmitri Plotnikov}
93