TransitionAnimationView.java revision 58d8be3ff47747454afdfadfe2f566a183cdee3b
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.anim.animator_fade_in); 72 mExitAnimationId = a.getResourceId(R.styleable.TransitionAnimationView_exitAnimation, 73 android.R.anim.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) { 109 mPreviousStateBitmap = Bitmap.createBitmap( 110 right - left, bottom - top, Bitmap.Config.ARGB_8888); 111 mPreviousStateView.setBackgroundDrawable( 112 new BitmapDrawable(getContext().getResources(), mPreviousStateBitmap)); 113 mClipRect.set(mClipMargins.left, mClipMargins.top, 114 right - left - mClipMargins.right, bottom - top - mClipMargins.bottom); 115 } 116 } 117 118 public static void startAnimation(View view, boolean closing) { 119 TransitionAnimationView container = null; 120 ViewParent parent = view.getParent(); 121 while (parent instanceof View) { 122 if (parent instanceof TransitionAnimationView) { 123 container = (TransitionAnimationView) parent; 124 break; 125 } 126 parent = parent.getParent(); 127 } 128 129 if (container != null) { 130 container.start(view, closing); 131 } 132 } 133 134 private void start(View view, boolean closing) { 135 if (mEnterAnimation.isRunning()) { 136 mEnterAnimation.end(); 137 } 138 if (mExitAnimation.isRunning()) { 139 mExitAnimation.end(); 140 } 141 if (view.getVisibility() != View.VISIBLE) { 142 if (!closing) { 143 mEnterAnimation.setTarget(view); 144 mEnterAnimation.start(); 145 } 146 } else if (closing) { 147 mExitAnimation.setTarget(view); 148 mExitAnimation.start(); 149 } else { 150 Canvas canvas = new Canvas(mPreviousStateBitmap); 151 Paint paint = new Paint(); 152 paint.setColor(Color.TRANSPARENT); 153 canvas.drawRect(0, 0, mPreviousStateBitmap.getWidth(), mPreviousStateBitmap.getHeight(), 154 paint); 155 canvas.clipRect(mClipRect); 156 view.draw(canvas); 157 mPreviousStateView.setVisibility(View.VISIBLE); 158 159 mEnterAnimation.setTarget(view); 160 mEnterAnimation.start(); 161 } 162 } 163 164 @Override 165 public void onAnimationEnd(Animator animation) { 166 mPreviousStateView.setVisibility(View.INVISIBLE); 167 } 168 169 @Override 170 public void onAnimationCancel(Animator animation) { 171 } 172 173 @Override 174 public void onAnimationStart(Animator animation) { 175 } 176 177 @Override 178 public void onAnimationRepeat(Animator animation) { 179 } 180} 181