blob: 2e277a968e509db18c2af8007fe21f45c516e5d6 [file] [log] [blame]
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -08001/*
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 */
16package com.android.contacts.widget;
17
Daniel Lehmanncf3d9f02012-03-07 17:38:34 -080018import android.animation.ObjectAnimator;
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080019import android.content.Context;
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080020import android.graphics.Color;
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080021import android.util.AttributeSet;
22import android.view.View;
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080023import android.widget.FrameLayout;
24
25/**
Maurice Chu6e607d52012-05-15 16:57:26 -070026 * 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 Plotnikov85609ed2010-11-11 15:59:01 -080028 */
Maurice Chu6e607d52012-05-15 16:57:26 -070029public class TransitionAnimationView extends FrameLayout {
30 private View mMaskingView;
31 private ObjectAnimator mAnimator;
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080032
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 Plotnikov85609ed2010-11-11 15:59:01 -080043 }
44
45 @Override
Daniel Lehmanncf3d9f02012-03-07 17:38:34 -080046 protected void onFinishInflate() {
47 super.onFinishInflate();
Maurice Chu6e607d52012-05-15 16:57:26 -070048 mMaskingView = new View(getContext());
49 mMaskingView.setVisibility(View.INVISIBLE);
50 mMaskingView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
Daniel Lehmanncf3d9f02012-03-07 17:38:34 -080051 LayoutParams.MATCH_PARENT));
Maurice Chu6e607d52012-05-15 16:57:26 -070052 mMaskingView.setBackgroundColor(Color.WHITE);
53 addView(mMaskingView);
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080054 }
55
Maurice Chu6e607d52012-05-15 16:57:26 -070056 public void setMaskVisibility(boolean flag) {
57 if (flag) {
58 mMaskingView.setAlpha(1.0f);
59 mMaskingView.setVisibility(View.VISIBLE);
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080060 } else {
Maurice Chu6e607d52012-05-15 16:57:26 -070061 mMaskingView.setVisibility(View.INVISIBLE);
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080062 }
63 }
64
Maurice Chu6e607d52012-05-15 16:57:26 -070065 /**
Yorke Leed7b32b62012-12-26 16:17:34 -080066 * 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 Chu6e607d52012-05-15 16:57:26 -070074 */
Yorke Leed7b32b62012-12-26 16:17:34 -080075 public void startMaskTransition(boolean showMask, int duration) {
Maurice Chu6e607d52012-05-15 16:57:26 -070076 // Stop any animation that may still be running.
77 if (mAnimator != null && mAnimator.isRunning()) {
78 mAnimator.end();
79 }
Maurice Chu6e607d52012-05-15 16:57:26 -070080 mMaskingView.setVisibility(View.VISIBLE);
81 if (showMask) {
82 mAnimator = ObjectAnimator.ofFloat(mMaskingView, View.ALPHA, 0.0f, 1.0f);
Maurice Chu6e607d52012-05-15 16:57:26 -070083 } else {
84 // asked to hide the view
85 mAnimator = ObjectAnimator.ofFloat(mMaskingView, View.ALPHA, 1.0f, 0.0f);
Maurice Chu6e607d52012-05-15 16:57:26 -070086 }
Yorke Leed7b32b62012-12-26 16:17:34 -080087 if (duration != -1) {
88 mAnimator.setDuration(duration);
89 }
90 mAnimator.start();
Dmitri Plotnikov85609ed2010-11-11 15:59:01 -080091 }
92}