TransitionAnimationView.java revision 7913fd1634c84d04417125b9145d5db49b22ab05
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 */
16package com.android.contacts.widget;
17
18import com.android.contacts.R;
19
20import android.animation.Animator;
21import android.animation.Animator.AnimatorListener;
22import android.animation.AnimatorInflater;
23import android.content.Context;
24import android.content.res.TypedArray;
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.Color;
28import android.graphics.Paint;
29import android.graphics.Rect;
30import android.graphics.drawable.BitmapDrawable;
31import android.util.AttributeSet;
32import android.view.View;
33import android.view.ViewParent;
34import android.widget.FrameLayout;
35
36/**
37 * A container for a view that needs to have exit/enter animations when rebinding data.
38 * This layout should have a single child.  Just before rebinding data that child
39 * should make this call:
40 * <pre>
41 *   TransitionAnimationView.startAnimation(this);
42 * </pre>
43 */
44public class TransitionAnimationView extends FrameLayout implements AnimatorListener {
45
46    private View mPreviousStateView;
47    private Bitmap mPreviousStateBitmap;
48    private int mEnterAnimationId;
49    private int mExitAnimationId;
50    private int mAnimationDuration;
51    private Rect mClipMargins = new Rect();
52    private Rect mClipRect = new Rect();
53    private Animator mEnterAnimation;
54    private Animator mExitAnimation;
55
56    public TransitionAnimationView(Context context) {
57        this(context, null, 0);
58    }
59
60    public TransitionAnimationView(Context context, AttributeSet attrs) {
61        this(context, attrs, 0);
62    }
63
64    public TransitionAnimationView(Context context, AttributeSet attrs, int defStyle) {
65        super(context, attrs, defStyle);
66
67        TypedArray a = getContext().obtainStyledAttributes(
68                attrs, R.styleable.TransitionAnimationView);
69
70        mEnterAnimationId = a.getResourceId(R.styleable.TransitionAnimationView_enterAnimation,
71                android.R.animator.fade_in);
72        mExitAnimationId = a.getResourceId(R.styleable.TransitionAnimationView_exitAnimation,
73                android.R.animator.fade_out);
74        mClipMargins.left = a.getDimensionPixelOffset(
75                R.styleable.TransitionAnimationView_clipMarginLeft, 0);
76        mClipMargins.top = a.getDimensionPixelOffset(
77                R.styleable.TransitionAnimationView_clipMarginTop, 0);
78        mClipMargins.right = a.getDimensionPixelOffset(
79                R.styleable.TransitionAnimationView_clipMarginRight, 0);
80        mClipMargins.bottom = a.getDimensionPixelOffset(
81                R.styleable.TransitionAnimationView_clipMarginBottom, 0);
82        mAnimationDuration = a.getInt(
83                R.styleable.TransitionAnimationView_animationDuration, 100);
84
85        a.recycle();
86
87        mPreviousStateView = new View(context);
88        mPreviousStateView.setVisibility(View.INVISIBLE);
89        addView(mPreviousStateView);
90
91        mEnterAnimation = AnimatorInflater.loadAnimator(getContext(), mEnterAnimationId);
92        if (mEnterAnimation == null) {
93            throw new IllegalArgumentException("Invalid enter animation: " + mEnterAnimationId);
94        }
95        mEnterAnimation.addListener(this);
96        mEnterAnimation.setDuration(mAnimationDuration);
97
98        mExitAnimation = AnimatorInflater.loadAnimator(getContext(), mExitAnimationId);
99        if (mExitAnimation == null) {
100            throw new IllegalArgumentException("Invalid exit animation: " + mExitAnimationId);
101        }
102
103    }
104
105    @Override
106    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
107        super.onLayout(changed, left, top, right, bottom);
108        if (changed || mPreviousStateBitmap == null) {
109            if (mPreviousStateBitmap != null) {
110                mPreviousStateBitmap.recycle();
111                mPreviousStateBitmap = null;
112            }
113            int width = right - left;
114            int height = bottom - top;
115            if (width > 0 && height > 0) {
116                mPreviousStateBitmap = Bitmap.createBitmap(
117                        width, height, Bitmap.Config.ARGB_8888);
118                mPreviousStateView.setBackgroundDrawable(
119                        new BitmapDrawable(getContext().getResources(), mPreviousStateBitmap));
120                mClipRect.set(mClipMargins.left, mClipMargins.top,
121                        width - mClipMargins.right, height - mClipMargins.bottom);
122            } else {
123                mPreviousStateBitmap = null;
124                mPreviousStateView.setBackgroundDrawable(null);
125            }
126        }
127    }
128
129    @Override
130    protected void onDetachedFromWindow() {
131        super.onDetachedFromWindow();
132        mPreviousStateView.setBackgroundDrawable(null);
133        if (mPreviousStateBitmap != null) {
134            mPreviousStateBitmap.recycle();
135            mPreviousStateBitmap = null;
136        }
137    }
138
139    public static void startAnimation(View view, boolean closing) {
140        TransitionAnimationView container = null;
141        ViewParent parent = view.getParent();
142        while (parent instanceof View) {
143            if (parent instanceof TransitionAnimationView) {
144                container = (TransitionAnimationView) parent;
145                break;
146            }
147            parent = parent.getParent();
148        }
149
150        if (container != null) {
151            container.start(view, closing);
152        }
153    }
154
155    private void start(View view, boolean closing) {
156        if (mEnterAnimation.isRunning()) {
157            mEnterAnimation.end();
158        }
159        if (mExitAnimation.isRunning()) {
160            mExitAnimation.end();
161        }
162        if (view.getVisibility() != View.VISIBLE) {
163            if (!closing) {
164                mEnterAnimation.setTarget(view);
165                mEnterAnimation.start();
166            }
167        } else if (closing) {
168            mExitAnimation.setTarget(view);
169            mExitAnimation.start();
170        } else {
171            if (mPreviousStateBitmap == null) {
172                return;
173            }
174
175            Canvas canvas = new Canvas(mPreviousStateBitmap);
176            Paint paint = new Paint();
177            paint.setColor(Color.TRANSPARENT);
178            canvas.drawRect(0, 0, mPreviousStateBitmap.getWidth(), mPreviousStateBitmap.getHeight(),
179                    paint);
180            canvas.clipRect(mClipRect);
181            view.draw(canvas);
182            mPreviousStateView.setVisibility(View.VISIBLE);
183
184            mEnterAnimation.setTarget(view);
185            mEnterAnimation.start();
186        }
187    }
188
189    @Override
190    public void onAnimationEnd(Animator animation) {
191        mPreviousStateView.setVisibility(View.INVISIBLE);
192    }
193
194    @Override
195    public void onAnimationCancel(Animator animation) {
196    }
197
198    @Override
199    public void onAnimationStart(Animator animation) {
200    }
201
202    @Override
203    public void onAnimationRepeat(Animator animation) {
204    }
205}
206